Componet based entity system.

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Componet based entity system.

Post by wildrj »

Ive been reading around for about 3 days now and i just cant seem to grasp the entire idea or how id lay this type of structure out. I am currently using the traditional deep hierarchy system but its becoming a pain to further extend. Now Ive come across two systems but i don't think there fully correctly.

First one uses templates.

Code: Select all

HealthComponetTemplate testtemplate = new HealthComponetTemplate("testTemplate");
HealthComponet test = new HealthComponet();
test.setIntialHealth(test::leg , 100);
testtemplate.setComponet(test);

entity.setComponet(entitymanager.getTemplate("testTemplate").object);
The second system is just setting componets manually.

Code: Select all

HealthComponet test = new HealthComponet();
entity.setComponet(test);
The issue is if i want the entity to iterate through the list and call certain functions i cant "auto" cast so to say.

Now from what i understand is what id do is use the entity itself and use the attached componets functions.

Code: Select all

if(entity.getComponet(test).type == "healthComponet"){
((HealthComponet*)entity.getComponet(test))->setHealth(0);
}
Am i correct or is there something im missing?
http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Actually the casting stuff is bad!
bc that way u kinda kill the flexibility of the component system.
i would recomend a signal/slot system.
To do that your entity needs a map of signals like.

Code: Select all

class Entity
{
public:
          //Stuff
          irr::core::map<irr::core::stringc, Signal> Events;
          void SendEvent(const irr::c8* type, void* data);
};
Now each component registers With the entity events its functions for exaple the health component registers a event called "setHealth".
now if u want to set the health u call.

Code: Select all

int health = 50;
myEntity->SendEvent("setHealth", (void*)&health);
that will call all functions of components or anything else that registered to the entity. that way u need no casting or anything and it even works when the entity has no element with a setHealth function. its simply not responding then.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
ZCCdark203
Posts: 47
Joined: Fri May 15, 2009 3:26 pm
Contact:

Post by ZCCdark203 »

I agree with Sudi. If you're going to use a component-based entity system, it's certainly a good idea to also integrate a signal/slot system.

I use it in Sirrf. Although, in a slightly different way than Sudi described. Sirrf, for example, does use casting. The sigslot system is primarily being used for communication between entities and components.

But regardless of the way you're going to use such signal/slot system, it's a nice addition.
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Post by wildrj »

Alright signal slot is simple enough now how to implment "get"functions
such as getHealth()... boggles me.
http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

The method takes a non-const pointer to void, you can use it for input and output: cast to the appropriate type and write to the variable.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

No, no, take sigslot over that implementation. It's cleaner, multithread safe and type safe.

But to get stuff, you make a signature just the same way as if you had to return an error code. Except the return value is void for reasons explained in the documentation. So:

void Func (int& ValueToRetrieve, int* OrOtherWayToRetrieve, bool SomeArg);
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

why do u want to "get" health?
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

For display in the UI perhaps?
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Post by wildrj »

Yes for ui among other things.
http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

so u could make ur health component make fire a "updateHealth" event and the ui registers for that event. that way u don't need GET methods.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply