Custom events?
by Milan Rancic · in Torque Game Builder · 08/11/2010 (12:22 pm) · 6 replies
I notice that there is event manager in TGB, and i tried to create my own events, but i just couldn't get it to work... i tried many different combinations, but in vain.
Does anyone worked with these before? If i can make my own events (not just onAddToScene, onCollision or onMouseDown) that would be awesome and very powerful!
------------------------------------------------------------------------
EventManager
Hierarchy: This object is derived from:
* SimObject
Methods:
isRegisteredEvent(String event)
Description: Check if an event is registered or not.
Params: event: The event to check. Return: Whether or not the event exists.
postEvent(String event, String data)
Description: Trigger an event.
Params: event: The event to trigger. data: The data associated with the event. Return: Whether or not the event was dispatched successfully.
registerEvent(String event)
Description: Register an event with the event manager.
Params: event: The event to register. Return: Whether or not the event was registered successfully.
remove(SimObject listener, String event)
Description: Remove a listener from an event.
Params: listener: The listener to remove. event: The event to be removed from.
subscribe(SimObject listener, String event, String callback)
Description: Subscribe a listener to an event.
Params: listener: The listener to subscribe. event: The event to subscribe to. callback: Optional method name to receive the event notification. If this is not specified, "on[event]" will be used. Return: Whether or not the subscription was successful.
unregisterEvent(String event)
Description: Remove an event from the.
Params: event: The event to remove.
dumpEvents()
Description: Print all registered events to the console.
dumpSubscribers(String event)
Description: Print all subscribers to an event to the console.
Params: event: The event whose subscribers are to be printed. If this parameter isn't specified, all events will be dumped.
Does anyone worked with these before? If i can make my own events (not just onAddToScene, onCollision or onMouseDown) that would be awesome and very powerful!
------------------------------------------------------------------------
EventManager
Hierarchy: This object is derived from:
* SimObject
Methods:
isRegisteredEvent(String event)
Description: Check if an event is registered or not.
Params: event: The event to check. Return: Whether or not the event exists.
postEvent(String event, String data)
Description: Trigger an event.
Params: event: The event to trigger. data: The data associated with the event. Return: Whether or not the event was dispatched successfully.
registerEvent(String event)
Description: Register an event with the event manager.
Params: event: The event to register. Return: Whether or not the event was registered successfully.
remove(SimObject listener, String event)
Description: Remove a listener from an event.
Params: listener: The listener to remove. event: The event to be removed from.
subscribe(SimObject listener, String event, String callback)
Description: Subscribe a listener to an event.
Params: listener: The listener to subscribe. event: The event to subscribe to. callback: Optional method name to receive the event notification. If this is not specified, "on[event]" will be used. Return: Whether or not the subscription was successful.
unregisterEvent(String event)
Description: Remove an event from the.
Params: event: The event to remove.
dumpEvents()
Description: Print all registered events to the console.
dumpSubscribers(String event)
Description: Print all subscribers to an event to the console.
Params: event: The event whose subscribers are to be printed. If this parameter isn't specified, all events will be dumped.
#3
If i create event called:
$MyEventManager.registerEvent( "eventXYZ" );
My object sould respond to:
function MyListener::onEventXYZ( %this, %data ) { }
I just add "on" in front of the event's name, right?
------------------------
I want to use global events to cast them when something global occurs. For eg. when player dies, i want to cast "playerDied" event, and only some enemies on stage will listen to it and they will change their behavior. Also the event "regenerate" that will cast every second, and every actor in game with regeneration ability will listen to it, and will regenerate every second (if wounded).
I can find many uses for events, this is just a beginning ;)
08/13/2010 (9:52 am)
I just dont get one thing... how did you come up with ::onSomeCoolEvent statement?If i create event called:
$MyEventManager.registerEvent( "eventXYZ" );
My object sould respond to:
function MyListener::onEventXYZ( %this, %data ) { }
I just add "on" in front of the event's name, right?
------------------------
I want to use global events to cast them when something global occurs. For eg. when player dies, i want to cast "playerDied" event, and only some enemies on stage will listen to it and they will change their behavior. Also the event "regenerate" that will cast every second, and every actor in game with regeneration ability will listen to it, and will regenerate every second (if wounded).
I can find many uses for events, this is just a beginning ;)
#4
08/13/2010 (1:10 pm)
Are you familiar with torquescript schedules?
#5
08/13/2010 (2:46 pm)
@Milan - Hmm. I'll debug the source code to see if you need the "on" before the function, or if it is 1:1 naming.
#6
@Kevin: yes, i know about schedules, i use them often.
08/13/2010 (10:18 pm)
Well, it is working now with "on" prefix. Maybe thats why i couldt make it work trying by my self.@Kevin: yes, i know about schedules, i use them often.
Employee Michael Perry
ZombieShortbus
// Create the EventManager. $MyEventManager = new EventManager() { queue = "MyEventManager"; }; // Create an event. $MyEventManager.registerEvent( "SomeCoolEvent" ); // Create a listener and subscribe. $MyListener = new ScriptMsgListener() { class = MyListener; }; $MyEventManager.subscribe( $MyListener, "SomeCoolEvent" ); function MyListener::onSomeCoolEvent( %this, %data ) { echo( "onSomeCoolEvent Triggered" ); } // Trigger the event. $MyEventManager.postEvent( "SomeCoolEvent", "Data" );Do you have any sample code of you using it?