Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Valdred on July 31, 2010, 12:16:07 pm

Title: Button input delay
Post by: Valdred on July 31, 2010, 12:16:07 pm
So I'm making a battle system. There is two "modes" Battle-mode and non-battle-mode.
I use the Q key to switch between them. I'm having one problem. As the script checks for button input every frame, the player needs to be a ninja with his finger (really fast) in order to switch without making it instantly switching back. Any Idea's?
Title: Re: Button input delay
Post by: SBR* on July 31, 2010, 01:05:05 pm
Input.trigger?(Input::Q)


Use that in a conditional branch or in a script in a conditional expression, whatever you want :P.
Title: Re: Button input delay
Post by: Valdred on July 31, 2010, 01:18:14 pm
I'm sorry, I forgot to tell that I'm using DerVVulfmans Input module.
Title: Re: Button input delay
Post by: SBR* on July 31, 2010, 01:37:05 pm
Quote from: Darth u-just-got-pwned on July 31, 2010, 01:18:14 pm
I'm sorry, I forgot to tell that I'm using DerVVulfmans Input module.


Could you please post this script?
Title: Re: Button input delay
Post by: Valdred on July 31, 2010, 02:18:12 pm
of course

Spoiler: ShowHide
#================================================= =============================
# ** Keyboard Input Module (Revised)
#------------------------------------------------------------------------------
# by DerVVulfman
# version 1.2
# 08-15-2007
#------------------------------------------------------------------------------
# Based on...
# Keyboard Input Module version 3
# by Near Fantastica (06.07.05)
#================================================= =============================

