why is the if elese statement and variable not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rbcodder
if Input.is_action_just_pressed("g") and out == false:
	out = true
elif Input.is_action_just_pressed("g") and out == true:
	out = false

its an fps game, i take the mag out by pressing g, if its out the variable is true. the animationplayer just plays the animation of taking it out, if i press it again, it plays the same thing again, but i want it to play it backwards. the out variable prints out true every time i press it.

If “in” is never printed then the variable ‘out’ must be changing from true back to false at some point in code not shown.

LeslieS | 2022-12-27 18:32

No it isnt, its only this code that works on the mag

rbcodder | 2022-12-27 19:01

Is the out variable’s value being set somewhere outside of the posted code?

jgodfrey | 2022-12-27 19:05

Oh yes its var out = false just this

rbcodder | 2022-12-27 19:08

If you’re setting the value to false somewhere above the posted code (in the same function), that’s likely the problem…

jgodfrey | 2022-12-27 19:22


rbcodder | 2022-12-27 20:59

:bust_in_silhouette: Reply From: Gluon

Well first I would say that your speed setting on the backwards is wrong, it should be a negative number. On the method play_backwards If custom_speed is negative and from_end is true, the animation will play backwards.

Even so though I think it seems clear that somewhere else in your code you must be setting out = false. The above code looks fine so that seems like the only possible option to me, try adding some break points to your code and testing this or add a series of print statements throughout your code with print("description of where in code " && out) and see if you can find out where that is happening.

Thank you, i will try it

rbcodder | 2022-12-27 19:09