I have a class which I want to use a preLoad boolean to prevent it being loaded twice in my loop.
I do that like this:
Sprite::Sprite(std::string& imagefile)
{
if(!preload){
if(!texture.loadFromFile(imagefile)){
exit(2);
}
sprite = sf::Sprite(texture);
width = sprite.getLocalBounds().width;
height = sprite.getLocalBounds().height;
preload = true;
}
}
The problem is that preload seems to start off true rather than false. The variable is set in the class like so:
private:
bool preload;
But, I need to set it to false first but it won’t allow me to assign a false to it in the header. What can I do to set it to false initially?
use a static bool and set it in the cpp file.
in .cpp file
the static keyword will make all object share this same variable, allowing you to call a function just once for the entire life of a program.