Value of Variable

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

When i use this syntax :

export (int) var num

with code in my _process function

if Input.is_key_pressed(KEY_Z):
    num = 5

the value of num is change to 5 and its value is fixed at 5 even if key_z is no longer pressed

but when i use this syntax:

var num = 1

the value of num change to 5 when key_z is pressed, but when key_z is no longer pressed, its value change back to 1

Why?
I am using godot 3

You might want to provide more code, my guess is that in the second method you put the var num = 1 inside a function that gets called repeatedly. Try putting it in a global (script?) scope, outside of any functions.

RazorSh4rk | 2019-01-19 11:28

:bust_in_silhouette: Reply From: p7f

Hi,
This is because global and local scopes. Is common feature in most programming languages.

You have defined num at the begginning of the code with export (int) var num… it is defined “globally” and you can use it inside every function of your script. So, when you change its value with num = 5 you are changing the value of that global value, and it won’t change again unless you explicity change it with another assignation like num = 1.

However, in the second way, when you use var num = 5 You are NOT using the global variable, you are defining a new variable with the same name inside the local scope of the function _process (var is used to define variables). So when you do var num = 5 you get a new variable named num with value of 5, different from global variable num. Inside _process when you use num, you will be using local variable instead of global variable. But when you exit _processfunction, local variables are removed, and you only keep your global variable with its original value. So when you stop pressing the button, the next time you enter process you wont be difining the local variable and you will be accessing global variable.

I don’t know if i made myself clear, please tell me if you don’t understand.

If you are familiar with other programming languages, tell me and i can see if i find a similar case for a better explanation.

p7f | 2019-01-19 14:34

is it just for _process function or every function i defined?
I am kind of familiar with python

mr.brown | 2019-01-19 15:15

Every function has its local scope. If inside a function a local variable (i.e. defined inside the function) has the same name than a global variable (defined outside functions), the variable used will be the local one. But this only affects inside that function!

I python, every variable you use inside a function must be defined in local scope. If you want to use a global variable you must use global keyword.

For example

x=1
def foo():
    x=2 
    print(x)
foo()
print(x)

This will print 2 first for the print inside foo, and then 1 because outside function it will be using global variable.

x=1
def foo():
    global x
    x=2 
    print(x)
foo()
print(x)    

This should print 2 twice, as global variable is modified inside function.
The first code snippet i share here would be similar to the code where you uses var num=5, as you are defining the local variable. The second snipet is similar to the other case.

p7f | 2019-01-19 15:18

I edited previous comment to add python exampme

p7f | 2019-01-19 15:27

I understand now, thanks for answering

mr.brown | 2019-01-19 16:15

You are welcome! You may select the answer so others see it is solved.

p7f | 2019-01-19 16:17