Sign in to follow this  
diego_pmc

A few questions about entity states

Recommended Posts

diego_pmc    102
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.

[b]1. I noticed entity states are... well, stateless.[/b] 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:

[code]
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);
}[/code]


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

([i]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.[/i])


[b]2. The state of the entity sprite.[/b] 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).

Share this post


Link to post
Share on other sites
Zahlman    1682
[quote name='diego_pmc' timestamp='1303024118' post='4799401']
[b]1. I noticed entity states are... well, stateless.[/b][/quote]

That happens. It might not necessarily continue to be the case.

[quote]What do you suggest I do?[/quote]

Continue to use the objects normally. "Special cases aren't special enough". The fact that a bunch of objects contain no state does not [b]prevent[/b] you from using them like ordinary state-having objects, in much the same way that the fact that Granny Smith apples are green does not change the way that you eat them.

[b][quote]2. The state of the entity sprite.[/b] 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?[/quote]

How are you causing the state change? The simplest way is to have the update function [b]return the next state object[/b] (either itself, or a new one, or ...), and let the entity update a) call the state update; b) accept the new state and start holding onto it; c) make any other corresponding changes.

Although... have you considered that maybe the sprite should be a part of the state, rather than the entity itself? Or maybe it should not be held as data at all, but retrieved on demand when you render? Have you considered that rendering might be functionality provided by the state, as well 'updating'? That also allows you to handle things like, e.g. in certain states the sprite should blink and in others it should stay always visible.

[quote]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).
[/quote]

Don't try to arrange things like this. You are wasting effort in an attempt to waste more effort (justifying some kind of singleton registry setup that you then have to maintain), all so that you can re-use objects (which is of dubious value). If you worry about reuse, first target, you know, the actual image resources and stuff like that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this