Quick question on component inheritance

Get help with installing and running the Cafu Engine here. This forum is also for general questions and discussion of all aspects regarding the Cafu Engine.
Post Reply
SoulRider
Posts:95
Joined:2014-04-06, 00:16
Quick question on component inheritance

Post by SoulRider » 2014-07-30, 17:20

While I am not able to program, I thought I'd spend some time designing the game I am going to be building on my return. I am currently designing the component system, and had a quick question on component inheritance.

In my design, if something is alive (i.e. can die/be destroyed), it will have a Health component. The health component will have the elements - GetMaxHealth, SetMaxHealth, AddHealth, GetHeath, SetHealth, GetIsAlive, Kill. Internal Variables would be health, maxhealth, isalive. Obviously this could be expanded as required.

I will then add a sub-component for those Alive entities which have armor called Armor and add the elements - GetMaxArmor, SetMaxArmor, AddArmor, GetArmor, SetArmor. Internal variables would be armor, maxarmor.

My question is, If I added the Armor component to an entity, would the Health component be added by default, as it is part of the armor component? or would I still need to to add the Armor and Health components to the entity?
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Quick question on component inheritance

Post by Carsten » 2014-07-30, 22:07

Hi SoulRider,

are your questions specific to the Cafu C++ or Lua code, or generic about component systems?

While I guess that it would be possible to have "sub-components" in the sense of C++ class inheritance (this is how I understood your description), I would recommend to not do that. Components should have (as they're implemented in Cafu) a common base class, but components should (normally) not derive from each other.

(You will find a counter-example in Cafu (the light sources), but there inheritance is not used as it is normally used in C++. Instead it is used to "group" point light and radiosity light sources in the Map Editor's menu -- and I now think that this was not a particularly good idea, even if not harmful either.)

I'd suggest that you either make a single component that has both the Health and Armor properties next to each other (with possible 0 values if unused by a particular entity), or make two properties that are entirely independent of each other, one for Health and one for Armor. In the latter case, it would quite obviously be required for e.g. a script that changes health and armor (e.g. when the entity has been damaged) to check whether the processed entity actually has the Health and/or Armor components.
Best regards,
Carsten
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Quick question on component inheritance

Post by SoulRider » 2014-07-31, 16:26

My questions are regarding the specific Cafu implementation. I have got a good basic understanding of components in general, and here I am looking to tune my understanding of the Cafu implementation.

I think going for the separate health and armor components works best for me personally. I can just add a const bool to the armor component called hasarmor set to true. Then in code I can call with if entity.hasarmor then... In fact, that would work for me, and my personal logical style. If I understand components correctly, one of their strengths is that iterating over the component data is very fast.

The one problem that was mentioned was that you often end up with a huge number if statements in your game when if gets large, and apparently that causes quite an overhead. One person's solution to that was sub-components.

How do component parts talk to each other? In that, say health reaches 0, and the player needs to die. How does the health components Kill code tell the other components to perform their OnKill functions, such as deathanimations, camera position changes etc? Would I have an OnKill function in the script of every entity that has to die? Do I need to use a messaging system to allow them to communicate? How would that work in the example given above?

I'll admit my general understanding is hazy, so I may have slightly misunderstood some aspects, or not explained myself very well.

I am really excited, and although I am not currently working on code, I have designed 2 multiplayer games and 1 singleplayer game for the ideas I am working on. Along with a whole host of other game ideas, I am really hoping to gain a lot of development time by using the component system, and improving my general programming ability. :)
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Quick question on component inheritance

Post by Carsten » 2014-08-01, 09:36

SoulRider wrote: I think going for the separate health and armor components works best for me personally. I can just add a const bool to the armor component called hasarmor set to true. Then in code I can call with if entity.hasarmor then... In fact, that would work for me, and my personal logical style. If I understand components correctly, one of their strengths is that iterating over the component data is very fast.
There is no need for a hasarmor boolean: you can just check if the Entity has the Armor component instead.
The one problem that was mentioned was that you often end up with a huge number if statements in your game when if gets large, and apparently that causes quite an overhead. One person's solution to that was sub-components.
This doesn't sound right...
How do component parts talk to each other? In that, say health reaches 0, and the player needs to die. How does the health components Kill code tell the other components to perform their OnKill functions, such as deathanimations, camera position changes etc? Would I have an OnKill function in the script of every entity that has to die? Do I need to use a messaging system to allow them to communicate? How would that work in the example given above?
In Cafu, entity "behavior" is not scattered across components, but only implemented in one component.

That is, components such as Health, Armor, Model, Sound, ... are all not concerned with players spawning, walking, shooting or dying. Instead, there is a single "behavior" component. Although such a component could be implemented in C++ (e.g. for advanced AI), in our case, this is always a Script component, and the script implements the entity's behavior.

Have a look at the various scripts in Games/DeathMatch/Scripts to see several examples. They also show you how other components are found and used.

(As an advanced subject, components can also refer to each other, and update their references to "siblings" only when something in the common parent entity changes. Check out C++ method ComponentBaseT::UpdateDependencies() for details.)
I am really excited, and although I am not currently working on code, I have designed 2 multiplayer games and 1 singleplayer game for the ideas I am working on. Along with a whole host of other game ideas, I am really hoping to gain a lot of development time by using the component system, and improving my general programming ability. :)
Component Systems are definitively a very good opportunity for that! :up:
Best regards,
Carsten
Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests