1

Um.. First thing's first. I must let you know that I don't have core knowledge in C# scripting at all.

Alrighty, so here's my case. :)

Let's say I have a Script named AIEnemy with this snippet:

public delegate void CallbackOnEnemyAttack(int attackDamage);
public static event CallbackOnEnemyAttack OnEnemyAttack;

And I have a GameManager Script that sits on a one-and-only empty GameObject.

The case is... the AIEnemy Script is attached to lots of other GameObjects in the Scene. And I'm left wondering if the delegate AND the event that I declared in the script are of single instance or will be different per AIEnemy component attached to every GameObject.

I'm kind of a neat freak when it comes to game architecture even though I'm a noob. I'm super interested in delegates/events in Unity to design the architecture of my scripts better since I really don't like how Unity doesn't expose public instances of Interfaces in the Inspector that I have to do a "hacky" way to get around it by casting/tightly coupling my scripts together.

derHugo
  • 83,094
  • 9
  • 75
  • 115
ReGaSLZR
  • 383
  • 4
  • 17
  • 1
    as long as these aren't static callbacks they are different per instance. – Headhunter Xamd Aug 10 '17 at 12:32
  • Thanks, Headhunter :) I wasn't sure about that tho because I can't declare my delegates as statics. Any idea why? – ReGaSLZR Aug 10 '17 at 13:25
  • 1
    this is mostly because of what delegates are, https://stackoverflow.com/questions/6835766/why-can-a-net-delegate-not-be-declared-static#6835822 <- this answer gives a better explanation. – Headhunter Xamd Aug 10 '17 at 14:34

1 Answers1

3

The idea of making an event static in Unity is so that you can easily subscribe to it without needing an instance of it. This should be used in a script with one instance and this means it must be attached to one GameObject only.

That delegate and event code should be put in a script called EventManager or somthing similar. This EventManager script is a script that is attached to one empty GameObject. Your enemy and other scripts can subscribe to this EvenetManager when they are spawned and also receive notification when there is an event.

What happens when you attach this multiple GameObjects and now it has multiple instances?

If you trigger and event, each script you attached to a GameObject will send their own event. If this is attached to 500 enemies and you trigger event, all the 500 enemies will send an event to the subscriber.

In most cases, it is better to make these variables private and expose a function that can be used to subscribe and trigger events on them.

This has been done already. Here is a complete example of EventSystem from Unity that uses Unity's UnityEvent which is slow. Here is the ported version I made that uses delegate and should be used if possible.

Subscribing to an event:

EventManager.StartListening("Spawn", SomeOtherFunction);

Un-Subscribing to an event:

EventManager.StopListening("Spawn", SomeOtherFunction);

Triggering an event:

EventManager.TriggerEvent("Spawn");
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Oh, oh! Thank you so much for the answer and sample codes too. I will do that and apply something like an EventManager. :) – ReGaSLZR Aug 10 '17 at 13:23
  • 1
    You are welcome. If you want, you can also make the `delegate` take `GameObject` as parameter and you can use that to determine which Object sent that event. Although, it would still be called many times so I suggest you stick with EventManager in my answer which uses `Dictionary` to function. Happy coding! – Programmer Aug 10 '17 at 13:31
  • 1
    Uguuuu, thanks again. Bless youuuu! :) Yeah, I'll stick with the EventManager and just optimize the codes when the need arises. :3 – ReGaSLZR Aug 10 '17 at 13:36