We need to talk about the scope of variables.
The scope of variables is where a variable can be used. Let’s say you have something like ‘Health’ or something specifically related to an object. It’s best to use an ‘Instance variable’. But if you have a global variable assuming a day and night cycle, or something like that, you can ‘Add global variable’
Let’s right-click ‘Add global variable’ before we have billions of numbers in strings.
It is a good example of a global variable.
We will add a sub-condition here.
We can add a global variable called ‘TempVariable’
I can use the ‘TempVariable’ within this area here.
I can’t use it outside. I can use this ‘HourOfDay’ no matter where. In any ‘Event sheets’ whether it ‘Rotate Player’ or ‘Main Event Sheet’
I can always access the ‘ Glocal number HourOfDay’ here.
This ‘TempVariable’ I cannot use externally.
When you make a variable specific to an object in the game, it’s better to make it an instance variable. If anything is supposed to be used throughout the game, it should be a global variable. If you’re making a simple game, you can get away with using global variables.
The reason is assuming you have only one player in the game that you could add.
You can add ‘Health’ to the global variable here.
In theory, you must add and subtract all those variables through the ‘System’
I like using the system variables.
We can add to a variable or subtract other variables. You can type ‘Lives’ in there. Let’s add ‘Boolean’ here with these global variables. You should capitalize each letter or each word. If the ‘GameOver’ is ‘False’, we can make it ‘True’
Now there’s ‘Toggle boolean’ and ‘Set boolean’
‘GameOver’ to ‘True’ or ‘False’ and I can set ‘Toggle boolean’
For example, if you have a game and we will set it to boolean here, and the initial value of ‘GravityDown’ is ‘True’
If you want to switch it every time you click or touch the screen or switch the gravity. You will ‘Toggle boolean’
You can ‘Set value’ it certainly has no variables.
You can also set the value to another reason. I like using global variables instead of local variables. If you have to reset the game, the system says ‘Reset global variables’
It’s very secure and easy. Take a look at ‘Instance Variables’
But note that none of the “Instance Variables” are reset. You have to do it manually. That’s a quick introduction to variables.