module Input
#--------------------------------------------------------------------------
# * Get Current Keypress State (Regular presses)
#--------------------------------------------------------------------------
def Input.getstate(key)
return true unless Win32API.new("user32","GetKeyState",["i"],"i").call(key).between?(0, 1)
return false
end
#--------------------------------------------------------------------------
# * Get Continuous Keyboard State (CapsLock, NumLock, ScrollLock)
#--------------------------------------------------------------------------
def Input.keyboardstate(rkey, key = 0)
return false unless Win32API.new("user32","GetKeyState",['i'],'i').call(rkey) & 0x01 == key
return true
end
#--------------------------------------------------------------------------
# * Numberpad Input System
#--------------------------------------------------------------------------
def Input.get_direction
return "Center" if Input.getstate(12)
return "PgUp" if Input.getstate(33)
return "PgDn" if Input.getstate(34)
return "End" if Input.getstate(35)
return "Home" if Input.getstate(36)
return "Left" if Input.getstate(37)
return "Up" if Input.getstate(38)
return "Right" if Input.getstate(39)
return "Down" if Input.getstate(40)
return "Ins" if Input.getstate(45)
return "Del" if Input.getstate(46)
return "*" if Input.getstate(106)
return "+" if Input.getstate(107)
return "-" if Input.getstate(109)
return "/" if Input.getstate(111)
if Input.keyboardstate(144,1)
for key in 96..105 #ASCII KEYS 48(0) to 57(9)
if Input.getstate(key)
return (key-48).chr
end
end
end
return nil
end
#--------------------------------------------------------------------------
# * Function Keys (Tab / Enter / Ctrl / Etc)
#--------------------------------------------------------------------------
def Input.get_function
return "Back" if Input.getstate(8)
if Input.getstate(16)
return "BackTab" if Input.getstate(9)
else
return "Tab" if Input.getstate(9)
end
return "Enter" if Input.getstate(13)
return "Ctrl" if Input.getstate(17)
return "Alt" if Input.getstate(18)
return "Pause" if Input.getstate(19)
return "Esc" if Input.getstate(27)
return "PrntScr" if Input.getstate(44)
# Function Keys (F1 - F12)
for key in 112..123
if Input.getstate(key)
num = key - 111
return "F" + num.to_s
end
end
return nil
end
#--------------------------------------------------------------------------
# * Individual Keys (Left OR Right keys, and Windows & Application keys)
#--------------------------------------------------------------------------
def Input.get_individual
return "CapsLock" if Input.getstate(20)
return "NumLock" if Input.getstate(144)
return "ScrlLock" if Input.getstate(145)
return "Lf Win" if Input.getstate(91)
return "Rt Win" if Input.getstate(92)
return "App Key" if Input.getstate(93)
return "Lf Shift" if Input.getstate(160)
return "Rt Shift" if Input.getstate(161)
return "Lf Ctrl" if Input.getstate(162)
return "Rt Ctrl" if Input.getstate(163)
return "Lf Alt" if Input.getstate(164)
return "Rt Alt" if Input.getstate(165)
return nil
end
#--------------------------------------------------------------------------
# * Letter Keys (Just your regular alphabet, caps or otherwise)
#--------------------------------------------------------------------------
def Input.get_letters
for key in 65..90 #ASCII KEYS 65(a) to 90(z)
if Input.getstate(key)
# Ensure lower-case if NEITHER or BOTH the shift and caps are pressed
if (Input.getstate(16) and Input.keyboardstate(20,1) ) or
(Input.getstate(16) != true and Input.keyboardstate(20,1) != true)
return key.chr.downcase
else
# Otherwise, uppercase
return key.chr.upcase
end
end
end
return nil
end
#--------------------------------------------------------------------------
# * Number Keys (only the top keys)
#--------------------------------------------------------------------------
def Input.get_numbers
return nil if Input.getstate(16)
for key in 48..57 #ASCII KEYS 48(0) to 57(9)
if Input.getstate(key)
return key.chr
end
end
return nil
end
#--------------------------------------------------------------------------
# * Keys (All others)
#--------------------------------------------------------------------------
def Input.get_key
if Input.getstate(32)
return " "
end
# Ensure lower-case if NEITHER or BOTH the shift and caps are pressed
if (Input.getstate(16) and Input.keyboardstate(20,1) ) or
(Input.getstate(16) != true and Input.keyboardstate(20,1) != true)
return ";" if Input.getstate(186)
return "=" if Input.getstate(187)
return "," if Input.getstate(188)
return "-" if Input.getstate(189)
return "." if Input.getstate(190)
return "/" if Input.getstate(191)
return "`" if Input.getstate(192)
return "[" if Input.getstate(219)
return "\\" if Input.getstate(220)
return "]" if Input.getstate(221)
return "'" if Input.getstate(222)
# Otherwise, uppercase
else
return ":" if Input.getstate(186)
return "+" if Input.getstate(187)
return "<" if Input.getstate(188)
return "_" if Input.getstate(189)
return ">" if Input.getstate(190)
return "?" if Input.getstate(191)
return "~" if Input.getstate(192)
return "{" if Input.getstate(219)
return "|" if Input.getstate(220)
return "}" if Input.getstate(221)
return "\x22" if Input.getstate(222)
end
# Shifted Top-row Numbers (not in ASCII order)
for key in 48..57
if Input.getstate(key) and Input.getstate(16)
case key
when 48; return ")"
when 49; return "!"
when 50; return "@"
when 51; return "#"
when 52; return "$"
when 53; return "%"
when 54; return "^"
when 55; return "&"
when 56; return "*"
when 57; return "("
end
end
end
return nil
end
end
Title: Re: Button input delay
Post by: SBR* on July 31, 2010, 02:56:50 pm
Input.trigger?(Input::Q)


Should still work, but this should work too (did you mean the key Q, and not the RMXP-button? Or is there no RMXP-button Q? W/e):


if !@q_pressed
 if Input.get_letters == 'q' || Input.get_letters == 'Q'
   @q_pressed = true
   #DO STUFF
 end
elsif Input.get_letters != 'q' && Input.get_letters != 'Q'
 !@q_pressed
end


Remove the second part (|| Input.get_letters == 'Q'; || Input.get_letters != 'Q') if you don't want SHIFT to be pressed at the same time. Caps is used too, so NVM Don't forget to set '@q_pressed' to 'false' in the initialize method.

EDIT: Fixed.
Title: Re: Button input delay
Post by: G_G on July 31, 2010, 04:45:38 pm
I recommend using Blizzards Input module. install tons of add-ons and enable custom controls. Its most likely faster and better.
Input.trigger?(Input::Key['A'])
Title: Re: Button input delay
Post by: SBR* on August 01, 2010, 07:05:03 am
Quote from: game_guy on July 31, 2010, 04:45:38 pm
I recommend using Blizzards Input module. install tons of add-ons and enable custom controls. Its most likely faster and better.
Input.trigger?(Input::Key['A'])



I agree.
Title: Re: Button input delay
Post by: stripe103 on August 01, 2010, 11:55:00 am
I agree too
Easier than downloading the whole Tons of Addons: ShowHide
#==============================================================================
# module Input
#==============================================================================

