# Entity manager loop opinions

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

## 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]; if(collision==true) [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.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013603
×