Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling entities in a game
    text
    copied!<p>As a small exercise I am trying to write a very small, simple game engine that just handles entities (moving, basic AI etc.)</p> <p>As such, I am trying to think about how a game handles the updates for all of the entities, and I am getting a little bit confused (Probably because I am going about it in the wrong way)</p> <p>So I decided to post this question here to show you my current way of thinking about it, and to see if anyone can suggest to me a better way of doing it.</p> <p>Currently, I have a CEngine class which take pointers to other classes that it needs (For example a CWindow class, CEntityManager class etc.)</p> <p>I have a game loop which in pseudo code would go like this (Within the CEngine class)</p> <pre><code>while(isRunning) { Window-&gt;clear_screen(); EntityManager-&gt;draw(); Window-&gt;flip_screen(); // Cap FPS } </code></pre> <p>My CEntityManager class looked like this:</p> <pre><code>enum { PLAYER, ENEMY, ALLY }; class CEntityManager { public: void create_entity(int entityType); // PLAYER, ENEMY, ALLY etc. void delete_entity(int entityID); private: std::vector&lt;CEntity*&gt; entityVector; std::vector&lt;CEntity*&gt; entityVectorIter; }; </code></pre> <p>And my CEntity class looked like this:</p> <pre><code>class CEntity() { public: virtual void draw() = 0; void set_id(int nextEntityID); int get_id(); int get_type(); private: static nextEntityID; int entityID; int entityType; }; </code></pre> <p>After that, I would create classes for example, for an enemy, and give it a sprite sheet, its own functions etc.</p> <p>For example:</p> <pre><code>class CEnemy : public CEntity { public: void draw(); // Implement draw(); void do_ai_stuff(); }; class CPlayer : public CEntity { public: void draw(); // Implement draw(); void handle_input(); }; </code></pre> <p>All of this worked fine for just drawing sprites to the screen.</p> <p>But then I came to the problem of using functions which exist in one entity, but not in another.</p> <p>In the above pseudo code example, do_ai_stuff(); and handle_input();</p> <p>As you can see from my game loop, there is a call to EntityManager->draw(); This just iterated through the entityVector and called the draw(); function for each entity - Which worked fine seeing as all entities have a draw(); function.</p> <p>But then I thought, what if it is a player entity that needs to handle input? How does that work?</p> <p>I haven't tried but I assume that I can't just loop through as I did with the draw() function, because entities like enemies won't have a handle_input() function.</p> <p>I could use an if statement to check the entityType, like so:</p> <pre><code>for(entityVectorIter = entityVector.begin(); entityVectorIter != entityVector.end(); entityVectorIter++) { if((*entityVectorIter)-&gt;get_type() == PLAYER) { (*entityVectorIter)-&gt;handle_input(); } } </code></pre> <p>But I don't know how people normally go about writing this stuff so I'm not sure of the best way to do it.</p> <p>I wrote a lot here and I didn't ask any concrete questions, so I will clarify what I am looking for here:</p> <ul> <li>Is the way I have laid out/designed my code ok, and is it practical?</li> <li>Is there a better more efficient way for me to update my entities and call functions that other entities may not have?</li> <li>Is using an enum to keep track of an entities type a good way to identify entities?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload