This is the first time I'm trying to make a 2D game, so I'm having quite a few difficulties in getting things right. Right now I'm trying to figure out exactly how the entity state machine should work.

1. I noticed entity states are... well, stateless. Basically, they just act on the data in the entity accordingly, they hold no data of their own. When an entity forwards events to its states it does it like this:

void Entity::handleEvent(const Event& event)
     // We pass this to the state so it could act upon the data in
     // the entity in a manner appropriate for the given event.
     state_->handleEvent(event, this);

That means that I don't really ever need more than one instance of a particular entity state; doing something like entity.setState(new StandState) is therefore highly wasteful. Then again, making each entity state a singleton hardly seems appropriate. What do you suggest I do?

(The same can be said about game states, so whatever the solution to this is, I guess it can be applied to game states as well.)

2. The state of the entity sprite. Most of the time, whenever the state of the entity changes, the sprite that is used to represent it must change as well. How should the entity representation be notified of the change in state?

Then again, should it be notified at all? It seem that if the representation needs to be made aware the entity has changed, then it (the representation) starts having an internal state. I would much rather have it stateless so that I require only one instance of every representation (just as before).

Edit: How the Entity and its sprite interact in my current setup.

class Entity
    void handleEvent(const Event& event);
    void render(Surface* surf, unsigned long currentTime);
    // void update(unsigned long currentTime);

    // The representation holds the actual sprite.
    EntityRepresentation* representation_;

void Entity::handleEvent(const Event& event)
    // The representation changes its internal state depending on the event.
    // This means a representation can only be used for a specific entity,
    // and thus may be wasteful.

void Entity::render(Surface* surf, unsigned long currentTime)
    representation_->render(surf, currentTime);

1 Answer 1


Well, for the first one, you could just make a state that has a static public member function handleMessage(), and then you don't really need to define any internal data. That would make states exceedingly cheap to allocate, as they would basically just be wrapped function pointers. For that matter, you could just have a global array of function pointers for those states (very C-ish way of handling this issue).

As for the second, it sounds like you have things backwards: the entity should notify the sprite, not the other way around. So, just have a setAnimationFrame() or whatever function exposed by the sprite, and let the entity call that as needed.

  • 1
    \$\begingroup\$ No. 1: Interesting solution, I'll consider it. :) | No. 2: You got me wrong, I never said the sprite should notify the entity. I'll edit my post to include some code to show how the entity and its sprite interact. \$\endgroup\$
    – Paul Manta
    Commented Apr 17, 2011 at 7:58
  • \$\begingroup\$ Ah, okay. We might want to drill a little further down into the EntityRepresenation bit here, because I'm not entirely sure I understand your problem. I'm also not sure that you want to have the entity representation have any sort of self behavior--rather, would you consider having the representation maintain the state of the entity only, and having a sprite or behavior object take that state as a parameter? \$\endgroup\$
    – ChrisE
    Commented Apr 17, 2011 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .