Jump to content

  • Log In with Google      Sign In   
  • Create Account


Managing Game Entities


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 KeyGames   Members   -  Reputation: 112

Like
0Likes
Like

Posted 31 August 2011 - 07:36 PM

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!

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 12431

Like
0Likes
Like

Posted 31 August 2011 - 09:02 PM

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.

#3 KeyGames   Members   -  Reputation: 112

Like
0Likes
Like

Posted 01 September 2011 - 10:12 AM

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.

#4 ApochPiQ   Moderators   -  Reputation: 12431

Like
0Likes
Like

Posted 01 September 2011 - 01:45 PM

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.

#5 KeyGames   Members   -  Reputation: 112

Like
0Likes
Like

Posted 01 September 2011 - 01:50 PM

I'm trying to get something like this:



#6 KeyGames   Members   -  Reputation: 112

Like
0Likes
Like

Posted 05 September 2011 - 02:09 PM

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 :)

#7 Sappharos   Members   -  Reputation: 140

Like
1Likes
Like

Posted 05 September 2011 - 06:17 PM

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.

#8 ApochPiQ   Moderators   -  Reputation: 12431

Like
1Likes
Like

Posted 06 September 2011 - 12:04 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS