Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Entity manager loop opinions

This topic is 2866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This days I'm refactoring code and one of the things I want to improve is my entity manager code. More precisely, the update funcion where entities are updated. My engine is a 2D tile based engine that I'm using for an iphone game. The idea I had was to have the entity update splitted in several tasks because I'm willing to move to component based entities when I have the time. This is a Physic task, a collision detection task, AI task, etc...

For long time I have been using a simple update() function for my entity update that was called by the entity manager in a loop. Now I have come across another way to do it (in the end it is the same) but gives me some advantages like for example differnt ticks for the different tasks (Physics can run at a differnt refresh rate than collisions, etc...)

Here is the source code. I would be really happy if anyone could comment, give opinions, or improve it. Sorry ti is ObjC but I think it is pretty easy to understand:

//Run the update process o nall entities.
for(int i=0; i< numGameObjects;++i)
GameObject* go=_entities;

//If this game object was marked to be delted don't do any process with it.
//It will be released when update loop is finished.
if(! [go isToBeDeleted])
//Player does its physics (Movement, etc...)
//We save old position to let the onMapCollision know
//The previous position before physics were applied.
CGPoint oldPos=[go position];
[go doPhysics];

if([go collisionEnabled])
//Check collisions against map
CollisionMask collisionMask=[go collisionMask];
if(collisionMask & MapEntity)
bool collision=[self checkEntityVsMapCollision:go];
[go onMapCollision:oldPos];

//Check collisions against other entities (just the ones that have not been checked previously)
for(int j=i+1; j < numGameObjects; ++j)
GameObject* otherGO= _entities[j];
EntityType type=[go type];
if( (collisionMask & type) && [go collidesWith:otherGO])
[go onEntityCollision:otherGO];
[otherGO onEntityCollision:go];

//Execute AI for this game object
[go doLogic];

There could be some sintactical errors in the code but I just wanted to show the idea. Sorry for the long question, but I thought I had to explain everything as clear as I could.

Thanks in advance.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!