Hello guys,
Me and my friend are trying to program a Entity Component system to get used to C++ and also to use it in our further projects. Below, you see that we have CEntity class which holds its Components in "std::unordered_map mComponents" and you see how we defined CComponents and inherited a InputComponent from it. In input component we have SetKey function which sets a variable inside.
We also defined System class which does its job in every period of time(every frame in game). Using query function in CEntity we find that there is a InputComponent in Entity and then using getComponentAdress we return the address of the component to a "CComponent* c". However, after that we cant use c->SetKey() function because it says "This function not defined for CComponet".
How should we solve this problem. What are the alternatives?
Thank you very much.
Oğuzhan D.
( Also I guess there are some design problems in terms of Entity-Component System implementation, ignore those for now :) )
One solution would be to make CComponent an abstract class and define SetKey as a pure virtual function in CComponent. You may not want to do this if other classes derived from CComponet would not have a SetKey function or SetKey would make no sense in those other derived classes.
Another solution is to cast the CComponent pointer to a ComponentInput pointer, but to do this, you need to be sure that what CComponent really points to is an object of type ComponentInput. You do seem to have allowed for this by retrieving entity by name.