# A few questions about entity states

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
{
public:
void handleEvent(const Event& event);
void render(Surface* surf, unsigned long currentTime);
// void update(unsigned long currentTime);

private:
// 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.
representation_->handleEvent(event);
}

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