Hello
As part of my self-taught learning I would like to understand where I am wrong in my reasoning.
Here is the code (I know this code is not the best for this operation, the tutorial presents a better way after).
func _input(event : InputEvent) -> void:
var h_movement : float = 0.0
var v_movement : float = 0.0
if event.is_action_pressed("ui_left"):
h_movement -= 300
if event.is_action_pressed("ui_right"):
h_movement += 300
if event.is_action_pressed("ui_up"):
v_movement -= 300
if event.is_action_pressed("ui_down"):
v_movement += 300
move_and_slide(Vector2(h_movement, v_movement))
print(Vector2(h_movement, v_movement))
return
So when I launch the game, in the debug window, when I go to the right, it displays (300, 0) and after I keep pressing or pressing the key once I get (0 , 0).
The explanation given by the tutorial is that the variables are declared locally and initialized to 0. But this here that I do not understand, indeed it is initialized to 0 but so what? During the first press to the right, the condition is executed so my position has been shifted by 300 units, I return to 0 (because according to the debug window, I am automatically considered as positioned at Vector2(0, 0) ) from my position but if I stay supported I should still shift by 300 units according to the given condition no?
Thanks in advance