Circular Dependencies and Events

One thing that has always bothered me. Maybe I missed the explanation either in the RPG Core Combat course, or earlier in this one. but I feel a bit lost here. I’ve noticed throughout the course we’ve had multiple Classes calling upon functions within each other through the use of Events, the latest being:

TurnSystemUI using the TurnSystem GetTurnNumber() function.
TurnSystem using TurnSystemUI’s OnTurnChanged event.

In the prior mentioned course the big no-no in making the game scripts, was not to avoid circular dependencies such as these. Did I miss the explanation on using Events to circumvent the circular dependency issue somewhere?

Where are you seeing the TurnSystem interact with the TurnSystemUI?
The OnTurnChanged event is on the TurnSystem, not TurnSystemUI, there’s no instance where the TurnSystem touches anything on the TurnSystemUI script so I’m not sure what exactly you’re referring to.

Sounds like you might have placed the event in the wrong script, the TurnSystem is fully decoupled from the TurnSystemUI, if you were to delete that UI script everything should work fine.

2 Likes

pardon my confusion, I’m just not really understanding the How they are decoupled part. This is my 2nd course through GDTV and I just havn’t seen such usage with Events prior. So Events and how they decouple scripts from each other is a bit new to me.

I do see, now, what you mean about how if TurnSystemUI script is removed that TurnSystem will still work properly. If you had asked me to write this code out a couple weeks ago instead of using making the Event OnTurnChanged, my code probably would have looked a little more like this:

class TurnSystem
private void OnTurnChanged()
{
    TurnSystemUI.UpdateTurnText();
    TurnSystemUI.UpdateEnemyTurnVisual();
}

In this specific scenario, where OnTurnChanged() calls the TurnSystemUI functions directly, then yes, TurnSystem and TurnSystemUI would now become circular dependent on each other.

but with how you have it using OnTurnChanged as an Event. it prevents that circular dependency because the functions are still being held and called within the TurnSystemUI script itself.

honestly just typing all this out has helped me process and understand how Events work a little bit more, and even still I probably got it wrong but, basically to sum up my basic understanding is this:
Events give the programmer a way to make Decoupled & Indirect calling of functions outside of it’s base class. While this isn’t fool proof, and if written poorly, they could still end up with a circular dependencies, it is a method to help prevent such dependencies from occurring.

I made a dedicated video on Events a while ago, it may help you understand what they do to see more usages of it Code Monkey - What are Events? (C# Basics)

Yes if you called TurnSystemUI directly from TurnSystem then they would indeed be tightly coupled together, you could not run that code without one or the other script.
Whereas what we have here, the TurnSystemUI requires the TurnSystem however the TurnSystem does not require the TurnSystemUI, that’s loose coupling, you can run the code with both scripts or just the TurnSystem.

Remember events are not “calling” anything in the regular sense (like when you directly call a function). When the event is fired, the subscriber get notified that the event happened, but the publisher does not know what subscribers exist and doesn’t care what type they are. The publisher just shouts “this happened” and the subscriber listening to that event runs the listener function.

If you had an event defined in the TurnSystem and listened to on the TurnSystemUI
And then you also had an event defined in the TurnSystemUI and listened to on the TurnSystem
Then in that case even though you were using events they would still be tightly coupled together.

3 Likes

Events were pretty new to me too when I started this course, at least as far as actually using them in a project. I am finally starting to get my head wrapped around them. I think it’s taken me so long because I’ve seen other tutorials that switch between using C# events, and unity events, and messing around in the inspector, and using confusing naming conventions…and I just got confused, but I think it’s starting to click for me. I saw Code Monkey’s tutorial on events a while back but had not been looking for it for a particular project, and I didn’t practice with them at the time. I think events are just one of those things you need to practice with to fully realize how they work.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms