#include <ScriptTarget.h>
Classes | |
struct | CallbackFunction |
class | Event |
class | EventRegistry |
struct | RegistryEntry |
struct | ScriptEntry |
Public Member Functions | |
virtual const char * | getTypeName () const =0 |
Script * | addScript (const char *path) |
bool | removeScript (const char *path) |
void | addScriptCallback (const Event *event, const char *function) |
void | removeScriptCallback (const Event *event, const char *function) |
void | clearScripts () |
bool | hasScriptListener (const char *eventName) const |
bool | hasScriptListener (const Event *event) const |
const Event * | getScriptEvent (const char *eventName) const |
template<typename T > | |
T | fireScriptEvent (const Event *event,...) |
Protected Member Functions | |
ScriptTarget () | |
virtual | ~ScriptTarget () |
void | removeScript (ScriptEntry *entry) |
void | registerEvents (EventRegistry *registry) |
Protected Attributes | |
RegistryEntry * | _scriptRegistries |
ScriptEntry * | _scripts |
std::map< const Event *, std::vector < CallbackFunction > > * | _scriptCallbacks |
Defines an interface for supporting script callbacks.
Classes that extend ScriptTarget can expose script events using the GP_SCRIPT_EVENT macros. Custom events should be registered using these macros at the top of the class definition. Events can be fired by calling the ScriptTarget::fireScriptEvent method, passing the registered ScriptTarget::Event object and any required parameters.
In addition to script events that are explicitly defined by a custom ScriptTarget class, all ScriptTarget scripts implicitly support an "attached" event. This event is called immediately after such a script is attached to a ScriptTarget and it takes a single parameter: the ScriptTarget object.
gameplay::ScriptTarget::ScriptTarget | ( | ) | [protected] |
Constructor.
virtual gameplay::ScriptTarget::~ScriptTarget | ( | ) | [protected, virtual] |
Destructor.
Script* gameplay::ScriptTarget::addScript | ( | const char * | path | ) |
Attaches a script to this object.
Scripts attached to a ScriptTarget are loaded using the PROTECTED scope, which loads scripts into their own protected script environment, allowing variables with the same name to be used without colliding with other scripts.
path | Path to the script. |
void gameplay::ScriptTarget::addScriptCallback | ( | const Event * | event, |
const char * | function | ||
) |
Adds the given global script function as a callback for the given event.
Individual script callback events registered via this method are expected to be global script functions. Registering individual callbacks in this manner is generally slower than registering a single script to handle script events for an object.
event | The event to add the callback for. |
function | The name of the script function to call when the event is fired; can either be just the name of a function (if the function's script file has already been loaded), or can be a URL of the form scriptFile.lua::functionName. |
void gameplay::ScriptTarget::clearScripts | ( | ) |
Removes all scripts and callbacks from this object.
bool gameplay::ScriptTarget::fireScriptEvent< bool > | ( | const Event * | event, |
... | |||
) |
Fires the specified script event, passing the specified arguments.
The only supported return types are void and boolean. When a boolean return type is used and there are multiple scripts registered for the given script event, event delegation will stop at the first script that returns a value of true.
event | The script event to fire, which was returned from EventRegistry::addEvent. |
... | Optional list of arguments to pass to the script event (should match the script event argument definition). |
The fire script event template specialization.
event | The event fired. |
... | Optional list of arguments to pass to the script event. |
Template specialization.
evt | The event fired. |
... | Optional list of arguments to pass to the script event (should match the script event argument definition). |
const Event* gameplay::ScriptTarget::getScriptEvent | ( | const char * | eventName | ) | const |
Gets the event object for the given event name, if it exists.
eventName | Name of the event. |
virtual const char* gameplay::ScriptTarget::getTypeName | ( | ) | const [pure virtual] |
Gets the type name identifier for the class that extends ScriptTarget.
Implemented in gameplay::Control, gameplay::Transform, gameplay::PhysicsController, gameplay::Form, gameplay::AnimationClip, gameplay::Node, gameplay::Container, gameplay::Button, gameplay::TextBox, gameplay::ImageControl, gameplay::RadioButton, gameplay::Slider, gameplay::CheckBox, gameplay::JoystickControl, and gameplay::Label.
bool gameplay::ScriptTarget::hasScriptListener | ( | const char * | eventName | ) | const |
Determines if there is a script installed that is listening for the given script event (i.e. has a function callback defined for the given event).
eventName | The script event to check. |
bool gameplay::ScriptTarget::hasScriptListener | ( | const Event * | event | ) | const |
Determines if there is a script installed that is listening for the given script event (i.e. has a function callback defined for the given event).
event | The script event to check. |
void gameplay::ScriptTarget::registerEvents | ( | EventRegistry * | registry | ) | [protected] |
Registers a set of supported script events and event arguments for this ScriptTarget.
The passed in EventRegistry object should contain a list of all script events to be supported by this ScriptTarget, along with their parameter definitions. This registry object will be held onto for the lifetime of the ScriptTarget and it is recommended that it be shared among all ScriptTargets of the same class/type.
This should normally be set using the GP_REGISTER_SCRIPT_EVENTS macro in the constructor of the child class.
registry | The EventRegistry containing the list of supported script events. |
bool gameplay::ScriptTarget::removeScript | ( | const char * | path | ) |
Removes a previously attached script from this object.
path | The same path that was used to load the script being removed. |
void gameplay::ScriptTarget::removeScript | ( | ScriptEntry * | entry | ) | [protected] |
Removes the specified script.
entry | The script entry to be removed. |
void gameplay::ScriptTarget::removeScriptCallback | ( | const Event * | event, |
const char * | function | ||
) |
Removes the given script function as a callback for the given event.
event | The event to remove the callback for. |
function | The name of the script function. |
std::map<const Event*, std::vector<CallbackFunction> >* gameplay::ScriptTarget::_scriptCallbacks [protected] |
Holds the list of callback functions registered for this ScriptTarget.
RegistryEntry* gameplay::ScriptTarget::_scriptRegistries [protected] |
Holds the event registries for this script target.
ScriptEntry* gameplay::ScriptTarget::_scripts [protected] |
Holds the list of scripts referenced by this ScriptTarget.