Coding with GPE

1.Summary

 

The “scripting language” for the Game Pencil Engine is JavaScript.

To learn more about JavaScript see W3Schools glorious tutorials.

 

Below we will provide you a non-exhaustive list of basic JavaScript Syntax that you should become familiar with when making a game with the Game Pencil Engine.

 

The Game Pencil Engine is easy to learn if one has a basic understanding of object-oriented programming or JavaScript.

2.Variables

JavaScript variables are named-containers for keeping track of data values.

 

Variables can be either:

  • Local variables(Assigned to game-objects, usually made with a prefix of “this.” in front of it)
  • Global variables(they are stored essentially in the browser and can be assessed by any object or function.
  • Temporary variables are variables used in functions, but are automatically deleted at the end of the function execution
  • Constant variables are variables in which their value can not be altered once set[Not fully unchangeable in JS].

 

Declaring variables in functions is as simple as saying:

var pi = 3.14;

var x = “X”;

var ten = 10;

 

Variables MUST begin with a letter!

 

Declaring variables in object constructors are done essentially the same way as above but in this format:

this.pi = 3.14;

this.x = “X”;

this.ten = 10;

 

3.Loops

 

 

Loops are executed multiple times until the condition is no longer met.

 

There are 3 standard loops:

 

For Loops

 

The syntax for a for loop is

(one-time-use-code; condition; iteration)

{

     //Code to be execution while the condition is true

}

An example for loop will be:

for( var i = 0; i < 24; i++)

{

     gpe.log(“It is “+i”:00″);

}

The above logs for 24 times(Starting at 0, Stopping at 23) fictional time.

 

While Loops

 

While loops are a much simpler type of loop and consists of only the condition

It’s syntax will be:

while(condition)

{

    //Execute Code while condition is true

}

Examples will be:

 

while( true){}//This loop will never end unless the program crashes, a zombie apocalypse or some other error occurs.

To redo the example above we can do this:

 var i = 0;

while(  i < 24 )

{

     gpe.log(“It is “+i”:00″);

    i++;

}

Do-While Loops

 

The Do-While loop is similar to it’s brother the “While-Loop”, but it does 1 execution of the code and then checks for the condition for further execution.

 

Here is a summary of it’s synthax:

do {
    //code block to be executed
}
while (condition);

 

And to repeat that glorious fake 24-hour code we will have to do:

 var i = 0;

do

{

     gpe.log(“It is “+i”:00″);

    i++;

}while(  i < 24 )

 

Each loop has their own positive advantage in programming and you will have to find which loop will work best for you in each situation.

 

Make sense? I hope so, if not see w3schools better tutorial.

 

4.Arrays, Lists and Vectors

In JavaScript Arrays are a a list of variables.

 

Arrays set/access their data by using brackets”[]” after their variable name.

 

For example:

var myShoes = []; //declaration of the array

 

//Assignment of it’s members:

myShoes[0] = “red sneakers”;

myShoes[1] = “black dress shoes”;

myShoes[2] = “orange running shoes”;

 

Array’s list begin at 0 and go on “indefinitely”, but must be assigned in order.

 

One of array’s best feature is how easy it is to iterate through their members. Using the shoe example above we can create the following code:

 

for(var i = 0; i < myShoes.length; i++)

{

    gpe.log(“My shoes at mat #”+i+” are “+myShoes[i]+”.” );

}

 

You may be able to infer from the code above that the amount of elements in an array is defined as “length”.

 

Suggest Edit