Managing Game Entities

Started by
6 comments, last by ApochPiQ 12 years, 7 months ago
Hello, GameDev community!

I'm currently confused on the best way to handle game entities. I've written classes before that could add new entities to a manager and stuff but I don't feel they are very efficient, and entity classes ended up being very large. My current game project is rather complex and I want an efficient way to manage 2D entities while keeping my framerate and logic update rate stable (currently running at 62.5 UPS with just Box2D's world step running). I was thinking about a component based system but I have no idea how to implement one and all the examples I've seen differed greatly, so I'm not sure which design I should go after.

Thanks!
Advertisement
Can you tell us more about the game you are making? A lot depends on that. What works well for one game may become horrifically unmanageable in another.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Hello, thanks for the response!

I'm making a sidescrolling platformer game with a storyline (scripting the AI and stuff) like Half Life 2 did. The game will have physics puzzles and destructible environments as well, so I'm using Box2D for physics since I'm each tile will have different collision boxes. (Basically each tile will have its own box2d polygon defined on it so i can make different shaped slopes, curves, etc.
Generally, entity/component systems are most useful when you have a fairly diverse range of types of "stuff" in your game world. The other extreme is that everything is more or less just different statistics fed into the same basic gameplay rules (like an RPG with only one player class) in which case you can do everything with a monolithic code design and a bit of data-driven programming.

It sounds to me like you're leaning closer to the latter moreso than the former, but I may just not have a clear enough idea of your plans yet.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I'm trying to get something like this:

I created classes for physics objects, but I think it could use some more optimization and cleaning up. One thing I don't like is the GetType() method and the types string comparison stuff. The code for updating, input, drawing, and other stuff for collisions is:

void World::OnContactBegin(b2Contact* pContact) {
void* userDataA = pContact->GetFixtureA()->GetUserData();
void* userDataB = pContact->GetFixtureB()->GetUserData();
PhysicsEntity* entityA = static_cast<PhysicsEntity*>(userDataA);
PhysicsEntity* entityB = static_cast<PhysicsEntity*>(userDataB);
if(entityA->GetType() == "PhysicsEntity" && entityB->GetType() == "PhysicsEntity") {
if(a)
entA->OnContactBegin(entB);
if(b)
entB->OnContactBegin(entA);
}
}


I have a class called Entity which contains the positions and sizes of the objects, a class called PhysicsEntity which inherits Entity and contains the box2d physics stuff, and a class World which contains the world simulation. This method calls all of the OnContactBegin() functions inside PhysicsEntity. If you want the full code for all of these classes to help me then I'd be happy to share it.

The thing I don't like is calling GetType() == "PhysicsEntity", each entity has a string type assigned to it and I assign it through the constructor of Entity(), and Entity has a GetType() method to return the string containing its class type, and it feels hacky to me and I'm wondering if there is a better way to do it.

Anyone willing to help me further optimize my entity management system please respond! Thanks :)

I created classes for physics objects, but I think it could use some more optimization and cleaning up. One thing I don't like is the GetType() method and the types string comparison stuff. The code for updating, input, drawing, and other stuff for collisions is:

void World::OnContactBegin(b2Contact* pContact) {
void* userDataA = pContact->GetFixtureA()->GetUserData();
void* userDataB = pContact->GetFixtureB()->GetUserData();
PhysicsEntity* entityA = static_cast<PhysicsEntity*>(userDataA);
PhysicsEntity* entityB = static_cast<PhysicsEntity*>(userDataB);
if(entityA->GetType() == "PhysicsEntity" && entityB->GetType() == "PhysicsEntity") {
if(a)
entA->OnContactBegin(entB);
if(b)
entB->OnContactBegin(entA);
}
}


I have a class called Entity which contains the positions and sizes of the objects, a class called PhysicsEntity which inherits Entity and contains the box2d physics stuff, and a class World which contains the world simulation. This method calls all of the OnContactBegin() functions inside PhysicsEntity. If you want the full code for all of these classes to help me then I'd be happy to share it.

The thing I don't like is calling GetType() == "PhysicsEntity", each entity has a string type assigned to it and I assign it through the constructor of Entity(), and Entity has a GetType() method to return the string containing its class type, and it feels hacky to me and I'm wondering if there is a better way to do it.

Anyone willing to help me further optimize my entity management system please respond! Thanks :)


At the very least you'll want to use numerical constants instead of strings. So use const int PHYSICS_ENTITY = 5 (or whatever); change the return type of GetType() and then test if(entityA->GetType() == PHYSICS_ENTITY... That way you're not doing array comparisons for each entity.

At the very least you'll want to use numerical constants instead of strings. So use const int PHYSICS_ENTITY = 5 (or whatever); change the return type of GetType() and then test if(entityA->GetType() == PHYSICS_ENTITY... That way you're not doing array comparisons for each entity.


Consider an automatic enumeration ("enum" in most languages) here instead of hand-coding all of the values.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement