Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
}
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
1  
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. –  Paul Manta Apr 17 '11 at 7:58
    
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? –  ChrisE Apr 17 '11 at 9:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.