Jump to content
  • Advertisement
Sign in to follow this  
Hnefi

Global Initialization Lists in C++

This topic is 3758 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

Heyall. Some of my classes have certain static resources - typically models and such - that only need to be initialized once. I've put the initialization code into a static method, which is called upon initialization of a particular level. However, not all levels need to initialize all objects, if said objects are guaranteed not to appear in the level. Therefore, I need to let the level initialize (and deinitialize) only certain objects. Currently, I hard-code this into the initialize/deinitialize functions, but I don't want that. Is there a way to create a list of target classes in C++, on which I can then invoke static member methods? Would I need to use RTTI for it? Is this an issue that can typically be solved through a decent embedded scripting language (I'm considering GUILE at the moment)? Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
You could just have the members be static to the class, and initialize them this way:


// your.h
class Foo
{
private:

static Bar mybar;
};

// your.cpp
#include "your.h"

Bar Foo::mybar(myarg1, myarg2, myarg3);



Really though, your situation makes for a good argument for not having them be statically initialized. What exactly is your motivation for static initialization?

Share this post


Link to post
Share on other sites
Because if I have 100 instances of an entity, I certainly do NOT want to load models, textures and sounds for that type of entity 100 times.

I don't understand your example. Are you suggesting that I save instances of each type of entity in the level class? That feels like a very hackish solution; I'd rather only actually instantiate the objects I need.

Perhaps an example to demonstrate what I want is in order. This is what I have:
class World
{
Init()
{
Ship::Init();
Missile::Init();
Asteroid::Init();
}

DeInit()
{
Ship::DeInit();
Missile::DeInit();
Asteroid::DeInit();
}
};

What I want is instead something like this:
class World
{
Init(<list of references to classes, not instances>)
{
BOOST_FOREACH(ref, list)
ref::Init();
}

DeInit(<list of references to classes, not instances>)
{
BOOST_FOREACH(ref, list)
ref::DeInit();
}
}


I suppose I might have to instantiate the classes I want initialized, and send them to World in the manner above.

Share this post


Link to post
Share on other sites
I think I better understand your problem now. The common solution is to use resource managers. Entities hold pointers to assets that might can be shared.

pseudocode:


// create an entity:
Entity myEntity(modelManager.request("model.txt"),
textureManager.request("texture.png"),
soundManager.request("sound.wav");


// initialize your entity:
Entity::Entity(*model, *texture, *sound)
: m_pModel(model),
m_pTexture(texture),
M_pSound(sound)
{
// other initialization
}

// a manager.request() might look like:
Model* ModelManager::request(filename)
{
if(cache.find(filename) == true)
{
return cache[filename];
}
else
{
Model* model = new Model(filename);
cache.add(model);
return model;
}
}



People come up with all sorts of generic, templated, polymorphic implementations with smart pointers and other nice things.

Share this post


Link to post
Share on other sites
Hmmm... yes, a resource manager would probably be approprate. Then I wouldn't have to spread those static declarations around everywhere. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hnefi
Hmmm... yes, a resource manager would probably be approprate. Then I wouldn't have to spread those static declarations around everywhere. Thanks for the help.


Definitely a cleaner solution that trying to make everything static. There are a number of good threads on these boards about resource managers. Be sure to check them out :)

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!