Following the State pattern, every state in my game is a derived object that reflects a particular segment of the game (MenuState, PlayingState, GameOverState, etc.)

Each of those state objects holds a container of objects pertinent to that world state (eg. menuButtonOBject for MenuState, playerObject/enemyObject for PlayingState, etc.)

For each frame, every object from that particular state makes a call to its update() method:

#include "GameState.h"
#include "GameObject.h"

class MenuState : public GameState {


    // member functions...
    virtual void update() { 
        foreach (object in objectContainer)

       std::vector<GameObject*> objectContainer;

My question being: what is the most common way to make the GameObject's update() method to have access to every other object in the state?

Eg: my veryBadassEnemyObject needs to know if the playerObject is within a particular x0,y0 -> x1,y1 rectangular area.

The straightest approach I can think of is simply passing the object container to the update function as a parameter, but I have the feeling that there is a much more professional, better way to do it.

  • 2
    \$\begingroup\$ Whatever data the creature needs could be "dependency injected" in the constructor. There are many ways information could be passed to an instance but I feel that if a specific object needs access to specific data, it should be injected in as a dependency during construction and or simply strongly coupled in the class. \$\endgroup\$
    – AturSams
    Commented Aug 28, 2014 at 12:09
  • \$\begingroup\$ Thanks for the tip. I'm passing in the constructor an object that holds a pointer to the object container as well and that will play the role of world observer - meaning that it can respond to specific queries from the game objects, for instance "am I seeing the player?" in the previous stated case of the enemy. I find this solution elegant and pretty straightforward. \$\endgroup\$
    – roymcclure
    Commented Aug 28, 2014 at 15:05
  • \$\begingroup\$ Sounds good. I will add this as an answer in case it resolve the issue. \$\endgroup\$
    – AturSams
    Commented Aug 28, 2014 at 15:10

1 Answer 1


If the game design requires that some instances of creatures gain flexible access to certain data through methods or POD, it may be useful to inject this data as optional parameters to the constructor. See dependency injection and/or the Strategy pattern.

For instance Sight, Hearing and smell could be instances that provide information about visibility of and or the sound other objects are making.

These could be used to compute the needed data of how creatures senses their environment. The advantage is that you can control the state of creatures dynamically.

For instance, to simulate blurred vision, you could hand over a Sight instance adds a random vector to the result.

If you always need the same functionality for all creatures, it is not unheard of to simply include it in the class as a static member.

Please note that I am not suggesting access to every other object and rather, to an ability object that has access to other objects' data and returns only the needed information. For instance, ability.whereIsPlayer(); //returns approximate player position or ability.whoIsVisible() // returns a collection of visible instances?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .