Leviathan  0.8.0.0
Leviathan game engine
Events

Listening for events in scripts

Standalone scripts can create a listener object

// This example creates a new listener object //
int OnChangedControlScheme(GenericEvent@ event){
NamedVars@ tempvalues = event.GetNamedVars();
string player = string(tempvalues.GetSingleValueByName("Player"));
string controlsname = string(tempvalues.GetSingleValueByName("ControlsName"));
return 1;
}
// Also a function with the signature
int OnNormalEvent(Event@ event){
}
// Could be defined and the following null replaced with it to listen for predefined events
EventListener@ OnChangedEvents = EventListener(null, OnChangedControlScheme);
// The only parameter is the name of the GenericEvent to listen for
OnChangedEvents.RegisterForEvent("GuiChangePLayerControls");
// For normal events you can listen for them by calling
OnChangedEvents.RegisterForEvent(EVENT_TYPE_TICK);
See also
Leviathan::Script::EventListener

GUI Scripts can use script metadata to listen for specific events

[@Listener="OnInit"]
int SetVersion(GuiObject@ instance, Event@ event){
// Set the text as the current Pong version //
string newtext = GetPongVersion();
instance.GetTargetElement().SetText(newtext);
return 1;
}

Getting values from GenericEvent

To get values from a GenericEvent you first need to get the underlying Leviathan::NamedVars and retrieve values from there

int OnChangedControlScheme(GenericEvent@ event){
// Getting the NamedVars //
NamedVars@ tempvalues = event.GetNamedVars();
// Getting a value and casting it to a type and assignong it to a variable //
string player = string(tempvalues.GetSingleValueByName("Player"));
// It is also possible to get other types
int somenumber = int(tempvalues.GetSingleValueByName("ControlsName"));
return 1;
}
See also
NamedVars

Firing events

To fire an event from AngelScript you first need to create an event object

// Create a GenericEvent like this //
GenericEvent@ generic = GenericEvent("NameOfEvent");
// Create an Event like this //
// Note: only events which don't take additional data parameters can be created like this
Event@ event = Event(EVENT_TYPE_INIT);
See also
Leviathan::EVENT_TYPE for possible types
Todo:
List events that are recognizable by scripts here

Then call the event sending function on the global Leviathan::EventHandler.

GetEngine().GetEventHandler().CallEvent(generic);

Setting event parameters

To set the data contained within a GenericEvent, you first need to access the event's Leviathan::NamedVars

NamedVars@ vars = generic.GetNamedVars();

And add variables there.

See also
NamedVars
Todo:
Allow scripts to create data for Leviathan::Event objects