Difference between revisions of "HPL3/Scripting/Scripting Guide/Local and Global Variables"

From Frictional Wiki
Jump to navigation Jump to search
Line 4: Line 4:
 
Local and global variables are variables that have a certain scope compared to the script in which it is located in.
 
Local and global variables are variables that have a certain scope compared to the script in which it is located in.
  
== Variables Scopes ==
+
==Variables Scopes==
 
A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
 
A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
* Inside a function or a block which is called local variables.
+
 
* In the definition of function parameters which is called formal parameters.
+
*Inside a function or a block which is called local variables.
* Outside of all functions which is called global variables.
+
*In the definition of function parameters which is called formal parameters.
 +
*Outside of all functions which is called global variables.
  
 
In HPL3, there are two main variable scopes: Local Variables and Global Variables.
 
In HPL3, there are two main variable scopes: Local Variables and Global Variables.
  
== Local Variables ==
+
==Local Variables==
Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables that are declared inside functions are not known to functions outside their own. The following is the example using local variables:
+
Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables that are declared inside functions are not known to functions outside their own. The following is the example using local variables:<syntaxhighlight lang="c++">
 +
void foo()
 +
{
 +
tString msLocalVariable = "Value";
 +
cLux_AddDebugMessage(msLocalVariable);
 +
}
 +
</syntaxhighlight>
  
  
== Global Variables ==
+
==Global Variables==
 
There are certain levels of scope to a script, there is the function's scope, then there is the local scope, then there is the global scope.
 
There are certain levels of scope to a script, there is the function's scope, then there is the local scope, then there is the global scope.
  

Revision as of 18:44, 14 August 2020


Local and global variables are variables that have a certain scope compared to the script in which it is located in.

Variables Scopes

A scope is a region of the program and broadly speaking there are three places, where variables can be declared:

  • Inside a function or a block which is called local variables.
  • In the definition of function parameters which is called formal parameters.
  • Outside of all functions which is called global variables.

In HPL3, there are two main variable scopes: Local Variables and Global Variables.

Local Variables

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables that are declared inside functions are not known to functions outside their own. The following is the example using local variables:

void foo()
{
	tString msLocalVariable = "Value";
	cLux_AddDebugMessage(msLocalVariable);
}


Global Variables

There are certain levels of scope to a script, there is the function's scope, then there is the local scope, then there is the global scope.

A function's scope consists of everything within its braces { }. Normal variables as shown above will only work in a function unless carried over to another function.

A local variable's scope is the whole entire script, so if you made a local variable, then you wouldn't have to worry about having to carry it over from function to function. The downside to it is that you can't use the value of the local variable in commands.

Cross-Script Global Variables

Cross-Script global variables are variables which can be used across multiple script files. They are usually used to check if the player has done specific things within the map to make something happen in another map.


Enums