This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 keyz is pressed, but when keyz is no longer pressed, its value change back to 1

Why?
I am using godot 3

in Engine by (12 points)

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.

1 Answer

+1 vote

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.

by (3,505 points)

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

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

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.

I edited previous comment to add python exampme

I understand now, thanks for answering

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.