Javascript for Begginers - Construct 3

4

Index

Stats

4,150 visits, 7,859 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 5 Sep, 2021. Last updated 7 Sep, 2021
  1. Global and Local Variables

Accessing Global Variables e.g. I create a Global Variable named : Variable1 in event Sheet with value 100

I want to set the Variable1 to 10

runtime.globalvars.Variable1 = 10 ;

Now I want to call a value from a javascript variable that I created named : a

var a = 10;	
runtime.globalVars.Variable1 = a;

Now I want to set the variable "b" that I just create with the value that is in the Variable : Variable1 that is : 100

and I will see if it works with an alert that should show 100

var b = runtime.globalVars.Variable1 ;
alert(b)

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

or

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

// or 
runtime.globalVars.Variable1 += value // the same to subtract and set

or

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
//-------------------------------------------------------------------------~

or

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

or

var person = {
  firstName : "Rafael",
  lastName  : "Trigo",
  age     : 99,
  eyeColor  : "pink"
};
runtime.globalVars.Variable2 = person.age + " " + person.firstName // returns 99 Rafael

or

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');
}
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!