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

From Frictional Wiki
Jump to navigation Jump to search
Line 22: Line 22:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
Local variables can be on the class-scope as well, for example:<syntaxhighlight lang="c++">
 +
class cScrMap : iScrMap
 +
{
 +
tString msLocalVariable = "Value";
 +
 +
void foo()
 +
{
 +
int lFunctionVariable = 5;
 +
cLux_AddDebugMessage(msLocalVariable + lFunctionVariable);
 +
}
 +
</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.
+
Global variables are defined outside of all the functions, usually on top of the script file. The global variables will hold their value throughout the life-time of your script.
  
A function's scope consists of everything within its braces <code>{ }</code>.
+
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables −<syntaxhighlight lang="c++">
Normal variables as shown above will only work in a function unless carried over to another function.
+
const tString mlMyGlobalStringVariable = "Value";
  
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.
+
class cScrMap : iScrMap
 +
{
 +
  // The rest of the script file...
 +
</syntaxhighlight>
  
 
===Cross-Script Global Variables===
 
===Cross-Script Global Variables===

Revision as of 17:48, 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);
}

Local variables can be on the class-scope as well, for example:

class cScrMap : iScrMap
{
	tString msLocalVariable = "Value";
	
	void foo()
	{
		int lFunctionVariable = 5;
		cLux_AddDebugMessage(msLocalVariable + lFunctionVariable);
	}

Global Variables

Global variables are defined outside of all the functions, usually on top of the script file. The global variables will hold their value throughout the life-time of your script.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables −

const tString mlMyGlobalStringVariable = "Value";

class cScrMap : iScrMap
{
  // The rest of the script file...

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