SFML community forums

Help => General => Topic started by: p5ych0 on June 11, 2014, 04:47:37 am

Title: Entity Manager
Post by: p5ych0 on June 11, 2014, 04:47:37 am
I'm having trouble with managing entities; I'm always getting "vector iterator not incrementable ".

(below is some pseudo code)

class Game;
class Entity {
public:
    void update(sf::Time dt, Game& game) {
        game.addEntity(); //ERROR IS HERE
    }
};
 

class Game {
public:
    Game() : mEntities() {
        addEntity();
    }

    void addEntity(Entity* e) {
        mEntities.push_back(e);
    }

    vector<Entity*> mEntities;
};
 

When I try calling 'addEntity()' from a different class I get an error.  :-\
Title: Re: Entity Manager
Post by: math1992 on June 11, 2014, 06:07:30 am
It's more an C++ issue than a SFML one...

But

void addEntity(Entity* e)
 

is waiting for an Entity*, but you do not pass any in here

game.addEntity();
 

Title: Re: Entity Manager
Post by: p5ych0 on June 11, 2014, 06:10:09 am
Well I fixed that issue, and now I'm still getting the same error.
Title: Re: Entity Manager
Post by: math1992 on June 11, 2014, 06:13:19 am
Can I see what you did, I am pretty sure you did not fix it properly.
Title: Re: Entity Manager
Post by: p5ych0 on June 11, 2014, 06:33:04 am
Well in my Game.cpp I have methods for updating, rendering, etc.

void Game::update(sf::Time dt) {
    for (auto& it : mEntities)
        it->update(dt);
}

void Game::render() {
    for (const auto& it : mEntities)
        mWindow.draw(*it);
}

void Game::addEntity(Entity* e) {
    mEntities.push_back(e);
}
 
Title: AW: Entity Manager
Post by: eXpl0it3r on June 11, 2014, 08:09:18 am
Well compiler errors are a wonderful thing since they point you directly to the related code, unlike your post that just says that there's an error *somewhere*. ;)
Title: Re: Entity Manager
Post by: ElysianShadow on June 14, 2014, 03:26:46 am
Well in my Game.cpp I have methods for updating, rendering, etc.

void Game::update(sf::Time dt) {
    for (auto& it : mEntities)
        it->update(dt);
}

void Game::render() {
    for (const auto& it : mEntities)
        mWindow.draw(*it);
}

void Game::addEntity(Entity* e) {
    mEntities.push_back(e);
}
 

Not sure if I'm right or not, but shouldn't your ranged based loops have the following format instead of how you did it? :


     for ( Entity* e : mEntities )
     {
          /* do stuff */
     }

 

Or is that the same thing as what you already have?
Title: Re: Entity Manager
Post by: dabbertorres on June 14, 2014, 03:42:44 am
It's the "same". The auto keyword gets the correct type for you upon compilation.
Title: Re: Entity Manager
Post by: ElysianShadow on June 14, 2014, 03:44:41 am
It's the "same". The auto keyword gets the correct type for you upon compilation.

Oh thats right  ::) I was thinking of something else, but that makes sense
Title: Re: Entity Manager
Post by: p5ych0 on June 14, 2014, 04:58:55 am
Do you think it's because I add another entity in another entity's 'update' function, and the vector is getting invalidated?
Title: Re: Entity Manager
Post by: Nexus on June 14, 2014, 11:08:36 am
Well, this forum is actually for SFML-related problems. We occasionally answer other questions related to game development, but C++ language-specific questions shouldn't be the standard, there are better places for them (e.g. C++ forums, StackOverflow).

But if you ask here, you have to adhere to the forum rules, in particular you should post a minimal complete example that reproduces the problem (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368). You should also try with the debugger to find out as much as possible. This reduces the amount of guesswork on our side, and you will get a meaningful answer much faster.