Need advice on how best to structure an engine

So I am currently writing a game engine using SFML for graphics. The program compiles and works fine on linux using g++, but doesnt compile properly on MSVC++ 2010 due to link errors. This was because I structured the program something like this:

http://i.imgur.com/XBx6uHF.png

To try and translate, I declared a bunch of game object types (ships & space stations & whatnot), then declared a few vectors of pointers to those object types which gets iterated through each frame with an update call by the game engine object. Unfortunately, the way that MSVC++ 2010 compiles, declaring global variables in a header file which gets included by two different cpps causes a link error, since the objects seem to be included in both object files when compiled. This also illustrates why global variables are bad...

So the simple solution for this would be to move these lists down into the game engine object, but that has a few undesirable consequences, namely that game objects cant iterate through the global lists of their types (ie, so I cant say, pass a string into a function, and have the function sort through the list and give me the object with that name). I would very much like to have that option available, but Im not sure what the best method for achieving it would be. Maybe I could pass the master list by reference at construction of each new game object & store it in each individual object as a copy by referenece?

Any advice on how best to work with this would be greatly appreciated.
game objects cant iterate through the global lists of their types
Well, this is just terrible design. Is this to implement some sort of scripting language?

For the time being, the immediate solution is that global objects are only declared in headers, and defined in a single source.
For example,
1
2
3
4
5
6
//Foo.h
//Note: no parameters to constructor.
extern Foo foo;

//Foo.cpp
Foo foo(bar, baz);
If your build procedure under Linux didn't complain about this, it's because you were doing something wrong.
Its meant for implementing something like this

1
2
3
4
5
6
7
8
Game_object* Game_object::Get_pointer_by_string(std::string input_string)
{   for(std::vector<Game_object*>::iterator it = Game_objects_list.begin(); it != Game_objects_list.end(); ++it)
   {   if((*it)->name == input_string)
        {   return (*it);
        }
   }

}


Obviously a brutal example, since it doesnt return anything if the string doesnt match...

So this would be handy if I wanted to go to some game object & set a target object for it to follow/attack/etc.
But I cant do that once I make these lists members of the game_engine class I mentioned earlier.

I have no idea why the build process on linux didnt cough up a hairball. I suspect it was because I simply compiled the main cpp file with g++, & #included all of the cpps into there, so that they were all compiled into the same object.

Im sorry, but you completely lost me with that example. What the heck is extern?

I follow what you mean about declaring global objects in headers defined in a single source, but I cant in this instance, since both the game objects & game engine cpp need to see the definitions for the game object types. I think Im stuck with it like this.
So this would be handy if I wanted to go to some game object & set a target object for it to follow/attack/etc.
I don't see why this logic belongs in the engine.

But I cant do that once I make these lists members of the game_engine class I mentioned earlier.
Sure you can. Your objects would have to ask the engine for other objects, rather than look for them themselves. It would be an overall better solution.

I simply compiled the main cpp file with g++, & #included all of the cpps into there, so that they were all compiled into the same object.
That would be it.

Im sorry, but you completely lost me with that example. What the heck is extern?
extern tells the compiler "there is a symbol with this name and type defined in some other translation unit".

both the game objects & game engine cpp need to see the definitions for the game object types
That's not possible. A compiler only needs a declaration to allow usages of a symbol.

I can make the example clearer if you give me an example of your code where you define a global.
Topic archived. No new replies allowed.