Spherical forums

Sphere Development => Sphere Support => Script Support => Topic started by: williammah on March 25, 2014, 01:42:07 am

Title: Name-typing script
Post by: williammah on March 25, 2014, 01:42:07 am
Any ideas for a script that does that name-typing thing that RPGs let you do? I don't really get what GetKeyString is supposed to do, but I'm pretty sure it involves that as well.

Just give me a rough idea on how it can be done and I'll try to act on that advice.
Title: Re: Name-typing script
Post by: Rahkiin on March 25, 2014, 01:51:35 am
You do the following, roughly:

Code: (javascript) [Select]

var name = "";
var key;
while(!(IsKeyPressed {
    render scene with the name
    flipscreen()

    key = GetKey(); // waits for a key
    if(key == KEY_ENTER)
        break; // finished
    if(key == KEY_BACKSPACE)
        // remove last character unless name.length == 0
    else
        name += GetKeyString(key,IsKeyPressed(KEY_SHIFT))
}

And the end of the loop you have a name in the name variable. Roughly.


Good luck!


// Rahkiin
Title: Re: Name-typing script
Post by: Flying Jester on March 25, 2014, 02:09:07 am
Just going to modify that a little...
Code: (javascript) [Select]

var Finish = false;
var name = "";
/////
// Do your rendering of the name entry screen
//
  flipscreen();

  /////
  // Process all key events
  //
  while(AreKeysLeft()){
    var key = GetKey();
    if(key == KEY_ENTER){
        Finish = true;
        break;
    }
    if(key == KEY_BACKSPACE)
      name = name.substring(0, name.length-1); //This function is extremely forgiving.
    else
        name += GetKeyString(key,IsKeyPressed(KEY_SHIFT));
  }
}


This way, there is no limit or enforcement of how many keys are processed per frame.

For instance, then when you draw the text of the name, you could do this:

Code: [Select]

font.drawText(lolX, lolY, name+((Math.floor(GetTime()/2000)%2)?"|":""));

That would put a cursor on the end of the name, and flash it at once a second.
Title: Re: Name-typing script
Post by: williammah on March 25, 2014, 02:15:14 am
I swear you guys are the best. It worked! :D

Goodness me think of the game mechanics involving this!!!
Title: Re: Name-typing script
Post by: Radnen on March 25, 2014, 07:25:04 am

I swear you guys are the best. It worked! :D

Goodness me think of the game mechanics involving this!!!


It's actually a standard way of handling input in Sphere. ;) There are other use cases for the code FlyingJester showed you other than just text-typing. In Sphere catching AreKeysLeft() is usually the way to go about making buttons do things in games, like bringing up a menu.

Code: (javascript) [Select]

function Input() {
    while(AreKeysLeft()) {
        var key = GetKey();
        if (key == KEY_ENTER) show_menu();
        if (key == KEY_ESCAPE) Exit();
    }
}


And Input() here could go in some naive update-loop, whether you use SetUpdateScript or not.
Title: Re: Name-typing script
Post by: N E O on March 25, 2014, 09:14:44 am
If it's not already in the wiki article, keep in mind that while adding to the key queue is limited by the OS's typing rate settings, reading from it isn't.