2
\$\begingroup\$

I am creating an editor extension that includes an API for developers to use. It also includes events like (for example) "OnHealthChanged."

The problem is that events I have stumbled upon would require a user to subscribe to them, which is not what I actually want. I want developers simply type in the event handler in their script, and it would just work.

For example, I want a programmer to write a function "OnHealthChanged" in their own script. When the health changes, their code runs.

void OnHealthChanged() 
{
    //code that is going to run when health changes
}

Any idea where can I look?

I want to create something like "OnCollisionEnter()" event in Unity, which runs code every time a game object collides with another game object.

Hope it makes sense.

\$\endgroup\$
  • \$\begingroup\$ Elaborate your question more; I can't even understand what you want to do. \$\endgroup\$ – S. Tarık Çetin Jul 10 '16 at 8:36
  • \$\begingroup\$ How would you decide who will react to those events? There must some form of subscribing. Subscribing is actually what you call "type in the event" - i.e OnHealthChanged execute "that" method \$\endgroup\$ – Nikaas Jul 10 '16 at 9:42
  • \$\begingroup\$ Updated the question with more explanation \$\endgroup\$ – Elmar Talibzade Jul 10 '16 at 10:06
  • \$\begingroup\$ This doesn't sound out of line with what Unity does with its Message system — nothing magic. See Method 3 in this answer. The trick is that it's also not cheap. Searching through every object in the scene trying to send a message they might not even care about takes time. So, you will want a way to narrow down the set of potential recipients you message. If you give us more details about what your editor extension does and how developers are expected to use the API, we can suggest ways to target your messages to the code that needs them. \$\endgroup\$ – DMGregory Jul 10 '16 at 13:30
  • \$\begingroup\$ @DMGregory, I would've used a message system if I knew exactly what gameobject it would invoke. For this project, the only thing I know is that script would need a custom a namespace declared ("using NameSpaceName" bit). But I am not sure if I can narrow down the list based on that. So messaging system is a no go for me. I may need to use C# events and delegates, but I don't understand how can they be used. \$\endgroup\$ – Elmar Talibzade Jul 10 '16 at 17:05
1
\$\begingroup\$

If you require OnHealthChanged to be part of some interface of the game objects in question, you can attempt to cast any arbitrary game object to that interface and then call the method:

// ...given...
interface ISupportHealthChangedNotification {
  void OnHealthChanged();
}

var supportsHealthChanged = someGameObject as ISupportHealthChangedNotification;
if (supportsHealthChanged != null) {
  supportsHealthChanged.OnHealthChanged();
}

You could also use reflection to do this.

Reflection allows you to look at, among other things, all of the methods on a given type. Using this, you can find the OnHealthChanged method on any game object whose health you changed and invoke it:

// ...somewhere after you've changed the health...

var onHealthChangedMethod = GetType().GetMethod("OnHealthChanged");
if (onHealthChangedMethod != null) {
  onHealthChangedMethod.Invoke(this, null);
}

Note that doing this lookup constantly, every frame, is not necessarily the most super-efficient thing in the world so you may want to cache the MethodInfo once you find it.

Note that both of these methods require you to know about the game objects whose health is being changed, and either be in control of the health actually changing or for your code itself to be hooked up to some existing event that tells you the health changed (which you then forward via the above method). You cannot feasibly do this if you don't have hooks into the code that should stimulate the events you are interested in.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.