variable changing after if statement.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Samsong1018

I am new to Godot so I hope I am not just missing something.
I have a variable assigned to a bool. I have an if statement checking wether or not it is false. I have it set true when my player is not on the ground. and false when it is on the ground. I have printed it through my script and it works fine. but after the if statement it is always set to false.

var my_var = false

if my_var == false:
do something here.

in my if statement it is always false. but never anywhere else. I check my variable in other locations and it always does this. I am on Mac with Godot vs. 3.2.1

the code you are providing is just fine. It surely is a logical flaw in your code. You have to show us more of your code to get any help.

klaas | 2020-09-03 20:05

Agreed. The problem is definitely in your code. Rather than trying to explain what you think your code is doing, just post it here so we can see it.

As Klass mentioned, there is nothing wrong with the sample you posted. I assume the variable is being set / reset elsewhere in your code, but we can’t determine that without the code.

jgodfrey | 2020-09-03 23:53

same problem bro.

Bagasito | 2022-11-25 23:31

:bust_in_silhouette: Reply From: jgodfrey

Without code, this is just a guess, but I bet…

That you’ve defined a global variable (so, outside of any function) named my_var. Then, you’re attempting to set and reference that global var in a function, but you’ve mistakenly defined a local variable with the same name by using var my_var = false in the function.

If that’s the case, the variable you set and check in the function is the local variable, while other references elsewhere in your script are the global var - which are two completely independent variables.

If that’s the problem, you can fix it by not defining the variable in the function. So, replace the var my_var = false with my_var = false.