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

From Frictional Wiki
Jump to navigation Jump to search
Line 3: Line 3:
  
 
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 ==
 +
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:
 +
 +
 +
== 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.
  
A function's scope consists of everything within its braces ({ }).
+
A function's scope consists of everything within its braces <code>{ }</code>.
 
Normal variables as shown above will only work in a function unless carried over to another function.
 
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.
 
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.
It is mostly used to check and see if the player has done multiple things within the map to make something greater happen.
 
  
{{todo|Add code example more explanation about scopes}}
+
===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.
  
 
{{NavBar|HPL3/Scripting/Scripting_Guide/Sequences|Sequences|HPL3/Scripting/HPL3 Scripting Guide|HPL3 Scripting Guide|HPL3/Scripting/Scripting_Guide/Enums|Enums}}
 
{{NavBar|HPL3/Scripting/Scripting_Guide/Sequences|Sequences|HPL3/Scripting/HPL3 Scripting Guide|HPL3 Scripting Guide|HPL3/Scripting/Scripting_Guide/Enums|Enums}}

Revision as of 17: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:


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