Hello there
I have myself a custom TextObject class and I have myself a GameState class which has a vector of TextObjects, so basically this is how I am populating my vector
TextObject textObject;
TextObjects.push_back(textObject);
TextObjects.push_back(textObject);
TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24, fontLoader.Fonts[FontNames::Main_Font]);
TextObjects.at(0).CentreText();
TextObjects.at(1).Setup(200, 100, "Game Over", 48, fontLoader.Fonts[FontNames::Main_Font]);
TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
To me this doesn't look quite right but it does work, but I have myself in a pickle, sometimes the player may get a new highscore and if they do I want to show some more text, so I have my code like so
TextObject textObject;
TextObjects.push_back(textObject);
TextObjects.push_back(textObject);
TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24, fontLoader.Fonts[FontNames::Main_Font]);
TextObjects.at(0).CentreText();
TextObjects.at(1).Setup(200, 100, "Game Over", 48, fontLoader.Fonts[FontNames::Main_Font]);
TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
if (highScore == true)
{
TextObject txtObject;
TextObjects.push_back(txtObject);
TextObjects.at(2).Setup(200, 3, "New high score!", 24, fontLoader.Fonts[FontNames::Main_Font]);
}
But when I go to render my third text item in my vector I get this error "Unhandled exception at 0x5361DCE2 (sfml-graphics-d-2.dll) in SpaceDestroyer.exe: 0xC0000005: Access violation reading location 0xCDCDCDD0." in xtree "_Nodeptr _Pnode = _Root();"
My TextObject has a standard constructor so even If I try
if (highScore == true)
{
TextObjects.push_back(TextObject());
TextObjects.at(2).Setup(200, 3, "New high score!", 24, fontLoader.Fonts[FontNames::Main_Font]);
}
I still get the error, what is the correct way for my to populate my vector? or is there a better way to store objects than using a vector?