Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Keyboard-Controlled Name Input Script

Status
Not open for further replies.
Introduction

There ya go, folks - my first released script... you may have noticed I'm not into releasing scripts from my game at all, but this one is an exeption because I've searched for a similar script long ago... well, in the end, I made it myself :P
Well, since you're able to get this script, too, you have to endure my painfully short introduction text XD


Purpose

Allows you to enter the heroes name by keyboard. Supported keys are a-z/A-Z, Shift, Space and Backspace to input the character's name, as well as two different modes (8-Character and 16-Character, automatica)
lly triggered) and two possible actor characterset sizes (triggerable via the @size variable inside Window_NameEdit).

Screenshots

http://img53.imageshack.us/img53/9718/nameinputxo6.th.jpg[/IMG]
8-Character Version


Script

Code:
#==============================================================================
# Keyboard Input Module
#----------------------------------------------------------------------------
# Script by Cybersam
#==============================================================================

module Input
  @keys = []
  @pressed = []
  Mouse_Left = 1
  Mouse_Right = 2
  Mouse_Middle = 4
  Back= 8
  Tab = 9
  Enter = 13
  Shift = 16
  Ctrl = 17
  Alt = 18
  Esc = 27
  Space = 32
  Numberkeys = {}
  Numberkeys[0] = 48        # => 0
  Numberkeys[1] = 49        # => 1
  Numberkeys[2] = 50        # => 2
  Numberkeys[3] = 51        # => 3
  Numberkeys[4] = 52        # => 4
  Numberkeys[5] = 53        # => 5
  Numberkeys[6] = 54        # => 6
  Numberkeys[7] = 55        # => 7
  Numberkeys[8] = 56        # => 8
  Numberkeys[9] = 57        # => 9
  Numberpad = {}
  Numberpad[0] = 45
  Numberpad[1] = 35
  Numberpad[2] = 40
  Numberpad[3] = 34
  Numberpad[4] = 37
  Numberpad[5] = 12
  Numberpad[6] = 39
  Numberpad[7] = 36
  Numberpad[8] = 38
  Numberpad[9] = 33
  Letters = {}
  Letters["A"] = 65
  Letters["B"] = 66
  Letters["C"] = 67
  Letters["D"] = 68
  Letters["E"] = 69
  Letters["F"] = 70
  Letters["G"] = 71
  Letters["H"] = 72
  Letters["I"] = 73
  Letters["J"] = 74
  Letters["K"] = 75
  Letters["L"] = 76
  Letters["M"] = 77
  Letters["N"] = 78
  Letters["O"] = 79
  Letters["P"] = 80
  Letters["Q"] = 81
  Letters["R"] = 82
  Letters["S"] = 83
  Letters["T"] = 84
  Letters["U"] = 85
  Letters["V"] = 86
  Letters["W"] = 87
  Letters["X"] = 88
  Letters["Y"] = 89
  Letters["Z"] = 90
  Fkeys = {}
  Fkeys[1] = 112
  Fkeys[2] = 113
  Fkeys[3] = 114
  Fkeys[4] = 115
  Fkeys[5] = 116
  Fkeys[6] = 117
  Fkeys[7] = 118
  Fkeys[8] = 119
  Fkeys[9] = 120
  Fkeys[10] = 121
  Fkeys[11] = 122
  Fkeys[12] = 123
  Collon = 186        # => \ |
  Equal = 187         # => = +
  Comma = 188         # => , <
  Underscore = 189    # => - _
  Dot = 190           # => . >
  Backslash = 191     # => / ?
  Lb = 219
  Rb = 221
  Quote = 222         # => '"
  #-------------------------------------------------------------------------------
  USED_KEYS = [Mouse_Left, Mouse_Right, Mouse_Middle] 
  #-------------------------------------------------------------------------------
  
