Local and Global Variables

From Frictional Wiki
Jump to navigation Jump to search


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