LGCK event model

The engine uses handlers (such as the one below) to respond to various event. A handler is just a code snippet that is called when a condition is reached.

-- this a simple ai for a drone monster that moves LEFT/RIGHT.  
 
unMapSprite(self); 
 
if canFall ( self) then
  moveSprite( self, DOWN)  ;
  playSound ("FALL");
else
  if ticks % 5 == 0 then
    local x;
    local y;
    local aim; 
 
    x , y, aim = getSpriteVars(self) 
 
    if isPlayerThere(self, aim) then
      attackPlayer ( self );
    end 
 
    if aim < LEFT or aim > RIGHT then
      aim = LEFT
    end 
 
    if canMove(self, aim) then
      moveSprite(self, aim);
      if canFall(self) then
	aim = 5 - aim;
	moveSprite(self, aim);
      end 
    else
	aim = XOR(aim, 1);      
    end 
 
    setAim ( self, aim ); 
 
  end 
end 
 
mapSprite(self);

Game Events

onInitGame

This event is fired the game starts.

onPrepareLevel

This event is fired when a level starts.

onCompleteLevel

This event is fired when a level is completed.

onDeath

This event is fired when the player is killed.

onGoalCollected

This event is fired when all the goal objects are collected.

onGoalKilled

This event is fired when any goal object is killed (or pick-up which is the same concept in LGCK).

onPickup

This event fires whenever a pick-up trigger is killed. The classic example is the diamond or flower. Both can be made pick-up trigger class .

onLoadGame

This event fires when a save game is being restored.

onSaveGame

This event fires when a save game is being created.

onRestartLevel

This event is triggered when the level is restarted. The usually fires up when the player is killed and has to start the level again.

onNotifyClosure

This event fires up when the level is about to close. In between having completed the objective and completing the level and leaving the level.

Level Events

onCreate

This method is called when the level is first created.

onDraw

This method is called when it's time to draw the screen.

onRestart

This method is called when the level is restarted.

onGoalCollected

This method is called when all the goals are collected just before the level is completed

onGoalKilled

This method is called when a sprite marked as a goal is killed.

onLevelCompleted

This method is called just before moving to the next level.

onTimeOut

This method is called when timeLeft reaches zero.

onKeyPressed

This method is called when a key is presed. Use getLastKey() to retrieve the keyCode.

onKeyUp

This method is called when a key is released. Use getLastKey() to retrieve the keyCode.

onHandler

This method is called on every tick for each level.

onNotifyClosure

This works the way as same as the game notify closure event.

onIntroDraw

This method is called while the introduction screen is being displayed. You can override this to display custom text.

Sprite Events

OnSpawn

This method is called when this object is created ( respawns included ).

onActivate

This method is called when this object is activated.

onDeath

This method is called when this object is destroyed (death, picked up etc.)

onHurt

This method is called when this object is hurt. Only applies to player at the moment.

onTouch

This method is called when the player touches this object.

onTrigger

This method is when a trigger is called on this object. Trigger key on this object must match that of the source for this event to be called.

onHandler

This method is called when the object handler is called. You can use this method to redefine the custom monster class for example.

onSplat

This method is called when the player falls on this object.

onHitTest

This method is called when the player hit this object from below. (player aim supplied)

onZKey

This method is called z-key action performed on this object

onAuto

This method is executed when automated timer rings

onLeftClick

This method is called when the player left click on this sprite

onRightClick

This method is called when the player right click on this sprite

onPowerUp

tba

onJump

This method is called when the player jumps.

onFall

This method is called then object falls.

onLanding

This method is called when the object lands after a fall.

onLeap

This method is called when object performs as leap. Similar to onMove.

onMove

This method is called when the object is moving (e.g. walking). This is currently only implemented for the player.

onFire

This method is called when the player/monster is firing.

onAttack

onNotifyClosure

This works the way as same as the game notify closure event.

Special notes

The game engine recognizes many objects. These objects are numbered by their ObjectType (or ObjType) and can be referenced to by the SPRITE_XXXX constants. A type refers to an object not an instance.

An object instance (also called a sprite) is simply an occurrence of the object found on a level. ObjId (or ObjectId) refers to a specific instance of an object. Each of these occurrences will have special properties such as a trigger key, visibility, frozen and goal attributes. In an object event handler, the objectId will always be available via the local variable `self`. (see examples)

See Also

Lua Binding
Lua About
Sprite Class