I'm in the fairly early stages of coding a game, and I've come across this problem a few times now.
Essentially, I run into situations where some object needs to access a container of all other objects of the same type (or of some other type!). For example, a Collider object needs to be able to check if it has run into any other Collider objects. Or a Unit needs to get a list of nearby enemy Units to be able to pick a target.
My question is where to keep these containers?
- Global? Globals are (almost) always bad in my experience, and I'm sure there's a better way than just having a global list of everything that anyone can access.
- Static member variable? This lets objects of one type access all of the same type, but it doesn't cover cases where one type needs access to other types (for example a Pathfinding object needing access to Colliders, or an AI needing a list of all Units)
- Manager classes? These would essentially be singletons then, and although I have very little experience with the SIngleton pattern I've heard nothing but bad things
I'm not sure of the best way forward. Every solution seems to have some serious holes and none of them seem particularly good.