SFML community forums

Help => General => Topic started by: Canvas on June 21, 2014, 09:19:11 pm

Title: Trying to display text returns me an error
Post by: Canvas on June 21, 2014, 09:19:11 pm
Hey guys, I'm trying to display some text in my game but I keep getting the error displayed in the image I have attached coming up, the font is not null, nor the character size, nor the test or colour, what am I doing wrong here?

Here is the render code

//Objects and text
    for (int i = 0; i < DisplayObjects.size(); i++)
    {
        auto object = DisplayObjects.at(i);
        Window.draw(object->GetSprite());
        if (object->IsTextObject())
        {
            Window.draw(object->GetText());
        }
    }

Also here is the code I have which sets up the text
void DisplayObject::SetText(std::string setText)
{
        displayText.setFont(font);
        displayText.setColor(sf::Color(0, 0, 0));
        displayText.setCharacterSize(24);
        displayText.setString(text);
        text = setText;
        textObject = true;
}
Title: Re: Trying to display text returns me an error
Post by: Nexus on June 21, 2014, 10:07:08 pm
Please read this (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368).

And are you sure you want to copy the other object?
auto object = DisplayObjects.at(i);

Same applies for the string parameter. Also, at() is a really awful function to use during iteration. You should work with iterators, indices are for random access.
Title: Re: Trying to display text returns me an error
Post by: Canvas on June 21, 2014, 11:49:15 pm
How would i build an iterator over my vector? also the copying of the object was there just for debugging
Title: Re: Trying to display text returns me an error
Post by: Jesper Juhl on June 22, 2014, 12:07:11 am
for (const auto& object : DisplayObjects) {
}
Would be the simple option.

Or if you want iterators
for (auto it = begin(DisplayObjects); it != end(DisplayObjects); ++it) {
}
 

And there are more options.