---Most of the stuff I am talking about I read here---
I am currently tring to wrap my head around component based systems to make a simple game engine of sorts.
I am having a hard time understanding how its supposed to work. I get that there are components which all inherit from the same base, which provides basic functions (init, update, end) and lets all of the different kind of components be stored in the same array of pointers. I get that. However I dont get how any efficient interactions of the components in a game object are supposed to happen.
Lets say I made a component that stores the amount of gold in a treasure chest (lets call it dataComponent). Lets also assume I made a component that does something when its update function is called (lets call it logicComponent)
There is also a class called Entity
class entity{
private:
vector<component*> components;
public:
void addComponent(component* comp);
void removeComponent(component* comp);
void getComponent(int id);
}
Lets now say I plan to make treasureChest, without any functions. Its just a background model. Then I will make a new enitity and using addComponent I will add the dataComponent(and give it 100 gold pieces in its constructor) and a logicComponent(and give it nothing in its constructor). The player might take out, or put in as much gold as he likes. We dont know how much gold there is in the chest. There might even be multiple dataComponents storing how many jewels or weapons are in the chest.
Example:
void main(){
Entity chest;
chest.addComponent(new dataComponent<int>("gold",100)); //100 gold pieces
chest.addComponent(new dataComponent<int>("guns",2)); //2 weapons
chest.addComponent(new logicComponent()); //this is going to do the printing
updateEnt(Entity);//simply iterates through all components of the entity and calls thier update functions.
}
EXPECTED OUTPUT:
This chest contains 100 gold and 2 guns
The logic components job is to somehow get its hands on the data and print it to the screen.
My question is: How can the logic component get access to the other components in the same object as it is in and access the approperate ones (the ones it needs to function) without looking through the entire array every single time?
logicComponent
. You don't necessarily want a component to reach around and look at its entity and then figure out what components it has attached and grab data. That's a system's responsibility to have functionality, and systems don't process things in this scalar one-entity-at-a-time mindset. It is at least better to err on the side of components just being raw data, not logic. YourlogicComponent
probably wants to be a system of some sort. \$\endgroup\$ – user77245 Dec 12 '17 at 19:27