Jump to content
  • Advertisement
Sign in to follow this  
Jaay_

C++ Angle bracket function

This topic is 661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I've been trying to add a component system to my game but I just came across a roadblock! I wanna make it so you can call something like this:

whatever.addComponent<UsersDefinedComponentDerivedFromTheComponentClass>();

and the add component function would do something like this:

void addComponent()
{
    components.push_back(derivedComponent);
}

Sorry, I know this is probably super simple but I'm quite new to coding in C++. Please help and thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

I'm sorry but I tried using templates but I couldn't seem to get them working. Could you give me an example?

Share this post


Link to post
Share on other sites
void addComponent(std::unique_ptr<BaseComponent> component) {
  components.push_back(component);
}
Probably something like that, perhaps with a different type of pointer, depending on how exactly you handle ownership.

Share this post


Link to post
Share on other sites

Or just do addComponent(BaseComponent* baseComponent). No need to use templates.

Edited by dpadam450

Share this post


Link to post
Share on other sites

Seems someone wants to build a copy of the Unity entity model :D

 

This is not part of the question nor the answer but in my opinion this is not the best concept someone could have when not writing another Unreal/Unity like ultimate all-round solution without source code access but that is marginal ...

 

To rely on the unity concept and what you have written you want to achive with

whatever.addComponent<UsersDefinedComponentDerivedFromTheComponentClass>();

you should realy realy take note of what the people above told you about taking pointers to derived classes. C++ is not C# and when instanciating a class to its base class you usuly need a pointer to it no way arround because without pointer there is no chance to store it in a generic data container like std::vector.

 

You know templates are compile time evaluated? So doing something in a template is then included fix into you compiled source code where a pointer is more dynamic when runtime generated.

 

So now to answer your question there is a way to do what you want in declaring an object of the class you wish to add from template type T with some restraints.

template<typename T> void addComponent()
{
    /**
     Your T needs a parameterless constructor here or you need a way to push a list
     of instanciating parameters to this code line
    */
    T* t = new T(); 

    /**
     Now you have t as T* what is your component simply add it to the list
    */
    components.push_back(t);
}

The problem here is to prevent memory leaks when not cleaning up components befor deleting them from the vector.

An alternative but more complicated approach is to declare the object static and only store individual sets of data just as

template<typename T> void addComponent()
{
    /**
     Your T needs a parameterless constructor here or you need a way to push a list
     of instanciating parameters to this code line
    */
    static T t; 

    /**
     Now you have &t as T* what is your component simply add it to the list
    */
    components.push_back(&t);
}

Because t is static here you wont need to clean up memory but and that is a big but, because it is static when changing one data field in one instance of T it is changed in all other instances too so you would need a way to store values outside of t. I take such static stuff for my JIT language (C#, Java) like object struct that itself can hold any data type of whatever is in my engine code and for the JIT reflection like meta system when calling a function via meta data.

 

I personaly would prefer the pointer using system to make an addComponent(ComponentBase* component) function :wink:

Edited by Shaarigan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!