The "perfect" Entity Class

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

The "perfect" Entity Class

Post by K-Bal »

Hey guys,

I'm breaking my head on a flexible, reusable, comfortable entity (base) class for game objects. My first thought was, that making members for health, etc. is not a flexible design. So I made a more generic system for that but it's current implementation seems messy to me.

Furthermore, I'm thinking of how action-callbakcs can be implemented in the best way. Signals and slots came to my mind. Any ideas on implementation would be great.

Please, just throw in your implementations and interfaces ;) Here is mine so far:

Entity.hpp:
http://pastebin.org/91506
Entity.cpp:
http://pastebin.org/91507

Ciao,
Marius
User avatar
GR33B
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Nov 14, 2008 3:33 pm
Current Project: Project Crimson
Programming Language of Choice: C#
Location: Virginia
Contact:

Re: The "perfect" Entity Class

Post by GR33B »

My entity class contains everything that my other classes had in common: a rectangle for holding position, height, and width, a unique ID int, etc. It should really just contain what all the classes that would inherit it already share.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: The "perfect" Entity Class

Post by K-Bal »

GR33B wrote:My entity class contains everything that my other classes had in common: a rectangle for holding position, height, and width, a unique ID int, etc. It should really just contain what all the classes that would inherit it already share.
That is the exact thing that I did not want to do ;) Imagine you want to reuse this class for a game where positions of objects are not measured in floats but in ints and they have no width and height. I'm trying to find a generic allround solution.
User avatar
GR33B
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Nov 14, 2008 3:33 pm
Current Project: Project Crimson
Programming Language of Choice: C#
Location: Virginia
Contact:

Re: The "perfect" Entity Class

Post by GR33B »

K-Bal wrote:
GR33B wrote:My entity class contains everything that my other classes had in common: a rectangle for holding position, height, and width, a unique ID int, etc. It should really just contain what all the classes that would inherit it already share.
That is the exact thing that I did not want to do ;) Imagine you want to reuse this class for a game where positions of objects are not measured in floats but in ints and they have no width and height. I'm trying to find a generic allround solution.
Well a floating point rectangle would solve that issue, as you could use the x and y coordinates for the position and disregard the width and height.

But if your game was to vary that much, I would just alter the entity class to fit that game's needs. It's not that realistic to want an entity class that will be ideal for any type of game, because there's so many different kinds of games and ways to create them. The entity class will be almost always game specific.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: The "perfect" Entity Class

Post by GroundUpEngine »

Dude I'm having the same issue aswell! I want my engine to be reusable as possible, but I'm not sure what to do with the Entity Class to keep it generic :cry:
However from looking at your code, I can see your on the money ;)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: The "perfect" Entity Class

Post by K-Bal »

GR33B wrote: Well a floating point rectangle would solve that issue, as you could use the x and y coordinates for the position and disregard the width and height.

But if your game was to vary that much, I would just alter the entity class to fit that game's needs. It's not that realistic to want an entity class that will be ideal for any type of game, because there's so many different kinds of games and ways to create them. The entity class will be almost always game specific.
It's not just about variation from game to game. I constantly encounter the situation of adding a new attribute for something in the game. That means: define a member, write getter and setter functions. I hate it. So with a generic attribute system and maybe script defined and signal/slot controlled action callbacks, I would not even have to inherit from Entity for specialization.
GroundUpEngine wrote:Dude I'm having the same issue aswell! I want my engine to be reusable as possible, but I'm not sure what to do with the Entity Class to keep it generic :cry:
However from looking at your code, I can see your on the money ;)
Thanks. Don't you think that this way of handling attributes is kind of messy/bug prone/pure gayness? ;)

Ciao,
Marius
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: The "perfect" Entity Class

Post by ismetteren »

This is just a thought, but what if every part of the game had its own interface. Like the render system would have a RenderAble interface, that classes that want to be rendered(Most entities, but maybe not all) would have to implement.
The implementations might still vary from game to game though, but might in many cases also be reusealbe.
I havent really thought this through, and i havent implemented it. Just a quick thought.
Image ImageImage Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: The "perfect" Entity Class

Post by GroundUpEngine »

GroundUpEngine wrote:Dude I'm having the same issue aswell! I want my engine to be reusable as possible, but I'm not sure what to do with the Entity Class to keep it generic :cry:
However from looking at your code, I can see your kinda on the money ;)
Fixed =)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: The "perfect" Entity Class

Post by K-Bal »

ismetteren wrote:This is just a thought, but what if every part of the game had its own interface. Like the render system would have a RenderAble interface, that classes that want to be rendered(Most entities, but maybe not all) would have to implement.
The implementations might still vary from game to game though, but might in many cases also be reusealbe.
I havent really thought this through, and i havent implemented it. Just a quick thought.
Sounds good, but I would make another class for this that inherits from Entity and from Renderable. Thus you have Logic encapsulated in Entity and Graphic in Renderable.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: The "perfect" Entity Class

Post by GroundUpEngine »

K-Bal wrote:
ismetteren wrote:This is just a thought, but what if every part of the game had its own interface. Like the render system would have a RenderAble interface, that classes that want to be rendered(Most entities, but maybe not all) would have to implement.
The implementations might still vary from game to game though, but might in many cases also be reusealbe.
I havent really thought this through, and i havent implemented it. Just a quick thought.
Sounds good, but I would make another class for this that inherits from Entity and from Renderable. Thus you have Logic encapsulated in Entity and Graphic in Renderable.
That a pretty neat idea! It's kinda similar to SFML's sf::Drawable
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: The "perfect" Entity Class

Post by avansc »

ummm...

how about just having one SetValue function, passing it a void*, and then just using some logic inside to cast it properly.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: The "perfect" Entity Class

Post by K-Bal »

avansc wrote:ummm...

how about just having one SetValue function, passing it a void*, and then just using some logic inside to cast it properly.
Yep, I already thought of this. Would require the user to remember the types of all attributes. Don't if that is a good thing but I guess the generalization just has it's costs. I also thought about having an attribute base class and derived specializations but I could not think of a useful interface.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: The "perfect" Entity Class

Post by avansc »

take a look at boost::any or cast_any something like that.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: The "perfect" Entity Class

Post by K-Bal »

Thanks, that boost::any looks like it was especially made for my situation ;)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: The "perfect" Entity Class

Post by K-Bal »

Just integrated boost::any in about one minute. Here is the most generic entity class I've seen so far ;)

http://pastebin.org/94537

I would not derive from it unless you need additional functionality. Just rewrite the Update/Draw/Constructor functions to suit your needs.

Also consider using a (std::/boost::)unordered_map instead of std::map if you are saving a lot of attributes.

Marius
Post Reply