Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by SpectralEdge · Feb 02, 2012 at 03:46 PM · eventaction

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?

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Feb 02, 2012 at 04:55 PM 0
Share

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.

avatar image SpectralEdge · Feb 02, 2012 at 07:26 PM 0
Share

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?

avatar image Jessy · Feb 02, 2012 at 07:52 PM 0
Share

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.

avatar image SpectralEdge · Feb 02, 2012 at 08:52 PM 0
Share

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.

avatar image Jessy · Feb 02, 2012 at 09:18 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Feb 02, 2012 at 04:57 PM 4
Share

The difficult-to-debug and nonperformant Broadcast$$anonymous$$essage is no substitute for what the OP wants to do.

avatar image SpectralEdge · Feb 02, 2012 at 05:15 PM 0
Share

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.

avatar image equalsequals · Feb 03, 2012 at 12:45 AM 2
Share
  • 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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges