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.

Hashes?

Hey guys, first off, the script:

Code:
#==============================================================================#
#  Script: Keyboard Input                                                      #
#------------------------------------------------------------------------------#
#                                                             Author: Zanifer  #
#                                                                Version: 1.0  #
#==============================================================================#
module Input
  Keyboard = {}
  Keyboard["Pause"] = 0x13
  module_function
 	def Triggered?(Keyboard.keys)
		Win32API.new("user32","GetAsyncKeyState",['I'],'I').call(Keyboard.keys)
	end
end
if Input.Triggered?('Pause')
	p 'Hello World!'
end


Ok, when I run it, for the life of me, I either screw up the hash itself, or can't recall the key cause it cant convert the string to an integer, in this case, 0x13. Furthermore, is it possibly an error with the I's in the Win32API?

Thanks
 
Okay.
First, you should create an instance or a variable for that API. So that you don't initialize it each frame. And why put Keyboard.keys in the method's argument, just a little word will do right. I put 'key' for my example. Also the API was correct but you can also do it as below, it's up to you.

Code:
#==============================================================================#
#  Script: Keyboard Input                                                      #
#------------------------------------------------------------------------------#
#                                                             Author: Zanifer  #
#                                                                Version: 1.0  #
#==============================================================================#
module Input
  GAKS = Win32API.new('user32', 'GetAsyncKeyState', 'I', 'I')
  Keyboard = {}
  Keyboard['Pause'] = 0x13
  module_function
  def triggered?(key)
    GAKS.call(key)
  end
end

if Input.triggered?('Pause')
  p 'Hello World!'
end
 
Ok, see, Ive tried it as such, and tried it again now. I rewrote some of it so now it looks like this:

Code:
#==============================================================================#
#  Script: Keyboard Input                                                      #
#------------------------------------------------------------------------------#
#                                                             Author: Zanifer  #
#                                                                Version: 1.0  #
#==============================================================================#
module Input
  GetAsyncKeyState = Win32API.new('user32', 'GetAsyncKeyState', 'i', 'i')
  Keyboard = {
  'Backspace' => 0x08,
  'Tab' => 0x09,
  'Enter' => 0x0D,
  'Shift' => 0x10,
  'Leftshift' => 0xA0,
  'Rightshift' => 0xA1,
  'Ctrl' => 0x11,
  'Leftctrl' => 0xA2,
  'Rightctrl' => 0xA3,
  'Alt' => 0x12,
  'Leftalt' => 0xA4,
  'Rightalt' => 0xA5,
  'Pause' => 0x13,
  'Capslock' => 0x14,
  'Esc' => 0x1B,
  'Spacebar' => 0x20,
  'Pageup' => 0x21,
  'Pagedown' => 0x22,
  'End' => 0x23,
  'Home' => 0x24,
  'Left' => 0x25,
  'Up' => 0x26,
  'Right' => 0x27,
  'Down' => 0x28,
  'PrtScr' => 0x2C,
  'Ins' => 0x2D,
  'Del' => 0x2E
  }
  module_function
  def Triggered?(key)
		GetAsyncKeyState.call(key)
	end
end
if Input.Triggered?('Pause')
  p 'Hello World!'
end

Yet, I still receive the syntax error "cannot convert String into Integer".

EDIT: BTW, I placed keyboard.keys into the argument thinking it would access the hash.keys and use the assigned value rather then treat it as a string. Would it just be easier to not use hashes and replace em with variables?
 
There are two ways to solve this problem.
1. Input.Triggered?('Pause') => Input.Triggered?(Input::Keyboard['Pause'])
2. GetAsyncKeyState.call(key) => GetAsyncKeyState.call(Keyboard[key])
 

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