Local Variables
Local Variables can only be used in the script block in the Event Sheet
I create a local variable named : Variable2
I created a new script file in the project tab , and set it to import for events , so we can call all the information in this file from event sheet , I created a variable : var c = 987654321; in this file
localVars.Variable2 = c // I call the Local Variable that I created and set it with the var c value
runtime.globalVars.Variable1 = localVars.Variable2// set the global variable with the local variable
alert(runtime.globalVars.Variable1)//the alert will show 987654321
You can create a variable in the script Block in the Event Sheet like bellow , and you can add more information in the same variable
For accessing the variables created in event sheet in script files you can use globalThis
var d = localVars.Variable2 + runtime.globalVars.Variable1 +10
alert(d);
//or
//globalThis.d = localVars.Variable2 + runtime.globalVars.Variable1 +10
//alert(globalThis.d);
Working with Javascript Variables
Set , Add or Subtract from variables
If we have a variable in script Named : Coins and want to add , set or subtract values after some condition like on colision or on tap , we can do it
let coins = 0; // declare the variable
coins +=1 ; //add 1 to coins
coins =1 ; //set the coins to 1
coins -=1 ; //subtract 1 from coins
let value = 1; // insted of use 1 we can use this variable
coins +=value ; //add 1 to coins
coins =value ; //set the coins to 1
coins -=value ; //subtract 1 from coins
// or
runtime.globalVars.Variable1 += value // the same to subtract and set
globalThis.coins = 0; // or you can use this
globalThis.coin +=value;
Functions and Ternary using Variables
runtime.globalVars.Variable2 = 2 > 1 ? " correct" : "Incorrect"// Correct
// functions
//-------------------------------------------------------------------------~
var v = 50;
var z = 45;
runtime.globalVars.Variable2 = myFunction(v,z);
function myFunction(p1, p2) {
return p1 * p2; } // The function returns the product of p1 and p2 : 2250
var person = {
firstName : "Rafael",
lastName : "Trigo",
age : 99,
eyeColor : "pink"
};
runtime.globalVars.Variable2 = person.age + " " + person.firstName // returns 99 Rafael
var z = 20 ;
if ( z > runtime.globalVars.Variable2){
runtime.globalVars.Variable2 = 1000
} else{
runtime.globalVars.Variable2 = 50 + z
}
Now lets work with cases see the examples and change the value in the Variable2 to see the results , I set the Variable initialy with the value Pig2 so it will output " Some Pigs "
var Animal = null;
var Animal = runtime.globalVars.Variable2
switch (Animal) {
case 'Dog':
alert('Dog');
break;
case 'Cat':
alert('Cat');
break;
case 'Bird':
alert('Bird');
break;
case 'Pig':
case 'Pig1':
case 'Pig2':
case 'Pig3':
alert('Some Pigs');
break;
case 'Any Animal':
default:
alert('Any Animal');
}