module Input
 
  #----------------------------------------------------------------------------
  # Simple ASCII table
  #----------------------------------------------------------------------------
  Key = {'A' => 65, 'B' => 66, 'C' => 67, 'D' => 68, 'E' => 69, 'F' => 70,
         'G' => 71, 'H' => 72, 'I' => 73, 'J' => 74, 'K' => 75, 'L' => 76,
         'M' => 77, 'N' => 78, 'O' => 79, 'P' => 80, 'Q' => 81, 'R' => 82,
         'S' => 83, 'T' => 84, 'U' => 85, 'V' => 86, 'W' => 87, 'X' => 88,
         'Y' => 89, 'Z' => 90,
         '0' => 48, '1' => 49, '2' => 50, '3' => 51, '4' => 52, '5' => 53,
         '6' => 54, '7' => 55, '8' => 56, '9' => 57,
         '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,
         'F1' => 112, 'F2' => 113, 'F3' => 114, 'F4' => 115, 'F5' => 116,
         'F6' => 117, 'F7' => 118, 'F8' => 119, 'F9' => 120, 'F10' => 121,
         'F11' => 122, 'F12' => 123,
         ';' => 186, '=' => 187, ',' => 188, '-' => 189, '.' => 190, '/' => 220,
         '\\' => 191, '\'' => 222, '[' => 219, ']' => 221, '`' => 192,
         'Backspace' => 8, 'Tab' => 9, 'Enter' => 13, 'Shift' => 16,
         'Left Shift' => 160, 'Right Shift' => 161, 'Left Ctrl' => 162,
         'Right Ctrl' => 163, 'Left Alt' => 164, 'Right Alt' => 165,
         'Ctrl' => 17, 'Alt' => 18, 'Esc' => 27, 'Space' => 32, 'Page Up' => 33,
         'Page Down' => 34, 'End' => 35, 'Home' => 36, 'Insert' => 45,
         'Delete' => 46, 'Arrow Left' => 37, 'Arrow Up' => 38,
         'Arrow Right' => 39, 'Arrow Down' => 40,
         'Mouse Left' => 1, 'Mouse Right' => 2, 'Mouse Middle' => 4,
         'Mouse 4' => 5, 'Mouse 5' => 6}
  # default button configuration
  UP = [Key['Arrow Up']]
  LEFT = [Key['Arrow Left']]
  DOWN = [Key['Arrow Down']]
  RIGHT = [Key['Arrow Right']]
  A = [Key['Shift']]
  B = [Key['Esc'], Key['NumberPad 0'], Key['X']]
  C = [Key['Space'], Key['Enter'], Key['C']]
  X = [Key['A']]
  Y = [Key['S']]
  Z = [Key['D']]
  L = [Key['Q'], Key['Page Down']]
  R = [Key['W'], Key['Page Up']]
  F5 = [Key['F5']]
  F6 = [Key['F6']]
  F7 = [Key['F7']]
  F8 = [Key['F8']]
  F9 = [Key['F9']]
  SHIFT = [Key['Shift']]
  CTRL = [Key['Ctrl']]
  ALT = [Key['Alt']]
  # All keys
  ALL_KEYS = (0...256).to_a
  # Win32 API calls
  GetKeyboardState = Win32API.new('user32','GetKeyboardState', 'P', 'I')
  GetKeyboardLayout = Win32API.new('user32', 'GetKeyboardLayout','L', 'L')
  MapVirtualKeyEx = Win32API.new('user32', 'MapVirtualKeyEx', 'IIL', 'I')
  ToUnicodeEx = Win32API.new('user32', 'ToUnicodeEx', 'LLPPILL', 'L')
  # some other constants
  DOWN_STATE_MASK = 0x80
  DEAD_KEY_MASK = 0x80000000
  # data
  @state = "\0" * 256
  @triggered = Array.new(256, false)
  @pressed = Array.new(256, false)
  @released = Array.new(256, false)
  @repeated = Array.new(256, 0)
  #----------------------------------------------------------------------------
  # update
  #  Updates input.
  #----------------------------------------------------------------------------
  def self.update
    # get current language layout
    @language_layout = GetKeyboardLayout.call(0)
    # get new keyboard state
    GetKeyboardState.call(@state)
    # for each key
    ALL_KEYS.each {|key|
        # if pressed state
        if @state[key] & DOWN_STATE_MASK == DOWN_STATE_MASK
          # not released anymore
          @released[key] = false
          # if not pressed yet
          if !@pressed[key]
            # pressed and triggered
            @pressed[key] = true
            @triggered[key] = true
          else
            # not triggered anymore
            @triggered[key] = false
          end
          # update of repeat counter
          @repeated[key] < 17 ? @repeated[key] += 1 : @repeated[key] = 15
        # not released yet
        elsif !@released[key]
          # if still pressed
          if @pressed[key]
            # not triggered, pressed or repeated, but released
            @triggered[key] = false
            @pressed[key] = false
            @repeated[key] = 0
            @released[key] = true
          end
        else
          # not released anymore
          @released[key] = false
        end}
  end
  #----------------------------------------------------------------------------
  # dir4
  #  4 direction check.
  #----------------------------------------------------------------------------
  def Input.dir4
    return 2 if Input.press?(DOWN)
    return 4 if Input.press?(LEFT)
    return 6 if Input.press?(RIGHT)
    return 8 if Input.press?(UP)
    return 0
  end
  #----------------------------------------------------------------------------
  # dir8
  #  8 direction check.
  #----------------------------------------------------------------------------
  def Input.dir8
    down = Input.press?(DOWN)
    left = Input.press?(LEFT)
    return 1 if down && left
    right = Input.press?(RIGHT)
    return 3 if down && right
    up = Input.press?(UP)
    return 7 if up && left
    return 9 if up && right
    return 2 if down
    return 4 if left
    return 6 if right
    return 8 if up
    return 0
  end
  #----------------------------------------------------------------------------
  # trigger?
  #  Test if key was triggered once.
  #----------------------------------------------------------------------------
  def Input.trigger?(keys)
    keys = [keys] unless keys.is_a?(Array)
    return keys.any? {|key| @triggered[key]}
  end
  #----------------------------------------------------------------------------
  # press?
  #  Test if key is being pressed.
  #----------------------------------------------------------------------------
  def Input.press?(keys)
    keys = [keys] unless keys.is_a?(Array)
    return keys.any? {|key| @pressed[key]}
  end
  #----------------------------------------------------------------------------
  # repeat?
  #  Test if key is being pressed for repeating.
  #----------------------------------------------------------------------------
  def Input.repeat?(keys)
    keys = [keys] unless keys.is_a?(Array)
    return keys.any? {|key| @repeated[key] == 1 || @repeated[key] == 16}
  end
  #----------------------------------------------------------------------------
  # release?
  #  Test if key was released.
  #----------------------------------------------------------------------------
  def Input.release?(keys)
    keys = [keys] unless keys.is_a?(Array)
    return keys.any? {|key| @released[key]}
  end
  #----------------------------------------------------------------------------
  # get_character
  #  vk - virtual key
  #  Gets the character from keyboard input using the input locale identifier
  #  (formerly called keyboard layout handles).
  #----------------------------------------------------------------------------
  def self.get_character(vk)
    # get corresponding character from virtual key
    c = MapVirtualKeyEx.call(vk, 2, @language_layout)
    # stop if character is non-printable and not a dead key
    return '' if c < 32 && (c & DEAD_KEY_MASK != DEAD_KEY_MASK)
    # get scan code
    vsc = MapVirtualKeyEx.call(vk, 0, @language_layout)
    # result string is never longer than 2 bytes (Unicode)
    result = "\0" * 2
    # get input string from Win32 API
    length = ToUnicodeEx.call(vk, vsc, @state, result, 2, 0, @language_layout)
    return (length == 0 ? '' : result)
  end
  #----------------------------------------------------------------------------
  # get_input_string
  #  Gets the string that was entered using the keyboard over the input locale
  #  identifier (formerly called keyboard layout handles).
  #----------------------------------------------------------------------------
  def self.get_input_string
    result = ''
    # check every key
    ALL_KEYS.each {|key|
        # if repeated
        if self.repeat?(key)
          # get character from keyboard state
          c = self.get_character(key)
          # add character if there is a character
          result += c if c != ''
        end}
    # empty if result is empty
    return '' if result == ''
    # convert string from Unicode to UTF-8
    return self.unicode_to_utf8(result)
  end
  #----------------------------------------------------------------------------
  # get_input_string
  #  string - string in Unicode format
  #  Converts a string from Unicode format to UTF-8 format as RGSS does not
  #  support Unicode.
  #----------------------------------------------------------------------------
  def self.unicode_to_utf8(string)
    result = ''
    string.unpack('S*').each {|c|
        # characters under 0x80 are 1 byte characters
        if c < 0x0080
          result += c.chr
        # other characters under 0x800 are 2 byte characters
        elsif c < 0x0800
          result += (0xC0 | (c >> 6)).chr
          result += (0x80 | (c & 0x3F)).chr
        # the rest are 3 byte characters
        else
          result += (0xE0 | (c >> 12)).chr
          result += (0x80 | ((c >> 12) & 0x3F)).chr
          result += (0x80 | (c & 0x3F)).chr
        end}
    return result
  end
 
