Several classes need to access the same data, where should the data be declared?

Posted by Juicy on Game Development See other posts from Game Development or by Juicy
Published on 2011-06-27T12:23:38Z Indexed on 2011/06/27 16:33 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

I have a basic 2D tower defense game in C++.

Each map is a separate class which inherits from GameState. The map delegates the logic and drawing code to each object in the game and sets data such as the map path. In pseudo-code the logic section might look something like this:

update():
  for each creep in creeps:
    creep.update()
  for each tower in towers:
    tower.update()
  for each missile in missiles:
    missile.update()

The objects (creeps, towers and missiles) are stored in vector-of-pointers. The towers must have access to the vector-of-creeps and the vector-of-missiles to create new missiles and identify targets.

The question is: where do I declare the vectors? Should they be members of the Map class, and passed as arguments to the tower.update() function? Or declared globally? Or are there other solutions I'm missing entirely?

© Game Development or respective owner

Related posts about c++

Related posts about architecture