Sign in to follow this  

Unity Accessing dll defined class members for user interface.

Recommended Posts

I'm trying to create a  C++ unity like plugin system where a user can define a class derived from a standard bass class (think MonoBehaviour) and add class variables to an inspector style menu (think public class variable). I can't pass along a pointer to a member variable from a undefined class so I'm wondering how the main program could be made aware of the dll defined class members. Is this even possible in C++?

 

Share this post


Link to post
Share on other sites
Posted (edited)

Sure. This is normally done by some kind of reflection system, which C# has by default but C++ does not. You'll have to build it yourself, and it will generally be far more involved than just "passing pointers to members" as you have noted. If you search for "C++ reflection" you'll find lots of links to various discussions, techniques, and experiments that you can either use directly or base your own system on.

 

What it fundamentally boils down to, at least for the context of your described problem, is providing a way for a class to expose or provide a list of "Property" objects which describe the appropriate exposed members of the class. A Property structure might look like, the bare minimum:

struct Property {
  std::string name;
  PropertyType type;
  std::size_t offset;
};

That is, it encodes the name of the property, its type (is it a float, a bool, a string, an integer?) and its offset (in bytes from the base address of an instance of the containing object). Generally you'd wrap up a collection of Property objects in some kind of Type object, and make sure your base class has a virtual method that returns the Type object corresponding to that object's type. Custom subclasses, in external DLLs, can override the virtual method and return a Type object that contains the Property objects that correctly describe it. These can then be parsed to build your inspector, or do other things.

Edited by Josh Petrie

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this