Apologies if this is not correct channel to post. I don't know what would be the best way to ask this question, but I am working on a game engine and I have various node objects which can be added to a scene and for my editor which uses imgui I want to display these various nodes in a popup context window so that when you right click a list of nodes would appear. I'm curious if there is anyway for my code to be able to be aware or detect these classes and populate this list rather than me having to hardcode each one with a chain of if statements. Also, I know that code can't just magically be aware of a class I'd probably have to do something I'm just not quite sure what that would be.
C++ how coould I display classes/object in a imgui popup context?
The topic you're probably looking for is called “reflection”. It's the ability for the program to understand things about itself. There are lots of different ways to manage this. Some languages have it intrinsically as part of the language (C# is an example there I think), some don't. C++ appears to be trying to add something for a future standard. Sometime engines will add it on top of the language (this is how Unreal Engine works).
You can also write it on your own in some languages. C++ can use template and macro trickery to emulate it. There was a good thread here a number of years ago.
--Russell Aasland
--Principal Engineer
--Midsummer Studios
@steamdog I assume you know which objs are selected at any given time?
Add a string to the objects super class and append their name at the constructor, print that string in the box.
You can add all kinds of info this way, but just with this example you would be able to track the objects themselves and inheritance.
"No, you're never too old to rock and roll
If you're too young to die"
Extracting information from the actual classes is one approach, you can also create descriptions of node objects outside source code, and then use those descriptions. It can be as simple as a spreadsheet with eg 3 columns: class name, field type, and field name, like (prettified it for readability):
object , string, name
, int , level
position, real , x
, real , y
You can extract the node names and fields pretty easily from the data and use them in your editor. Similarly, it's not too complicated to generate a text like
class position {
public:
real x;
real y;
};
which looks suspiciously like a class definition that you could feed to C++ 🙂
Obviously, this idea is not new. The keyword here is meta-modeling, and there are tools available that already do this and more. I don't know if it exists in C++, but I know the Ecore meta-modeling effort that does this to generate the data backbone of Java applications. You can even graphically make class diagrams of your classes and the tools then generate a machine-readable meta-model description (that contains data like the above spreadheet and more), and/or generate code.
The Ecore project targets Java, but in the approach itself there is nothing inherent that limits it to Java only. I do also know existence of the PyEcore project, which aims to do the same kind of things for Python.
I wouldn't be surprised if efforts for C++ exist here as well.