As far as the code you posted goes, getting two different results is the expected behavior. Let me try to explain why (you'll have to translate the GDScript code into visual script if you want to use visual script):
Lets focus on just this code for now, as it is the cause of your problem.
if a == 0:
if Input.is_action_just_pressed("ui_accept"):
a = 1
if a == 1:
if Input.is_action_just_pressed("ui_accept"):
a = 0
Here is how the computer will process the above code:
line 1 | Check if variable a is equal to 0. If a equals 0, move to line 2, otherwise move to line 4
line 2 | Check if input with name ui_accept has just been pressed. If so, move to line 3, otherwise move to line 4
line 3 | Set variable a to a value of 1. Move to line 4
line 4 | Check if variable a is equal to 1. If a equals 1, move to line 5, otherwise move to line 7
line 5 | Check if input with name ui_accept has just been pressed. If so, move to line 6, otherwise move to line 7
line 6 | Set variable a to a value of 0. Move to line 7
line 7 | End of function. Await being called again, then start at line 1.
|
The problem here is you are setting a and then immediately checking for it in the very next if. Because the input key is still down (because we haven't finished this function call) it's going to go down that part of the program and is going to set a to zero again.
|
When you are using elif, you get this:
if a == 0:
if Input.is_action_just_pressed("ui_accept"):
a = 1
elif a == 1:
if Input.is_action_just_pressed("ui_accept"):
a = 0
And the computer processes it like this:
line 1 | Check if variable a is equal to 0. If a equals 0, move to line 2, otherwise move to line 4
line 2 | Check if input with name ui_accept has just been pressed. If so, move to line 3, otherwise move to line 7
line 3 | Set variable a to a value of 1. Move to line 7
line 4 | Because the previous if check did not succeed, we are going to check if variable a is equal to 1. If a equals 1, move to line 5, otherwise move to line 7
line 5 | Check if input with name ui_accept has just been pressed. If so, move to line 6, otherwise move to line 7
line 6 | Set variable a to a value of 0. Move to line 7
line 7 | End of function. Await being called again, then start at line 1.
|
This gives the desired result (a being set flipped from 0 to 1 when the button is pressed) because of how the computer reads the code. The major difference is by using elif instead of if, you are telling the computer to check the if condition first, and then check the elif condition. When you are using two if checks, it will check them both regardless of what happened with the first if check.
|
So, how do you fix this without using elif? In GDScript you could do something like this:
var set_variable = false
if a == 0 and set_variable == false:
if Input.is_action_just_pressed("ui_accept"):
a = 1
set_variable = true
if a == 1 and set_variable == false:
if Input.is_action_just_pressed("ui_accept"):
a = 0
set_variable = true
or you could do something like this:
if Input.is_action_just_pressed("ui_accept"):
if a == 0:
a = 1
else:
a = 0
As I said above, you'll have to translate the code into visual script, as I have no experience with visual script.
This may be a good reference on to what is happening. It is for Python, but GDScript is basically Python, and the concept of execution order when it comes to conditions is universal no matter the language you are using: Link