- Home /
C# Action from Event
I have large sets of events (onMouseClick, OnButtonPress, OnBoxLoad, OnEnemyDie, etc)
And a large set of actions (LoadBox, ThrustForward, BlowUp, AddHealth, etc)
The goal is to allow the user to create their own bindings by adding an action to an event.
Perhaps something like KeyPressed += new EventHandler(LoadBox), where KeyPressed is an event, and LoadBox is an Action(Action )
I think this should work in theory, if I have a list of events and a list of actions and allow the player to pick an event and an action to add to its observers...but I can't get the code to work.
The closest I have gotten is:
public void AttachEventToFunction(Event TheEvent,EventArgs TheArgs, Action TheFunction)
{
TheEvent += new EventHandler<TheArgs>(TheFunction);
}
Which, Obviously, Does not work. How can I make this work? Is there a better way to go about what I am trying to do? Am I asking this in the wrong place?
Why can't you do "$$anonymous$$eyPressed += LoadBox;" and make sure that LoadBox takes the same arguments as $$anonymous$$eyPressed? If you're using (object sender, EventArgs e), that might be easier than trying to match Actions.
Because I still have to know what the action(method/function) is to put it in a list for connecting. Is there a simpler way to do that?
I don't understand what this "list for connecting" is. What you should probably do is take this to Stack Overflow. I'd like you to link to the SO question from here, though.
I want to have two lists, one with methods, one with events and allow the user to select an event from the event list and a method(action) from the method list and "connect" them by having the event trigger the method. Action is just a built in c# delegate for pointing to methods.
I know what an Action is. If the parameters of the event and Action match, you can use +=. The "new EventHandler" will be unnecessary. If they don't match, then you can wrap the Action in another function, like $$anonymous$$eyPressed += keyPressedArgs => LoadBox();. (Of course, you don't want an anonymous method if you're ever going to want to unsubscribe the Action from the event.) Again, this word "list" is confusing.
Answer by aldonaletto · Feb 02, 2012 at 04:15 PM
I don't know if this can help you, but it's possible to implement something similar to the an event handler with Component.BroadcastMessage .
You could create an OnEnemyDie(Transform enemy) event using something like this:
Transform root;
void SendEventOnEnemyDie(Transform enemy){ root.BroadcastMessage("OnEnemyDie", enemy, SendMessageOptions.DontRequireReceiver); } This will call the function OnEnemyDie(Transform enemy) in any script attached to the root object or to any of its children (DontRequireReceiver prevents runtime errors in scripts where the function doesn't exist).
Unfortunatelly, you must have a reference to the root object - there's no documented way to use it in all game objects at once (but I suspect that some undocumented method can do that).
A simple way to work around this is to child all objects to a common root object - if you're allowed to do that, of course.
The difficult-to-debug and nonperformant Broadcast$$anonymous$$essage is no substitute for what the OP wants to do.
That could work in a simple game, but it would be cumbersome and slow for the sheer number of events I need to hook in to.
to Jessy - I have found that Broadcast$$anonymous$$essage is only useful when you have a bunch of GameObjects floating out in space and you need them to do something without having reference to them. To me, it's really nothing short of a hack to make up for poor program architecture. Remember: Friends don't let friends use Broadcast$$anonymous$$essage()!
Your answer
Follow this Question
Related Questions
Is it possible to render a UnityAction field in a custom editor window? 0 Answers
When to use 'delegate', 'event' or 'Action' ? 1 Answer
Creating a Step-By-Step Event-Based Tutorial 1 Answer
Clear event System.Action list 1 Answer
"The event can only appear on the left hand side of `+=' or `-=' operator" 1 Answer