Coding Game Objects

1.Game Object Architecture


After learning about all the object editor, now it is time to move onto coding your objects.

Game Objects are edited by making use of it’s dynamic button-bar seen on the left-side of the workspace:




There are five categories of code for all game objects. Each as unique and important as the other.


So Let’s Begin with the…..



2.Basic Object Functions



Looking at the picture above you can see there is quite the list of functions to edit. These functions are called automatically by the Game Pencil Engine each frame(Exception being the “Constructor, Descructor and Sprite Done” functions).


So piece by piece, let’s discuss their use:



The constructor of an object is where variables are declared for an object. This function is called as soon as the object is created and is the chief building block for game objects.



The destructor of an object is the last function and object performs before it dies. Depending on the game and it’s design it is where many dynamic variables are put to rest or where the player is rewarded points/coins, etc, etc.


Start Frame

The Start frame function is what is called at the start of each game frame within the game loop. It is crucial for many game AI and is used often to reset variables, states, etc.


Main Logic

The main logic function is the bread and butter of most game engines and is called after start-frame. Here is where the game ai usually takes place.


Sprite Done

The sprite done function is called after an object’s sprite animation is completed and is about to be reset. This is useful for games in which you need to have an action happen once a sprite animation is completed.


End Frame

The end frame function is also called every frame and is called right before the engine enters into it’s render state. This is usually used as a finalization step before presenting to the user what happened in the main-logic step.



The pre-render function is the first of the three render functions in which users typically call rendering-functions to be later drawn over such as shadows, effects, etc.



The render function for game objects is where you typically will draw the game object as you will like the player to be presented. If this function is left blank, than it will just default to drawing the sprite animation of the object at it’s (x,y) position in the case that it does not inherit from another object. 


Render HUD

This render function will possibly be the least used by most objects. This function is the last to be rendered before the next game frame. It is limited to game screen and where you will place gui elements and things on the HUD to relate information to users.

3.Timed Functions

The Game Pencil Engine allows people to create up to 10 timed functions to be called at the user’s discretion via frame-based countdowns.



Timed Functions begin at zero, like a normal array. 


Some possible uses of time functions include, but not limited to:

  • Spell cooldowns in RPG/RTS games.
  • Smoother pause before an object is destroyed.
  • To reduce the calling of an object’s game logic.
  • To represent an object thinking.
  • etc, etc


You can learn more below on how to set these countdowns here.

4.Collision Functions



This is possibly one of the most important and most-used sections of the object editor.


Here you are able to create custom-code to be automatically called each frame this object collides with a member of another object family. 


By clicking on the “Add Collision” button you are able to select which object-family this code is processed for(including itself).



  • The player touches a coin
  • The player touches an enemy
  • The player touches water
  • A spacecraft touches an enemy bullet
  • etc, etc, etc


5.Custom Functions



Custom functions are just that, custom functions created by you for objects to call on themselves or by other objects.

There are cases in which you may want to create custom functions in order to reduce the amount of times you re-use your code. Custom functions are passed down to child members of functions and as such the scope must remain known.


Examples of custom functions:

  • this.speak()
  • this.follow(objParam)
  • this.give_quest()
  • etc, etc

Many games can be made without ever using this tab and is intended for advanced use of the engine. These functions unlike the others are not called automatically by the game pencil engine.


You are able to return variables in custom functions as well as set parameters for it(JavaScript style for this version of GPE).



6.Special Functions



Special Functions are very similar to the Basic Object functions in which they are called automatically, but is called quite rarely.



This function occurs after at the opening of each scene.



This function occurs at the ending of each scene.



This function occurs at the start of the game.



This function occurs at the end of the game.



This function occurs when the player dies.


Game Over

This function occurs when the player runs out of lives.


7.Public Game Object Functions

The following functions are included within all game objects and if being called within the object’s function must be prefixed with “this.”

Collision-Related Function


-Returns the height of the collison-box of the object with it’s current sprite set. Returns 0 if sprite is not set.


-Returns the width of the collison-box of the object with it’s current sprite set. Returns 0 if sprite is not set.


Movement Related Functions

get_my_direction(  x2, y2)

-Returns the angle in degrees if the object pointed towards (x2,y2).

get_my_distance = function(  x2,  y2)

-Returns the distance between the object and (x2,y2).


-Returns the x-coordinate of the object.


-Returns the rightmost x-coordinate of the object’s collision box.


-Returns the y-coordinate of the object.


-Returns the bottom y-coordinate of the object’s collision box.


-Applies the GPE’s default physics on the object.

move_left( xAmount )

-Moves the object horizontally to the left  by xAmount(positive, 0 or negative number).

move_right( xAmount )

-Moves the object horizontally to the right by xAmount (positive, 0 or negative number).

move_up ( yAmount)

-Moves the object vertically up  by yAmount (positive, 0 or negative number).

move_down ( yAmount)

-Moves the object vertically down by yAmount (positive, 0 or negative number).


-Sets the object’s x-position to newX.


-Sets the object’s y-position to newY.

update_coords(newX, newY)

-Updates the object’s coordinates to (newX, newY).


General Purpose Functions


-Returns the depth of the object on the scene.


-Makes the object dependent on the camera to render and operate.


-Makes the object independent from the camera to render and operate.


-Destroys the object and removes it from the scene and game after calling it’s user-defined self-destruct code.

set_depth (newDepth)

-Sets the object depth in the game scene by newDepth.

set_sprite(nextSprite )

-Sets the object’s sprite to the newSprite id and updates it collisionbox code as well.


Timer Related Functions

get_countdown( timerId)

-Gets the amount of ticks left for the user defined function indexed by timerId(0 to 9).

start_countdown( timerId, gameFrames)

-Set’s the timer for the user defined function indexed by timerId(0 to 9) to gameFrames ticks.





Game Objects are dynamic and essentially control the entire game engine.


So whether if you’re making a small platformer game, episodic adventure, rts, rpg please become comfortable with the easy to use features as well as advanced features of game objects.



<<<Go back to Game Objects 101.

Suggest Edit