Sign in to follow this  
Followers 0
atrX

Scripting Basics: Variable Scope

1 post in this topic

What is a variable scope?

The scope of a variable is essentially the part of your code in which this variable is accessible. If a variable is used outside of its scope an error will be thrown by the game: "Uninitialized variable VARNAME". Let's have a look at this basic example:

Please login or register to see this code.

 

Global or level variables

A global (also known as level) variable can be accessed in any function and in any gsc file from the moment it is initialised:

Please login or register to see this code.

 

Entity variables

You can also declare a variable for an entity, the scope for an entity variable is essentially the same as a level variable, except that it is only accessible by that specific entity it is assigned to:

Please login or register to see this code.

 

Scope of the for loop's declaration/initialisation

As seen in the tutorial about for loops, you can declare and initialize a variable in the first part of it. The scope of a variable initialized in a for loop's initialization ends from the moment the for loop ends:

Please login or register to see this code.

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0