Sign in to follow this  
sarim

Global Class Objects?

Recommended Posts

Im making a game engine and i need to be able to make my engine object available to all the other classes in the other source files.

I have the engine declared as so:

Engine engine;

How would i make "engine" global so that i could use it within other classes in other source files?

Share this post


Link to post
Share on other sites
You might want to read [url=http://archive.gamedev.net/reference/articles/article1798.asp]this article[/url].

Share this post


Link to post
Share on other sites
[quote name='KrNaGe' timestamp='1311190411' post='4838111']
Thats not really telling me how to fix my problem tho :P i need to make it global
[/quote]

Have you really read the article? It does cover your problem.

Share this post


Link to post
Share on other sites
[quote name='KrNaGe' timestamp='1311190411' post='4838111']
Thats not really telling me how to fix my problem tho :P i need to make it global
[/quote]

No you don't.


If an object requires to know the engine, then simply pass the engine to the object (or the appropriate subsystem). For example, imagine a class that represents a mesh. The mesh requires access to the rendering system when it is to be rendered, but instead of using one global engine, simply pass the engine either at creation time or during rendering to the mesh.

I wouldn't mind you using globals however this kind of attitude has corrupted our codebase at work, so I took it upon myself to haunt anyone promoting the use of globals (there are VERY view cases in which globals are THE way to go).

Share this post


Link to post
Share on other sites
[quote name='KrNaGe' timestamp='1311189561' post='4838104']
Im making a game engine and i need to be able to make my engine object available to all the other classes in the other source files.

I have the engine declared as so:

Engine engine;

How would i make "engine" global so that i could use it within other classes in other source files?
[/quote]


I have a class like this:

//GameCore.h
class GameCore
{
public:
void someNormalFunction()
void daDaDa()
.....
static GameCore* Get(); //This returns a global object
}

//GameCore.cpp

GameCore *nastyGlobal;

GameCore::Get()
{
if(nastyGlobal == NULL)
nastyGlobal = new GameCore()

return nastyGlobal;
}

//otherFile.cpp

....
GameCore::Get()->PlayGame();
GameCore::Get()->Stop();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this