end
Title: Re: Button input delay
Post by: Blizzard on August 01, 2010, 12:49:44 pm
D:

Quote from: Darth Naughty on January 09, 2008, 08:50:47 am
Do NOT remove any of the parts, the script NEEDS all 3 parts to work with other systems.
Title: Re: Button input delay
Post by: SBR* on August 01, 2010, 01:01:24 pm
Quote from: Darth Naughty on August 01, 2010, 12:49:44 pm
D:

Quote from: Darth Naughty on January 09, 2008, 08:50:47 am
Do NOT remove any of the parts, the script NEEDS all 3 parts to work with other systems.



I'm wondering: why?
Title: Re: Button input delay
Post by: Blizzard on August 01, 2010, 01:06:05 pm
e.g. If you cut out HP/SP Absorb from Tons and used it with Blizz-ABS, it won't work. Blizz-ABS doesn't check for the HP/SP Absorb script but first for Tons and then for the HP/SP Absorb script. If you added the variable for Tons' version without adding all the other necessary scripts, your game would crash due to Blizz-ABS checking for the other scripts from Tons.

EDIT: The Custom Controls alone might work fine as long there is no Blizz-ABS and no RMX-OS.
Title: Re: Button input delay
Post by: Valdred on August 01, 2010, 01:29:57 pm
I'm not using rmxos or blizz abs.
Title: Re: Button input delay
Post by: Blizzard on August 01, 2010, 02:00:46 pm
I'm not saying it will work with everything else like that either.
Title: Re: Button input delay
Post by: WhiteRose on August 01, 2010, 02:13:12 pm
For maximum compatibility, I'd recommend just using the whole Tons script whenever you think you might use a part of it rather than just taking out that part and using that, as, even if your method does turn out to work, it might cause issues later down the road. Copying the whole script won't slow your game down or anything, and it's only twenty extra seconds of work on your part.
Title: Re: Button input delay
Post by: Blizzard on August 01, 2010, 02:27:24 pm
Quote from: Dartha Stewart on August 01, 2010, 02:13:12 pm
Copying the whole script won't slow your game down or anything


