First, an overview:
Scripted Global variables:
- Are stored in the save file, and carry between ALL maps.
- Are used by using GetGlobalVar<Type> and SetGlobalVar<Type>
- Names are passed into the above functions as a string
- Default to 0 (or ""), and are created dynamically so can be used without an initialisation.
Scripted local vars
- Are stored in the save file, but only apply to one map.
- Are used by using GetLocalVar<Type> and SetLocalVar<Type>
- Names are passed into the above functions as a string
- Default to 0 (or ""), and are created dynamically so can be used without an initialisation.
Angelscript vars
- Defined by placing the type (e.g: int, string, float, uint) followed by an identifier.
- Are the stuff which can be passed into functions and the like
- Are not saved, or transferred between maps.
- The scope (I.e: how many {'s in it is) determines what can access it
- Must be defined before use.
So, how can we use each one?
//Local vars
void RandomRoutine1()
{
//Assignment
SetLocalVarInt("variable_name",1234);
SetLocalVarString("RandomVaraible_1","Yo dawg!");
//Appending / adding
AddLocalVarInt("variable_name",1);
AddLocalVarString("RandomVaraible_1","I heard you like strings!");
//Retreving
int x = GetLocalVarInt("variable_name");
string y = GetLocalVarString("RandomVaraible_1");
//X now has the value 1235 (1234 + 1)
//Y now has the value "Yo dawg!I heard you like strings!"
}
//Global vars work similarly. Take for example, using a score variable
//to chose which ending the player gets
void OnGameStart()
{
SetGlobalVarInt("PlayerScore",1); //Initialise global score
}
void somewhereOnAnylevel()
{
AddGlobalVarInt("PlayerScore",1); //Add 1 to global score var
}
void somewhereAtEndOfGame()
{
switch(GetGlobalVarInt("PlayerScore"))
{
case 0: {badEnding(); break;}
case 1: {mediumEnding(); break;}
case 2: {goodEnding(); break;}
default: {hackerEnding();}
}
}