A few questions about entity states

Paul Manta
  • A few questions about entity states Paul Manta

    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);
    }
    

  • 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.

Tags
c++ state entity-system
Related questions and answers
  • Alright so i'm making a vertical side scroller where you are an '8' character traveling downward while avoiding my randomly generated walls (the "generation" function) on the left and right sides... and traveling throug it. This is due to some odd reason for the "numb_collisions", which is used to count how many collisions have occured since initial collision (if it's 0 it means it IS the initial... saves the data into a vector which represents the console window. It is later used to print the "screen" to the console at a set rate (long double speed), as seen in the for loop after the map drawings

  • the Diffuse color variation or making it store any value that can change but making it so random that I woulnd't know how to relate the data to the other frames, or need a complex system for it. That's why...well... I'm building the animation system of my game engine (the skeletal and skinned animation stuff), and I came to a point where I added so much functionality in the frame and node structures... I was thinking about making smaller structures with the fTime variable each so I can have attribute keyframes, light keyframes and buffer keyframes, then make the node structure look like

  • every frame to handle input and render etc I call an UpdatePhysics method to update the physics simulation. void GameState::UpdatePhysics(unsigned int TDeltaTime) { World->stepSimulation(TDeltaTime... code shows the changes I made to get accurate physics. void GameState::createScene() { m_pSceneMgr->createLight("Light")->setPosition(75,75,75); // Physics // As a test we want a floor plane... the rigid body object btRigidBody *RigidBody = new btRigidBody(0, MotionState, Shape, LocalInertia); // Store a pointer to the Ogre Node so we can update it later RigidBody->setUserPointer((void

  • , Enemy, Powerup are NOT game classes. An entity is only defined by the components it owns (and that can change during time). So the player Airship starts with Sprite, Position, Health and Input components. A powerup has the Sprite, Position, BoundingBox. And so on. The main loop manages the game "physics", i.e. how the components interact each other: foreach(entity (let it be entity1...I'm writing a shooter (like 1942, classic 2D graphics) and I'd like to use a component based approch. So far I thought about the following design: Each game element (airship, projectile, powerup

  • () to call stored function that generates desired object. The position of the appropriate function in the vFactories is identical to the nObjectTypeID, so I can use indexing to access the function. So...Dear all, this is going to be tough: I have created a game object factory that generates objects of my wish. However, I get memory leaks which I can not fix. Memory leaks are generated by return new Object(); in the bottom part of the code sample. static BaseObject * CreateObjectFunc() { return new Object(); } How and where to delete the pointers? I wrote bool ReleaseClassType(). Despite

  • counting, it only counts when the resource is being read, (so the reference count may be 0, but an entity might still be keeping track of it's uid). It is also possible to mark resources for loading well... can't think of a decent scheme to use, or the right data-structures required to quickly manage them. Could someone who has implemented a system like this give an overview of how their's worked...I've decided I want to write a central ResourceManager/ResourceCache class for my hobby game engine, but am having trouble designing a caching scheme. The idea is that the ResourceManager has

  • To begin with I am developing a PSP application so I have no clue how to debug a PSP application. That doesn't mean I do not know how debug period. Anyway, when I run the game everything runs fine but when I press the X button on the PSP I see no bullet being drawn or updated on the screen. I know the problem doesn't lie with the pad because I press start and it exits my game so I do not think...(); missile-&gt;Init(true, s-&gt;X, s-&gt;Y); bullets-&gt;push_back(missile); } } void CleanUp() { for(unsigned int index = 0; index < bullets-&gt;size(); index

  • it has exposed a basic gap in my knowledge about what some of these lines of code do. should I just populate one big list from all the vectors and push that through? some need to be drawn... version of both. This is so the permanent ones can just be loaded in and are always drawn, and the temporary ones only get drawn for a frame. This way I can create moving lines for debugging, like...I want to include the drawing of other vectors of vertices in the following code, in a number of places it seems easy enough to simply add or multiply size()s of the two vectors, for instance

  • code in them... Right now for testing purposes, I just have the main loop call Level1 level1; and use the functions, but when I run the game I get a segmentation fault. This is the first time I've tried writing inherited classes, so I know I'm doing something wrong, but I can't seem to figure out what exactly. ...I'm trying to write my level classes by having a base class that each level class inherits from...The base class uses pure virtual functions. My base class is only going to be used as a vector

Data information