Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Entity Manager  (Read 3936 times)

0 Members and 1 Guest are viewing this topic.

p5ych0

  • Guest
Entity Manager
« 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.  :-\
« Last Edit: June 11, 2014, 05:08:24 am by p5ych0 »

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Entity Manager
« Reply #1 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();
 


p5ych0

  • Guest
Re: Entity Manager
« Reply #2 on: June 11, 2014, 06:10:09 am »
Well I fixed that issue, and now I'm still getting the same error.

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Entity Manager
« Reply #3 on: June 11, 2014, 06:13:19 am »
Can I see what you did, I am pretty sure you did not fix it properly.

p5ych0

  • Guest
Re: Entity Manager
« Reply #4 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);
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
AW: Entity Manager
« Reply #5 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*. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ElysianShadow

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Entity Manager
« Reply #6 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?

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Entity Manager
« Reply #7 on: June 14, 2014, 03:42:44 am »
It's the "same". The auto keyword gets the correct type for you upon compilation.

ElysianShadow

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Entity Manager
« Reply #8 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

p5ych0

  • Guest
Re: Entity Manager
« Reply #9 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Entity Manager
« Reply #10 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. 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.
« Last Edit: June 14, 2014, 11:14:24 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: