Game Development Community

How do I use the TorqueEvent and TorqueEventManager?

by Victor Ferenzi · in Torque X 2D · 02/16/2009 (12:03 pm) · 3 replies

I have a method with the following signature:

public void Display(string eventName, string data)
{
}

In one of the methods in the class where this method lives I tried the following:

TorqueEvent<string> display = new TorqueEvent<string>("Display", false);
TorqueEventManager.Instance.MgrPostEvent<string>(display, string.Empty);

I placed a break point on the first line of the Display method and also place one on the display assignment.
When I run the code I break on the display assignment and as I walk the code not errors are thrown but the
breakpoint in the Display event is never reached either.

The only way I can get this to work is to do the following:

TorqueEvent<string> display = new TorqueEvent<string>("Display", false);
TorqueEventManager.Instance.MgrListenEvents<string>(display, Display, null);
TorqueEventManager.TriggerEvent<string>(display, string.Empty);

My question is should this have worked in the first set of code without creating the MgrListenEvents for this method?

The documentation does not give much on using these features of Torque X and I have search the forums with no luck for examples.

Any help would be appreicated.

Victor

#1
02/16/2009 (5:32 pm)
Victor, I don't think your first block should work. Display is an event listener and as such needs to be registered to listen for events which is what this line of code from your second block does:

TorqueEventManager.Instance.MgrListenEvents<string>(display, Display, null);

Generally speaking, with events you need a few things

1. The event that will be raised/listened for.
2. An event handler. You must register this to listen for the event from #1.
3. Something to raise the event.

Using your example we have:

// NOTE: this code won't compile like this.

// The event
TorqueEvent<string> display = new TorqueEvent<string>("Display", false);

// Register the listener
// Here we are telling the program to listen for the "display" events
// we created above and to call "Display" when it hears them.
TorqueEventManager.Instance.MgrListenEvents<string>(display, Display, null);

// Raise the event
TorqueEventManager.TriggerEvent<string>(display, string.Empty); 

// The listener
public void Display(string eventName, string data)
{
   // I Listern for eventName events!
}
#2
02/17/2009 (6:56 am)
Thanks Sean. The explaination you gave makes sense and it follows what I was thinking about events.

Some examples I have come across appeared to indicate that using the listener was not necessary to have the event fire.

Victor
#3
02/17/2009 (8:07 am)
No problem.

Quote:
Some examples I have come across appeared to indicate that using the listener was not necessary to have the event fire.

You don't need a listener to fire the event, but as you've experienced first hand, nothing will happen without one ;)