module_function
  #--------------------------------------------------------------------------
  def triggered?(key)
    Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(key) & 0x01 == 1
  end
  #-------------------------------------------------------------------------- 
  def check(key)
    Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(key) & 0x01 == 1
  end
  #--------------------------------------------------------------------------
  def pressed?(key)
    return true unless Win32API.new("user32","GetKeyState",['i'],'i').call(key).between?(0, 1)
    return false
  end
  #--------------------------------------------------------------------------
  def mouse_update
    @used_i = []
    for i in USED_KEYS
      x = check(i)
      if x == true
        @used_i.push(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  def self.C
    self.trigger?(C)
  end
  #-------------------------------------------------------------------------- 
  def self.B
    self.trigger?(B)
  end
  #-------------------------------------------------------------------------- 
  def self.A
    self.trigger?(A)
  end
  #-------------------------------------------------------------------------- 
  def self.Down
    self.trigger?(DOWN)
  end
  #-------------------------------------------------------------------------- 
  def self.Up
    self.trigger?(UP)
  end
  #-------------------------------------------------------------------------- 
  def self.Right
    self.trigger?(RIGHT)
  end
  #-------------------------------------------------------------------------- 
  def self.Left
    self.trigger?(LEFT)
  end
  #--------------------------------------------------------------------------
  def self.Anykey
    if A or B or C or Down or Up or Right or Left
      return true
    else
      return false
    end
  end
  #--------------------------------------------------------------------------
end
Code:
#==============================================================================
# Name Input Script v1.4
#--------------------------------------------------------------------------
# Script by BlueScope
#==============================================================================


class Window_NameEdit < Window_Base
  #--------------------------------------------------------------------------
  attr_reader   :name
  attr_reader   :index
  #--------------------------------------------------------------------------
  def initialize(actor, max_char)
    super(0, 128, 640, 160)
    self.contents = Bitmap.new(width - 32, height - 32)
    @actor = actor
    @name = actor.name
    @max_char = max_char
    if @max_char < 9
      self.x = 128
      self.width = 384
    end
    name_array = @name.split(//)[0...@max_char]
    @name = ""
    for i in 0...name_array.size
      @name += name_array[i]
    end
    @default_name = @name
    @index = name_array.size
    @size = 0 # 0 for small sprites, 1 for bigger sprites
    refresh
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  def restore_default
    @name = @default_name
    @index = @name.split(//).size
    refresh
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  def add(character)
    if @index < @max_char and character != ""
      @name += character
      @index += 1
      refresh
      update_cursor_rect
    end
  end
  #--------------------------------------------------------------------------
  def back
    if @index > 0
      name_array = @name.split(//)
      @name = ""
      for i in 0...name_array.size-1
        @name += name_array[i]
      end
      @index -= 1
      refresh
      update_cursor_rect
    end
  end
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    name_array = @name.split(//)
    for i in 0...@max_char
      c = name_array[i]
      if c == nil
        c = "_"
      end
      x = 320 - 16 * 14 + i * 28
      self.contents.draw_text(x, 49, 28, 32, c, 1)
    end
    draw_actor_graphic(@actor, 56, @size == 0 ? 92 : 112)
  end
  #--------------------------------------------------------------------------
  def update_cursor_rect
    if @index < @max_char-1
      self.cursor_rect.set((@max_char > 8 ? -128 : -16) + @max_char * 14 + @index * 28, 48, 28, 32)
    else
      self.cursor_rect.set((@max_char > 8 ? -128 : -16) + @max_char * 14 + (@max_char-1) * 28, 48, 28, 32)
    end
  end
  #--------------------------------------------------------------------------
  def update
    super
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
end


class Window_NameHint < Window_Base
  #--------------------------------------------------------------------------
  def initialize
    super(128, 288, 384, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 0
    refresh
  end
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 0, self.width-40, 32, "Please enter Hero Name using the Keyboard", 1)
  end
  #--------------------------------------------------------------------------
end


class Scene_Name
  #--------------------------------------------------------------------------
  def main
    @actor = $game_actors[$game_temp.name_actor_id]
    @edit_window = Window_NameEdit.new(@actor, $game_temp.name_max_char)
    @hint_window = Window_NameHint.new
    @dummy_window = Window_Base.new(128, 288, 384, 64)
    if $game_temp.name_max_char > 8
      @dummy_window.x = 0
      @dummy_window.width = 640
    end
    Graphics.transition
    loop do
      Graphics.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @edit_window.dispose
    @hint_window.dispose
    @dummy_window.dispose
  end
  #--------------------------------------------------------------------------
  def change_name
    @actor.name = @edit_window.name
    $game_system.se_play($data_system.decision_se)
    $scene = Scene_Map.new
    if @edit_window.index == $game_temp.name_max_char
      $game_system.se_play($data_system.buzzer_se)
      return
    end
  end
  #--------------------------------------------------------------------------
  def input_processing
    if @edit_window.index == $game_temp.name_max_char
      $game_system.se_play($data_system.buzzer_se)
    else
      $game_system.se_play($data_system.cursor_se)
    end
  end
  #--------------------------------------------------------------------------
  def update
    @edit_window.update
    if Input.triggered?(27) # [ESCAPE]
      $game_system.se_play($data_system.decision_se)
      @edit_window.restore_default
      return
    end
    if Input.triggered?(8) # [BACKSPACE]
      if @edit_window.index == 0
        return
      end
      $game_system.se_play($data_system.cancel_se)
      @edit_window.back
      return
    end
    if Input.triggered?(13) # [ENTER]
      if @edit_window.name == ""
        @edit_window.restore_default
        return
      end
      change_name
    end
    if Input.triggered?(32) # [SPACE]
      input_processing
      @edit_window.add(" ")
    end
    for letter in 'A'..'Z'
      if Input.triggered?(letter[0])
        if Input.pressed?(16)
          input_processing
          @edit_window.add(letter.upcase)
        else
          input_processing
          @edit_window.add(letter.downcase)
        end
      end
    end
  end
  #--------------------------------------------------------------------------
end


Instructions

Insert the Keyboard Input Script above all other scripts (if you don't already have it; if you experience problems with the name input script, please try the version I posted above), insert the Name Input Script above main, below Scene_Name and Window_NameEdit. Both of them aren't needed anymore, Window_NameInput becomes redundant.

To call it, just use the default Enter Hero Name event command.

They're several ways to modify it... obviously, there are two versions of Window_NameEdit: a 8-character one and a 16-character one. It adjusts it's size automatically, depending on how many characters you choose to have in the event command. No bugs with this version using more than 8 characters, BTW.
Also, there's a variable called @size inside main definition of Window_NameEdit. This variable determines the y-level of the character sprite. If you use RTP-sized or similar characters, use 0, for everything bigger like RO's, HalfKaizers or such, use 1.
You can of course change the text inside Window_NameHint. Check line 106 of the Name Input Script for the draw_text command.


Bugs

None known


FAQs

Several, read the thread for Q&A...


Credits & Thanks

BlueScope (myself) for creating this... as you might've already guessed :D,
Cybersam for writing a fitting Keyboard Input Module,
Trickster for helping me out with the shortening of the input processing.
 
Instead of all those if statements that each have their own "end", why not use elsifs? For example:

Code:
if Input.triggered?(27) # [ESCAPE]
      $game_system.se_play($data_system.decision_se)
      @edit_window.restore_default
      return
    elsif Input.triggered?(8) # [BACKSPACE]
      if @edit_window.index == 0
        return
      end
      $game_system.se_play($data_system.cancel_se)
      @edit_window.back
      return
    elsif Input.triggered?(13) # [ENTER]
      if @edit_window.name == ""
        @edit_window.restore_default
        return
      end
      change_name
    elsif # Next input check

That way, instead of checking if a key is triggered 30 times per frame, it will only check the next one's if the first if statement is false. It should make it run better, I believe.
 
I know, but for the sake of writing the script, I made these statements... but yeah, changing this would increase the performance a bit... (which isn't needed at all hence the script isn't laggy or anything...). I just like to keep the overview :P
 
1) Condense, Condense, Condense
Code:
    if Input.triggered?(65)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("A")
      else
        @edit_window.add("a")
      end
    end
    if Input.triggered?(66)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("B")
      else
        @edit_window.add("b")
      end
    end
    if Input.triggered?(67)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("C")
      else
        @edit_window.add("c")
      end
    end
    if Input.triggered?(68)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("D")
      else
        @edit_window.add("d")
      end
    end
    if Input.triggered?(69)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("E")
      else
        @edit_window.add("e")
      end
    end
    if Input.triggered?(70)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("F")
      else
        @edit_window.add("f")
      end
    end
    if Input.triggered?(71)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("G")
      else
        @edit_window.add("g")
      end
    end
    if Input.triggered?(72)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("H")
      else
        @edit_window.add("h")
      end
    end
    if Input.triggered?(73)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("I")
      else
        @edit_window.add("i")
      end
    end
    if Input.triggered?(74)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("J")
      else
        @edit_window.add("j")
      end
    end
    if Input.triggered?(75)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("K")
      else
        @edit_window.add("k")
      end
    end
    if Input.triggered?(76)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("L")
      else
        @edit_window.add("l")
      end
    end
    if Input.triggered?(77)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("M")
      else
        @edit_window.add("m")
      end
    end
    if Input.triggered?(78)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("N")
      else
        @edit_window.add("n")
      end
    end
    if Input.triggered?(79)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("O")
      else
        @edit_window.add("o")
      end
    end
    if Input.triggered?(80)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("P")
      else
        @edit_window.add("p")
      end
    end
    if Input.triggered?(81)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("Q")
      else
        @edit_window.add("q")
      end
    end
    if Input.triggered?(82)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("R")
      else
        @edit_window.add("r")
      end
    end
    if Input.triggered?(83)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("S")
      else
        @edit_window.add("s")
      end
    end
    if Input.triggered?(84)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("T")
      else
        @edit_window.add("t")
      end
    end
    if Input.triggered?(85)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("U")
      else
        @edit_window.add("u")
      end
    end
    if Input.triggered?(86)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("V")
      else
        @edit_window.add("v")
      end
    end
    if Input.triggered?(87)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("W")
      else
        @edit_window.add("w")
      end
    end
    if Input.triggered?(88)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("X")
      else
        @edit_window.add("x")
      end
    end
    if Input.triggered?(89)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("Y")
      else
        @edit_window.add("y")
      end
    end
    if Input.triggered?(90)
      input_processing
      if Input.pressed?(16)
        @edit_window.add("Z")
      else
        @edit_window.add("z")
      end
    end
Code:
for letter in 'A'..'Z'
  if Input.triggered?(letter[0])

Ranges can also have strings for starting and ending points, the element reference method of class String when only sent a Integer parameter will return an Integer, the ASCII value/code for that character.

*) Note: Stings that are created with double quotes take a bit longer to processthan Strings that are created with single quotes, but sometimes they are needed (Expression subsitution and Backslash Notation), in this case, they aren't needed

But other than 1), well coded, BlueScope
 
I'm actually trying to use Cybersam's Input Module... and all I get is my project closing down... help :S i created the module at the very top of the Script list.

and on my map scene I put in a button trigger using

if Input.triggered?(9)

and my program closes as soon as i get past the title screen. I tried making a test script to test the button on the title screen.

if Input.triggered?(77)
print('M button has been pressed')
end

and again my project closed down and XP wants to send a error report to Microsoft... help me please lol
 
Trickster said:
But other than 1), well coded, BlueScope
Lol, the part you quoted is the only thing that's difficult about the script ^_^ Well, thank you though, a compliment from you (or Seph, equally :P ) is always a sign for being on the right way :D

For the shortening of the input processing, I thought there'd be a way to do this... I got you hint, but I have no idea how I'm supposed to get capitalized and non-capitalized characters... letter.capitalize won't work, I suppose?! XD
Oh, and I'm curious about what the [0] has to do in the conditional branch... Obviously, it converts the string to an integer (considering the error that appears when I try it without the [0]), but I have no idea how... could you please tell me?

@warhorse: It works for me, with your exact example... try it in a new project. If that doesn't work, you're maybe using an illegal version of RMXP...
 
Well, you'd know if you bought a legal copy, as you need to transfer money :P You can download the trial version at http://www.download.com, though, to get an impression of what you'll get.

But please back to topic now... Trickster still needs to help me out in herre :D
 
BlueScope said:
Lol, the part you quoted is the only thing that's difficult about the script ^_^ Well, thank you though, a compliment from you (or Seph, equally :P ) is always a sign for being on the right way :D

For the shortening of the input processing, I thought there'd be a way to do this... I got you hint, but I have no idea how I'm supposed to get capitalized and non-capitalized characters... letter.capitalize won't work, I suppose?! XD
Oh, and I'm curious about what the [0] has to do in the conditional branch... Obviously, it converts the string to an integer (considering the error that appears when I try it without the [0]), but I have no idea how... could you please tell me?

Well you could either use the upcase/downcase/swapcase method of the String to return the appropiate character or you could add 26 (I believe there where no characters in between upper and lower case) to the capital letters code.

Every string has a code, which is just the binary representation of that character. Try comparing 'A'.to_i and 'A'[0], and you will see that they return different numbers, the Rmxp Help file doesn't tell you of another use of the methods [], slice, and slice!. self[int], self.slice(int),self.slice!(int) which will return the characters code at index int, and basically the code is used for comparing two strings (ex lowercase a is greater than uppercase A)
 
Wow, it actually worked... now tell me how I should've guessed that by myself... -.- (I guess these things are things you won't learn by just looking at the default scripts, huh? ^_^ )

And I guess I got your explanation (I know that characters are represented by numbers, which is a must, hence you only have 0 and 1 to express anything ^_^ ), the problem is that I don't think I can use it in the future so easily... well, I'll try to do some scripts which use this feature and I'll see what I can get out...
 
BlueScope said:
Wow, it actually worked... now tell me how I should've guessed that by myself... -.- (I guess these things are things you won't learn by just looking at the default scripts, huh? ^_^ )

