-1

When iterating through a list, the variables of the object in the list won't update, with debugging it looked like the variables only updated temporarily until the loop ended. Why is this? I've been looking for a very long time.

for (std::list<GUI>::iterator it = allguis.begin(); it != allguis.end(); ++it) {
    GUI gui = *it;
    speed = 5
   if (gui.activated) {
    gui.position.x = gui.position.x + gui.distanceX * speed;
    gui.position.y = gui.position.y + gui.distanceY * speed;
           }
   }                

And the GUI class:

class GUI
{
public:
    sf::Vector2f position = sf::Vector2f(0,0);
    int sizex;
    int sizey;
    bool activated;
    float rotation;
    int damage;
    float distanceX;
    float distanceY;

};
1

1 Answer 1

4

GUI gui = *it; creates a local variable initialized with a copy of the value stored in container. You should use a refence instead:

GUI & gui = *it;

Or you can use C++11-style loop:

speed = 5; // no point in updating it on every iteration
for(auto & gui: allguis)
{
    if(gui.activated)
    {
        gui.position.x = gui.position.x + gui.distanceX * speed;
        gui.position.y = gui.position.y + gui.distanceY * speed;
    }
}  

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.