Jump to content
  • Advertisement
Sign in to follow this  
sarim

Global Class Objects?

This topic is 2559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Thats not really telling me how to fix my problem tho :P i need to make it global

Share this post


Link to post
Share on other sites

Thats not really telling me how to fix my problem tho :P i need to make it global


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

Share this post


Link to post
Share on other sites

Thats not really telling me how to fix my problem tho :P i need to make it global


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

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?



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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!