Sorry if the question may be a dumb one but i’m asking myself why we dont declare “ServerOnDie” as a “public static event” just like we did with “ServerOnBaseSpawned” and “ServerOnBaseDespawned”.
That way we could just call it in the GameOverHandler script via “Health.ServerOnDie” and could leave out the manual referencing every time we need to listen to that event.
It would be great if someone could help me understand this topic, as i’m currently stuck on this thought.
Hi there, good question.
For the base, each player has one base, and when it dies, the server can check which client’s base was destroyed and update the game state accordingly. Therefore it’s okay to make this static, as it is easy to separate the different player’s based on their network connection. In fact, this would be easier than searching the scene for all of the bases, and subscribing individually to each one’s OnBaseSpawned and Despawned event.
For the ServerOnDie event, this is being called by every single unit when they die. The scripts that subscribe to this are all scripts attached to the same unit as the health script. We only want to trigger these events on scripts for the unit that has died. The easiest way to do this is to get a hold of the instance of the health script directly, and subscribe to it. For example, the Unit script can look for the health script attached to the same gameObject and subscribe to its ServerOnDie event.
If we were to use a static event here, we would have to have a number of checks to ensure that the health script the event was being invoked from, was the correct one (i.e. the one on the same object).
Hope that helps!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.