Consider a game engine that utilizes a scene. Now, a scene can have a list of actors, which are the objects in the current scene. The pseudo for this might look like:
class Scene
{
public List<Actor> Actors { get; set; }
....
}
For the game loop, the engine calls Update() on the scene object, which then calls Update() on all of the actors.
Now, my question is this: What is a good method to allow actors to communicate (if actors need to be aware of other actors)? i.e. I might have an actor that represents a bullet. This bullet needs to be aware of all enemy actors (so that the bullet can kill them, etc.) I could provide a handle to each actor that references the current scene the actor is in, but that sounds like a bad idea (giving that kind of access/power to each actor).
I’d like to be able to keep all the actors blinded from other actors and the scene, if possible, and achieve behavior/communication through some contract, but I haven’t been able to come up with anything.
It depends on what you are trying to do. If your game engine is composition based, that may be a solution (I mean each game object is not defined by its class name but rather by the objects that compose it, and you can interact with those objects). You can also register them all in the Scene and be able to fetch them by name, or getting an array of existent by type (using template methods, for example). You can also use the Observer pattern: each game object implements an interface providing a service, and other game objects subscribe to that service, and are updated when the values of that other object is updated. There are tons of valid solutions, I’m just contributing with some more. It really depends on what you want to take out from the interactions between the objects.