I made sure of that. ;)
Title: Re: Button input delay
Post by: stripe103 on August 01, 2010, 02:53:58 pm
I'm sorry.  :^_^': I didn't take it from Tons of Addons, it took it from RMX-OS. And there are also other scripts that use it so I guess it work alone, but I'm not sure and you are the creator so I guess you know better. It has always worked for me though.
Title: Re: Button input delay
Post by: Valdred on August 01, 2010, 03:39:00 pm
I think I'll rather spend time making sure it works on it's own. :)
Title: Re: Button input delay
Post by: Blizzard on August 01, 2010, 05:10:31 pm
Regardless of the fact that it already works with everything else? You're being counterproductive.
Title: Re: Button input delay
Post by: WhiteRose on August 01, 2010, 07:28:54 pm
Quote from: Darth u-just-got-pwned on August 01, 2010, 03:39:00 pm
I think I'll rather spend time making sure it works on it's own. :)

Whatever floats your boat.
Title: Re: Button input delay
Post by: Valdred on August 02, 2010, 10:37:26 am
Quote from: Blizzard on August 01, 2010, 02:00:46 pm
I'm not saying it will work with everything else like that either.


Is it just my mediocre english? Doesn't this mean that you don't know if it will work with everything else?
Title: Re: Button input delay
Post by: Blizzard on August 02, 2010, 11:12:37 am
Yes, that's what I said. It might, it might not. I can't possibly know. At least using it in Tons increases the chances that it will because Tons has been tested with many other scripts.