Scripted events
An event is a script function that is called by the Novelty engine when a certain condition is met. In programming terms it works like a callback. The difference is that events are specifically associated with objects. Take a look at the following code:
event MyEvent.OnUpdate { // Do something }
An event's body starts with the keyword event. Next there's the name of the event, in this case MyEvent, followed by a dot and the event type, OnUpdate. Unlike regular functions, there are no parentheses.
Associating events to objects
If you compile the code above, Novelty will recognize it as an event and you can associate one or more objects to it. You do this in the Property inspector (while an object is selected) under Associate scripts.
Fig 1: Binding an event to an object
In this example, we've bound MyEvent.OnUpdate with an object, which means the code will be called every frame for that object.
Implicit arguments
Most events have implicit input arguments. These are passed to the event by the engine letting you know the context in which the event was triggered. Different events are given different arguments. For instance, the OnUpdate event is given a handle to the associated object (sender). This lets us act on that object alone:
event MyEvent.OnUpdate { // Move object 1 pixel to the right every frame sender.position.x += 1; }
Another argument that is passed to the OnUpdate event is elapsed which is the time passed since the last update.
event MyEvent.OnUpdate { // Move object 100 pixels per second sender.position.x += 100 * elapsed; }
Automatic binding by name
Objects are automatically bound to events with the same name, so an object named "Ball" will be automatically bound to the Ball.OnUpdate event.
Global events
Sometimes you might need to write an event for the entire novel. To do this you simply have to follow a simple naming convension. By assigning the name Novel to an event, it will be treated as a global event:
event Novel.OnMouseClick { Print("A mouse button was clicked"); }
List of event types
- OnUpdate - Called every frame (Novel, Object)
- OnCreate - Called when an object is created (Object)
- OnDestroy - Called when an object is destroyed (Object)
- OnInit - Called once at the very start of the game (Novel)
- OnShow - Called during an object's is appearance (Object)
- OnHide - Called during an object's is disappearance (Object)
- OnEnter - Called at the start of the page (Novel, Page)
- OnExit - Called at the end of the page (Novel, Page)
- OnMouseClick - Called when mouse is clicked (Novel, Object, Page)
- OnMouseRelease - Called when mouse buttons is released (Novel, Object, Page)
- OnMouseEnter - Called when mouse enters over an object (Object)
- OnMouseMove - Called when mouse moves over an object(Novel, Object)
- OnMouseLeave - Called when mouse leaves an object (Object)
- OnMouseWheel - Called when mouse wheeled is scrolled (Novel, Object, Page)
- OnKeyDown - Called when a keyboard key was pressed (Novel, Page)
- OnKeyUp - Called when a keyboard key was released (Novel, Page)
- OnParagraph - Called before a dialogue paragraph is displayed (Novel)