Skip to main content

News

Topic: Name-typing script (Read 5177 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Name-typing script
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.

  • Rahkiin
  • [*][*][*]
Re: Name-typing script
Reply #1
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

Re: Name-typing script
Reply #2
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.

Re: Name-typing script
Reply #3
I swear you guys are the best. It worked! :D

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

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Name-typing script
Reply #4

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.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Name-typing script
Reply #5
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.