Keyhit command issue?

Blitz3D Forums/Blitz3D Programming/Keyhit command issue?

Cubed Inc.(Posted 2013) [#1]
It seems that if I used the keyhit command with one specific key in one part of the game's code, any later utilization of the keyhit command of the same key value is cancelled out.

For example, if I code it so that the up key can enter doors and talk to NPCs, it'll only work for opening doors and not talking to NPCs because the door snippet came earlier.

Is there any reason for this anomaly?

This issue's actually very vital, because if it turns out that Blitz3d can't handle something as simple as sharing button commands, than my project's going to have to be cancelled.

I'm praying that there's some sort of silver lining to all this. I've put my blood, sweat and tears into my game, and I'll be devastated if it all goes to waste due to my ignorance -_-


Stevie G(Posted 2013) [#2]
Never been an issue for me. Gather all inputs at the start of the main game loop and store the results rather than multiple calls to keyhit.

e.g.

Action = Keyhit(ACTION_KEY_CODE)
.
If Action OPEN_DOOR
If Action TALK_TO_NPC


RemiD(Posted 2013) [#3]
Do what Stevie explained.

Instead of using If(Keyhit(Id)>0) several times in your code, check the state of each key at the beginning of the loop, store it in a variable, and then use the variable to do your others checks.
This is especially necessary to do that when you are coding a GUI and you need to disable double mouse click.

for example :
before the mainloop :
CIdle% = 1
CHit% = 2
CDown% = 3

at the beginning of the loop :
KeyActionState% = CIdle
If(KeyHit(Id)>0)
KeyActionState = CHit
Endif

then later in the code :
If(KeyActionState = CHit)
;Do something
Endif

then later in the code :
If(KeyActionState = CHit)
;Do something else
Endif

But for what you explain, i don't really understand why you need it, i think that in most cases there must be only one instruction executed per loop for each key hit or down, else it may become really confusing.


I'm praying that there's some sort of silver lining to all this.


or you could stop praying and start using the commands :
Debuglog("Keyhit(Id) = "+KeyHit(Id))
Flushkeys()
Waitkey()
With this, you will see exactly how your program behaves.


Matty(Posted 2013) [#4]
You are simply using the keyhit command wrong.

KeyHit returns the number of times a key has been pressed since last time the function was called.

If you call "KeyHit(letter A)" for example then a few lines down call it again then it will return 'x' the first time and '0' the second time...

this is normal...

As Stevie G says - store the value of KeyHit() the first time it is called and compare with the value.


Cubed Inc.(Posted 2013) [#5]
Thanks! You learn something new everyday!


Rroff(Posted 2013) [#6]
I virtualise (so to speak) all my buttons using a type - this is also useful in that you can assign more than one key to the same action and easily remap the bindings from a config file. Its fairly easy to extend it to handle multiple key combinations also i.e. actions triggered by pressing ctrl+f1 or something.

Type key
	Field key1 ; key code for main binding
	Field key2 ; key code for alt binding
	Field mouse ; optional code for mouse binding
	
	Field active
	Field hit
	Field hitState
End Type

Global IN_ACTION_JUMP.key = new key


I then clear them each frame and update with the keyhit/downs - anywhere in the code where I'd do for instance a jump action I'd then check IN_ACTION_JUMP rather than a specific key and you can just loop through each type instance to update the state of key actions without having to know the details of what key is bound to what.


jfk EO-11110(Posted 2013) [#7]
the same goes for for mousehit. contrary to keydown and mousedown, BTW.