Godot Version
Godot 4.7
Question
Hello! I have a question that’s more about broad coding practices rather than a specific Godot problem. I hope this is a good place to ask it! If it matters, I use C#, but most of this isn’t really language-specific as far as I can tell.
I’ve been reading a little bit about code architecture, which is a topic I haven’t learned about in any formal context, but I find it interesting, and it seems like it could be very important. A common idea that I’ve come across is that circular dependencies/references are something to be avoided. If class A refers to class B, class B should not also refer to class A. I’m starting a new Godot project, and I want to keep this principle in mind and avoid circular dependencies, but I’m finding it very difficult to wrap my head around. There are already multiple situations where I find myself having to jump through strange hoops to avoid them, and it feels like I’m making things more complicated for little, if any, gain. I understand that circular dependencies can cause memory leaks if one isn’t careful, as well as potential recursive errors, and they are apparently a sign of more deeply flawed design. I’ve also used C++ a little bit in the past, where circular dependencies are just not possible, because script build order determines what can reference what. I worry that I’m just following advice I read online without fully understanding it though, and that’s making things real messy.
Here’s one example. My game has turn-based combat, and has two classes called Actor and Ability. An Actor is a player character, enemy, or other participant in a battle, and an Ability is something an Actor can do on their turn. Abilities can target other Actors in different ways (an attack might only target enemies, while a buff can only target allies). My Actor class needs to hold a collection of Abilities, so that each Actor knows what it can do. The Ability class also needs some sort of GetTargets function, which takes a collection of all the Actors in the battle and returns the ones that are valid targets. But now the two classes have references to each other.
It seems like the simplest solution to this problem is to use interfaces. I make IActor and IAbility interfaces, which Actor and Ability respectively implement. Then Actor can hold a collection of IAbilities that it can use, and Ability.GetTargets can take and return a collection of IActors. Now Ability and Actor don’t directly reference each other. Unfortunately, it ends up being a lot trickier than that. IActor and IAbility cannot both depend on each other, since that would just be another circular dependency. This means either Actor’s collection of Abilities can’t be part of the IActor interface, or Ability.GetTargets can’t be part of the IAbility interface. Either way, if I start with an object of IActor type, there is no way to get to the set of targets of one of that actor’s abilities.
The problem gets worse when other systems need to interact with these classes, such as the UI. My plan is for them to communicate via an event bus, which is a solution I’ve used before and seen suggested many times for game dev, but is prone to circular dependencies. Let’s say I have an EventBus class. Actor and Ability both need to be able to send messages on that bus, so they need references to it. That means EventBus can’t have references to Actor and Ability themselves, so if I want to be able to send a message that includes one of those classes (such as “X actor has chosen to use Y ability”), I need the EventBus to pass those objects as IActor and IAbility objects. But again, either IActor doesn’t know what abilities it can use, or IAbility doesn’t know what targets it has, if not both. I’ve barely started the project, and I’m already in a convoluted mess of a situation where I kind of have two slightly different versions of most of my classes, and only one of those versions can actually do all the things it’s supposed to. And sure, I could cast an IActor to an Actor when I receive it from an event, but that feels like a pretty bad code smell in its own right. The whole point of an interface is that if you’re referring to one, you don’t care what specific implementation of it you’ve been given. If I decide I want to make an ActorV2 class later that also implements IActor, but I’m casting IActor to Actor all over the place, everything’s broken.
Another, more general example of the problem is in my state machine code. I’m more or less using StateMachine and State classes that I’ve used before in other projects. But as-is, StateMachine needs to reference State, so that it knows what state it’s in, and State needs to reference StateMachine, so that it can tell the machine when to transition to a different state. I can wrap both classes in interfaces to do something similar to what I did with Actor and Ability, but I just end up with similar problems again. I suppose State could just have a delegate for StateMachine.ChangeState rather than a reference to StateMachine itself, and that might be a good enough solution in this simple case, but it’s only going to get messier when my state machines start interacting with other systems.
I’m also skeptical that this interface solution actually does anything to prevent memory leaks. I still have object A holding a reference to object B, and B holding a reference to A, it’s just that they know each other by slightly different names. This is a frequently suggested solution to the problem of circular dependencies, so I assume there is some benefit to it, but it’s hard to see what that actually is. If I don’t actually understand the point of what I’m doing, I probably shouldn’t do it until I can figure that out.
Overall, it feels like I’m trying to slap a band-aid on my problem, and it just isn’t working very well. It seems like there are two reasonable solutions: “I’m overthinking all of this and circular dependencies aren’t that big a deal actually,” or “The structure of my code is fundamentally flawed and I need to rework it from the ground up.” I’m really struggling to think of completely different approaches to these problems, though. If my Ability class doesn’t know who it can target, where should that information be? If my Actor class doesn’t know what abilities it can perform, what class does? If I don’t have an Ability class at all, how do I encapsulate all the different choices a character has on their turn? Is the answer really to just ignore what seem to be common best practices, and keep my classes as tightly interconnected as they want to be? Maybe well-architected code really is this complicated, and I just need to get good and figure it out? I know these are big, general questions, but I could really use a nudge in the right direction. Thank you for reading all this, I appreciate it!
