Unable to convert from 'String' to 'Int' ???

BlitzMax Forums/BlitzMax Beginners Area/Unable to convert from 'String' to 'Int' ???

RmB303(Posted 2009) [#1]
Hi.
I'm just starting with object oriented programming, and have hit a brick wall.

I'm converting my procedural Pong game into an OO one.

Everything was working fine until I tried to put the key controlled movement into my TBat type.
There are two instances of this type - one for each player: batL and batR

the method is (and appropriate fields): -
Field x:Int, y:Int
Field up_button:String
Field down_button:String
	Method move()
		If KeyDown(self.up_button) Then Self.y:-5
		If KeyDown(self.down_button) Then Self.y:+5
	End Method


The 'left' player instance then has its keys defined by: -
batL.up_button=KEY_A; batL.down_button=KEY_Z


Obviously this isn't working.

Is it possible to send a string to a method to be used as a key_code, or do I have to read the key codes from within the main loop, and pass a 'result' to the movement method?

I've tried putting the string in quotes, but that doesn't work.
Do I need to somehow convert a key_code into an INT or something?
Do type methods only accept numeric values?
Please help.

p.s. I'm using 'Strict' as it helps me to catch bugs a lot easier - sometimes ;-)


dan_upright(Posted 2009) [#2]
Do I need to somehow convert a key_code into an INT or something?
each key on the keyboard is actually referred to by a number, not a letter - KEY_A and KEY_Z (and the rest) are constants which max provides so you have a more human readable way of referring to each key

in order to fix your problem, change up_button and down_button to ints, rather than strings =]


RmB303(Posted 2009) [#3]
Thanks a lot, everything works fine now. (Doesn't mean my game is actually any good though)

Seems obvious 'now' that the key_codes are just constants.
Thanks a lot :-)

I can't believe how much tidier and more manageable the code is using types and methods instead of old-school procedural code.
(Slowly dragging myself into the 21st century)