Variables: Local and Global
Sometimes it is necessary to initialize a variable and use it somewhere else.
A typical application is to define a certain value at the beginning of a simulation and reuse it every frame. To do this, a certain notation is required.
The main aspect with local and global variables is RAM management. Local variables will be "forgotten" after the execution of a script, global variables will be stored permanently.
Local Variables
Let's start with local variables. Let's further assume you have created a "FramesPre" script and there you add a new vector:
FramesPre: nullVelocity = Vector.new(0.0, 0.0, 0.0)
This standard notation defines a local variable. In other words: the variable can only be used within the "FramesPre" script.
If you want to reuse nullVelocity
in another script. e.g. in "SimulationPost" you have to define it again in exactly the same way:
SimulationPost:
nullVelocity = Vector.new(0.0, 0.0, 0.0)
Global Variables
Now we want to make nullVelocity
global:
FramesPre: scene.setGlobalVariableValue("nullVelocity", Vector.new(0.0, 0.0, 0.0))
"nullVelocity"
is the global variables name and a string. Therefore it has to be enclosed in quotes.Vector.new(0.0, .0.0, 0.0)
is the global variables value – here it is a vector.
FramesPost: nullVelocity = scene.getGlobalVariableValue("nullVelocity")
- It is no problem to reuse the variables name here.
- The global variable's value is assigned to the new local variable.