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

From Frictional Wiki
Jump to navigation Jump to search
 
Line 54: Line 54:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{{note|A variable which is declared outside a class scope must be a <code>const</code> variable!}}
+
{{note|A variable which is declared outside a class scope must be a <code>[[HPL3/Scripting/AngelScript_Fundamentals/Chapter_9_-_Miscellaneous_AngelScript_Features#Constants|const]]</code> variable!}}
  
 
===Cross-Script Global Variables===
 
===Cross-Script Global Variables===

Latest revision as of 11:55, 16 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"; // Declaring and Initializing a variable inside this function
	cLux_AddDebugMessage(msLocalVariable); // This will work
}

void foo2()
{
	cLux_AddDebugMessage(msLocalVariable); // This will result an error because the variable cannot be used, since it's created inside the scope of foo.
}

Local variables can be on the class-scope as well, and so they could be used in multiple functions, for example:

class cScrMap : iScrMap
{
	tString msLocalVariable = "Value";
	
	void foo()
	{
		cLux_AddDebugMessage(msLocalVariable); // This will work
	}

	void foo2()
	{
		cLux_AddDebugMessage(msLocalVariable + "Hello"); // This will work as well
	}

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 script after its declaration. The following is the example using global variable:

const tString mlMyGlobalStringVariable = "Value";

class cScrMap : iScrMap
{
  // The rest of the script file...
Note icon.png A variable which is declared outside a class scope must be a const variable!

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.

This is achieved using the two different functions: cScript_SetGlobalVar and cScript_GetGlobalVar. For example:

FirstMap.hps

void PickUpChip()
{
	cScript_SetGlobalVarBool("IsChipPickedUp", true);
}

SecondMap.hps

void DoSomething()
{
	if (cScript_GetGlobalVarBool("IsChipPickedUp")) 
	{
		// Do sometething 
	}
}

In the first map script file, we create a global bool variable and set the value to true. In the second map script file, we check if the value of that global variable is true and execute the code inside.


Enums