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?

more ▼

asked Feb 02, 2012 at 03:46 PM

avatar image

98 48 46 52

Why can't you do "KeyPressed += LoadBox;" and make sure that LoadBox takes the same arguments as KeyPressed? If you're using (object sender, EventArgs e), that might be easier than trying to match Actions.

Feb 02, 2012 at 04:55 PM Jessy

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?

Feb 02, 2012 at 07:26 PM SpectralEdge

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.

Feb 02, 2012 at 07:52 PM Jessy

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.

Feb 02, 2012 at 08:52 PM SpectralEdge

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 KeyPressed += 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.

Feb 02, 2012 at 09:18 PM Jessy
show all comments (comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Feb 02, 2012 at 04:15 PM

avatar image

61.5k 57 211 360

The difficult-to-debug and nonperformant BroadcastMessage is no substitute for what the OP wants to do.

Feb 02, 2012 at 04:57 PM Jessy

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.

Feb 02, 2012 at 05:15 PM SpectralEdge
  • to Jessy - I have found that BroadcastMessage 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 BroadcastMessage()!

Feb 03, 2012 at 12:45 AM equalsequals
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 02, 2012 at 03:46 PM

Seen: 4573 times

Last Updated: Jun 19, 2014 at 02:57 PM