Program isn't updating a variable that changes?

Blitz3D Forums/Blitz3D Beginners Area/Program isn't updating a variable that changes?

Liimbix(Posted 2015) [#1]
Hi all, I know I've been posting a lot and I hope I'm not annoying, but I think I'm having a problem again. I have this code:

If KeyHit(35)=True And helm=1 Then
helm=0
EndIf

If KeyHit(35)=True And helm=0 Then
helm=1
EndIf

If helm=1 Then
ShowEntity helmet
Else
HideEntity helmet
EndIf 



in my main "while" loop. But it only works once. The game starts with the helmet on, but then if you press "H", it goes off, like it should. But if you press "H" again to put it on, nothing happens.. I put this code before my Update world / Render world, and after it, but it doesn't seem like it's registering the variable change. Help? Thanks (:

EDIT: I just put some debug text on the screen to track the variable "helm" and once you press "H", and it switches to 0, then if you press it again, it doesnt go back to 1. So the show/hide entity isnt the problem. It's 'helm" updating (or lack thereof)


Matty(Posted 2015) [#2]
You are using keyhit incorrectly.

Key hit counts the number of times the key has been pressed since the last time the function was called.

Therefore the value will always be zero for keyhit in your second if statement.

Instead store the value of keyhit in a variable once per loop and check the variable instead.


Stevie G(Posted 2015) [#3]
As above. You could also just simplfy the code to the following ...

If Keyhit(35)
  Helm = 1 - Helm
  If Helm = 1
    Showentity Helmet
  else
    Hideentity Helmet
Endif



Liimbix(Posted 2015) [#4]
Thanks Stevie G and Matty, both were insightful :)