So,i'm having a bit of an issue with instantiating my gameobjects into to universe(My Scene object).
I can create an empty object from scratch and populate it from there with ease,my problem starts when i try to clone the gameobject which i just created.
My steps are
- I create an empty gameobject (GAMEOBJECT A)
- I add an component to it (COMPONENT A)
- Now i create a gameobject B from A
- GameObject B created,and the component A is in the component list of the gameobject B
- I try to get the component using inheritance but nullptr returns
It seems when i try to clone components,it returns only the base class pointer.There i try to recreate the derived class and add it to new gameobject's component list but obviously i fail.
- If i cant solve this with inheritance how can i solve it ?
- is there better way to implement such a desing ?
- or i just fail to use inheritance in c++.If it's, can you guys point out what am i doing wrong here
This is my GameObject AKA RGameElement
class RGameElement
{
protected:
vector<RElementComponent*> Elements;
public:
template <class T>
T* GetComponent() {
int elementSize = Elements.size();
for (int i = 0;i < elementSize;i++)
{
T* pr = dynamic_cast<T*>(Elements[i]);
if (pr != nullptr)
return pr;
}
return nullptr;
}
template <class T>
T* AddComponent() {
T* newElementComp = new T();
Elements.push_back(newElementComp);
return newElementComp;
}
void AddComponent(RElementComponent* targetComponent)
{
RElementComponent* newElement = new RElementComponent();
*newElement = *targetElement;
Elements.push_back(newElement);
}
/*void AddComponent(RElementComponent* targetComponent);*/
vector<RElementComponent*> GetComponent() const;
}
This is my Component AKA RElementComponent
class RGameElement;
class RElementComponent
{
public:
RGameElement* RootGameElement;
virtual RElementComponent GetReference() { return RElementComponent(); };
virtual void Setup() {};
virtual void StartLogic() {};
virtual void UpdateLogic() {};
}
and this my Instantiate via gameobject ref function
RGameElement* RUniverse::Instantiate(RGameElement* targetGameElement)
{
RGameElement* newElement = new RGameElement();
vector<RElementComponent*> components = targetGameElement->GetComponents();
int componentSize = components.size();
for (int i = 0;i < componentSize;i++)
{
newElement->AddComponent(components[i]);
}
Elements.push_back(newElement);
return newElement;
}
clonemethod. \$\endgroup\$