And I guess I got your explanation (I know that characters are represented by numbers, which is a must, hence you only have 0 and 1 to express anything ^_^ ), the problem is that I don't think I can use it in the future so easily... well, I'll try to do some scripts which use this feature and I'll see what I can get out...

Nope you can't learn all the syntax of ruby by looking at the default scripts, you would learn the RGSS built-in classes though, the help file doesn't even have all of the syntax of the built-in classes

Well I guess that's why It was left out of the help file, since there are very few times you acually need to use it (I don't even think I've used this once in any of my scripts)
 
...what makes it even more awesome that you know it... and if it's awesome that you know it, it's even more awesome that I do... so I'm awesome... Awesome! :D Okay, cut that out... thanks for your help, buddy :D
 
This is cool. You're now a small evolution away from keyboard input battle combo scripts(which would be the best thing ever)
 
@Wachunga: Yeah, I requested it and I wasn't able to find it, too, so I made it myself :P I guess it's easier for the everyday's game designer to use than a script that it written to be event-compatible... well, let's call it my script 'direct' and yours 'complex' :D
BTW, you should get your signature up-to-date ^_^

@ryanwh: You might post a request in the appropriate forum with a more specific description, and I might take it... well, if I get some time...
 
If i want to use more than 8 character must i change "if $game_temp.name_max_char > 8" into "if $game_temp.name_max_char > 16" am i right? or do i need to resize the window too?
[EDIT] or am i totally wrong as i'm realizing now?
 
You are, you define the window's width by the number of characters you choose to input inside the Event Command 'Enter Hero name. The script isn't functional with 16 characters ATM, for the reasons stated above. I could fix that, though, if you need it.
 

J-Crew

Member

Is it possible to use Cybersam's Module script on the map. Should I use a conditional branch stating if input.triggered?[key num] or is ther something lse I need to do. And I know this is a stupid question...but where do I put Cybersams Input Module.En fin, Nice script. It's a great improvment above the default name input that came with RPG Maker Xp.
 
@J: Read the instructions, I told where to place the keyboard input module.
For your other question, this doesn't belong here, but I'll answer it anyway... if you want to check for a specific key, check for this in the scripts line of the conditional event command:

Code:
Input.triggered?(aNumeric)

In general, you use the conditional branch script line just like a script editor conditional, just without the 'if' statement.
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top