Advertisement Jump to content
Sign in to follow this  

Unity Instantiate class in external data without recompile all program (user C++ class)

This topic is 1034 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

I have a program that read and parse a xml file like that:
    <class name="ClassA" title="A" file="ClassA.cpp" />
    <class name="ClassB" title="B" file="ClassB.cpp" />
In this case my program do this:
    new ClassA("A");
    new ClassB("B");
But my program can instantiate any class. My program require just that ClassA.h, ClassA.cpp, ClassB.h, ClassB.cpp must be in directory "/external_data".
I think to do thing like that:
    map["A"] = &Factory::instantiate<A>;
    map["B"] = &Factory::instantiate<B>;
but I can't because my program is already compiled, and you can add any class name in the xml file.
The xml file serve to link ClassName with file in "/external_data"
I want to compile just the file in "/external_data", and not recompile all the program.
For example:
Unity do this (link C# script to gameObject)
Unreal do this (Add C++ class)
Have you got an idea to do this?
Thank you
Edited by racks

Share this post

Link to post
Share on other sites

There's a lot of ways of doing thing, some simple, some complicated.  A simple way would be to setup an interface for your objects, then have derived classes compiled into DLLs with some type of accessor.  Your main application only cares about the interfaces, it operates solely on those.  The DLLs have a way to create an instance of their derived classes and return the base interface.  The main application loads the DLL, calls the creation function, and gets a new instance of it's class as the base interface.  This lets you abstract the application from ever needing to know any of the implementations, and lets you add more implementations later.  With that, you could then specify the DLL/creation function in your XML, or even use a type of smart scan in the application.  Prescan the external directory for DLLs, find all DLLs with your unique creation function that accepts a class name, then when you need a class, iterate over the creation functions with the requested class and see if any of them return a new instance.  After that there are a lot of ways to optimize it (not having to iterate over all creation functions, not needing to keep the external DLLs loaded during runtime, etc), but that's up to how you want to architect the system.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!