The Game Block Container Class

G.Block - Description

Blocks are simple container objects with basic variables and functions.

Their primary purpose is to organize your game code into major, logical blocks, which is why the class itself is so basic - you fill it up with game code.

Game code is stored in a Block's AI function, which is executed inside G.masterLoop() once per game loop iteration.

Only one Block is loaded and run at a time. Small games generally only need one Block, but dividing code into separate blocks can be useful on projects with large menuing systems, or complex levels.

Note: For clarity, this section often uses the 'G.Block.' prefix when referring to Block variables and methods. Inside game code, however, the prefix will always be a reference to the Block instance, eg, 'this.', 'G.B.level5.','G.B[G.cBid].', etc.

Variables

G.Block.id (type: string)

G.Block.id is the unique ID of the Block, and is used to to get a global reference to the Block.

If a Block's ID is 'myBlock', then the block can be globally referenced using G.B.myBlock.

G.Block.id is used by G.loadBlock(id) and G.makeBlock(id, load) to load and unload Blocks.

G.Block.S (type: object)

G.Block.S is an object for storing all your Block-specific game state -- things like NPC counts, level status.

GMP doesn't do any housekeeping with G.Block.S, so you can safely put whatever you want in it.

State variables are often set directly, eg. 'this.S.numAliens=12;', and can also be set in a batch using this.setState().

To reset G.Block.S, simply assign a new object to it (empty or otherwise), eg,

this.S={x:4}; or this.S={};

To delete a member variable from G.Block.S, use the delete keyword, eg, delete G.B.optionsMenu.S.gamesave2;

If you have only one Block in your game, you will likely ignore G.Block.S and just use G.S to store state variables.

G.Block.O (type: object)

G.Block.O is the set of all Gobs (game objects) in this Block. Gobs are referenced by ID. Unlike G.O, Gobs are added manually to this set.

This set is useful for performing batch operations on all the Gobs in a specific Block.

To reset G.Block.O, simply assign a new object to it (empty or otherwise), eg, this.O = { alien5: G.O.alien5 }; or this.O = { };

To delete a member variable from G.Block.O, use the delete keyword, eg, delete this.O.alien5;

If you have only one Block in your game, you will likely ignore G.Block.O and just use G.O to manage Gobs.

G.Block.tmp (type: object)

G.Block.tmp is a central storage for any temporary values within a block.

There's no garbage collection on this object, it is merely an identifiable name space provided for convenience.

There's no real advantage to using this tmp object over the global (G.tmp) object, it's here for convenience.

If you have only one Block in your game, you will likely ignore G.Block.tmp and just use G.tmp.

Methods

Note: many of the following methods return an object instead of returning nothing. The returned object is often the parent object (i.e., the Block). Doing this allows for method chaining, which can lead to compact code. The tutorial has examples that use chaining.

G.Block.setState(O) (returns this)

G.Block.setState(O) adds the members of object 'O' to the G.Block.S object.

If a member of O exists in G.Block.S, then it over-writes the existing value, otherwise it just adds it.

This function returns the parent Block object.

See G.Block.S for more details.

G.Block.AI() (returns this)

G.Block.AI() is the 'brain' function of a Block.

After a Block is loaded using G.loadBlock(myBlock.id), myBlock.AI() will be called once per game loop, executing myBlock's game logic.

By default G.Block.AI() does nothing, but it is always overwritten with a game play logic function (that the game developer writes) during game initialization.

See the tutorial for more details about how to use this function.

This function returns the parent Block object.

G.Block.load() (returns this)

G.Block.load() is called once, whenever a Block is loaded with G.loadBlock().

By default G.Block.load() does nothing, but it is always overwritten with a function (that the game developer writes) during game initialization.

This function should perform any tasks that are required to initialize or reload a Block.

See the tutorial for more details about how to use this function.

This function returns the parent Block object.

G.Block.unload() (returns this)

G.Block.unload() is called on the current Block just before it is swapped for another Block.

This function should perform any tasks that are required to shutdown a Block, such as turning off Block-specific Gobs, serializing the Block state and storing it as a cookie, or emptying the Block's tmp object.

By default G.Block.unload() turns off all Gobs assigned to G.Block.O, which is generally enough for most games. If you do not want this behaviour, then you will need to overwrite this function.

This function returns the parent Block object.

Supporting In-line Code

There is an in-line chunk of code immediately after the G.Block method definitions. This code uses method chaining, which is described a little more in the tutorial.

Here's the code: G.makeBlock('defaultBlock').loadBlock('defaultBlock').masterLoop();

This code has three parts.

First, it makes a default Block with G.makeBlock('defaultBlock'); This is because at least one Block is required for the game engine to work.

Second, this code loads the new Block with G.loadBlock('defaultBlock'); so that the main game loop has an active Block to run when it starts up.

Finally, this code starts the game loop, by calling G.masterLoop(): G.masterLoop();

The default Block can be re-used or ignored, as it consumes almost no resources.

Previous: G - Global Game Object Next: G.Gob