Novelty scripting reference > Scripted events

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


Back to top