TGB/Behaviors/Callbacks

From TDN

Contents

This is a complete list of the callbacks available to use with behaviors in TGB.

Initialization

These all are called at similar times, based on similar events, but they are different enough to all be necessary. For instance, some of these are called both in the game and editor, and some in the game only.

onBehaviorAdd

Called every time the behavior is added to an object, including in the editor. It should be used to initialize data. For instance, if a behavior requires mouse events, this would be a good place to call setUseMouseEvents on the object.

onBehaviorRemove

Called every time the behavior is removed from an object, including in the editor. Unless you are dynamically adding and removing the behavior from objects, it is not necessary to use this. If you are though, it is a good place to reset any data that was set in onBehaviorAdd. For instance, if you bind keys in onBehaviorAdd, you may want to unbind them in this method.

onLevelLoaded

Parameters:

  • %scenegraph - The t2dSceneGraph that the object is in.


This is only called in the game during a call to t2dSceneWindow::loadLevel. It is not called on objects that are created during the game, only on objects that are in a level file. This is a good place to initialize data that requires knowledge about the scenegraph, or that you don't want reflected in the editor.

onLevelEnded

Parameters:

  • %scenegraph - The t2dSceneGraph that the object is in.


This is a mirror of onLevelLoaded, and as such, should be used to clean up anything that was done in onLevelLoaded. This is called on every object that is in the level that is being ended, including those created outside of the level file.

onAddToScene

Parameters:

  • %scenegraph - The t2dSceneGraph that the object was added to.


This is called anytime an object is added to a scenegraph, except in the editor. This includes when a level is loaded, when an object is moved from one scenegraph to another, or when an object is added to its first scenegraph.

onRemoveFromScene

Parameters:

  • %scenegraph - The t2dSceneGraph that the object was removed from.


This is called anytime an object is removed from a scenegraph, except in the editor. This includes when a level is ended or when an object is moved from one scenegraph to another.

Mouse Events

For any of these events to be triggered, the object must have useMouseEvents enabled by calling setUseMouseEvents(true). Also, objects can call setMouseLocked(true) to lock the mouse with the object. This allows the object to receive all mouse event callbacks regardless of whether or not the mouse is within the bounding box of the object.

All mouse events are passed the same list of parameters. They are:

  • %modifier - A bitmask specifying any modifier keys that are pressed.
    • Bit 0: Left Shift
    • Bit 1: Right Shift
    • Bit 2: Left Control
    • Bit 3: Right Control
    • Bit 4: Left Alt (Windows) or Command (Mac)
    • Bit 5: Right Alt (Windows) or Command (Mac)
    • Bit 6: Left Option (Mac)
    • Bit 7: Right Option (Mac)
  • %worldPosition - The current position of the mouse.
  • %clicks - The number of times the mouse has been clicked in the same spot in a short amount of time.

onMouseMove

Called when the mouse moves within the object's bounding box.

onMouseEnter

Called when the mouse enters the object's bounding box.

onMouseLeave

Called when the mouse leaves the object's bounding box.

onMouseDown

Called when the left mouse button is clicked within the object's bounding box.

onMouseDragged

Called when the left mouse button is held down and the mouse moves within the object's bounding box.

onMouseUp

Called when the left mouse button is released within the object's bounding box.

onRightMouseDown

Called when the right mouse button is clicked within the object's bounding box.

onRightMouseDragged

Called when the right mouse button is held down and the mouse moves within the object's bounding box.

onRightMouseUp

Called when the right mouse button is released within the object's bounding box.

Game Events

onCollision

Parameters:

  • %srcObj - This object, that's colliding.
  • %dstObj - The object that is being collided with.
  • %srcRef - Custom information set by the source object. Only used by t2dTileLayer to pass the logical tile position of the tile that is colliding.
  • %dstRef - Custom information set by the destination object. Only used by t2dTileLayer to pass the logical tile position of the tile that is collided with.
  • %time - The time since the previous tick when the collision happened.
  • %normal - The normal vector of the collision.
  • %contacts - The number of contacts in the collision.
  • %points - The list of contact points.


This is called when an object collides with another object in the scene. Collision callbacks must be enabled on the object initiating the collision with a call to setCollisionCallback(true) for the callback to be triggered.

onWorldLimit

Parameters:

  • %limitMode - The world limit response mode set on the object.
  • %limit - This is either "Left", "Right", "Top", or "Bottom", depending on the side of the world limit that was hit.


This is called when an object reaches its world limit. It must be enabled in the editor by checking the callback box in the World Limit rollout.

onUpdate

This is called every tick (32 milliseconds). It should be used only when necessary as it can have pretty serious performance implications. Because of this, it must be enabled with a call to enableUpdateCallback. It can be disabled by calling disableUpdateCallback.

onPositionTarget

This is called when an object reaches its position target as set by setPositionTarget or moveTo.

onRotationTarget

This is called when an object reaches its rotation target as set by setRotationTarget or rotateTo.

onTimer

This is called when the timer set on an object via setTimerOn is triggered.

Animation Events

onAnimationStart

The animation has started to play.

onAnimationEnd

The animation has stopped playing.

onFrameChange

Parameters:

  • %frame - The frame that was changed to.


The animation has entered a new frame of playback.

Trigger Events

onEnter

Parameters:

  • %object - The object that entered the trigger.


Called when an object enters the trigger.

onStay

Parameters:

  • %object - The object that is in the trigger.


Called when an object remains inside a trigger.

onLeave

Parameters:

  • %object - The object that left the trigger.


Called when an object leaves the trigger.