0
\$\begingroup\$

I'm writing a game engine with an editor that allows you to create individual components like sprites, fonts, and particles. Then you're able to combine these individual components into what I call an "Entity", to create something more complex and whole.

Currently you can only construct an Entity through code. You derive a new class off of an Entity interface and its derived members are the components you wish it to have (sprites, texts, etc). You can then implement whatever new logic this Entity is supposed to do.

I want to be able to create these entities in my editor, so you're able to simply read a file at run time that loads in all the required components and initializes them to specified values.

  • I can auto generate the c++ derived Entity class file, then deal with either auto importing it into the code project (or relying on the user to do so). I can directly generate the specified initialization code in the constructor. Makes for easiest access to components when implementing custom logic, and can ensure components' memory is contiguous for better cache.
  • Or simply have the Entity base class read the file and stores all its required components in a std::vector. Initializing the components will require a robust, hefty function for parsing the file and applying the specified values. Accessing the components when implementing your logic will also be a pain, either remembering their indices, or using string names in a map (bleh).

I like the first auto generating solution better, but I'm afraid of the maintenance and upkeeping it may require. Also I can't just keep overwriting their class source file so I'll need them to separate their custom logic in another source file. Would want a clean way to do that, and possibly a clean way to auto import the source code into the project (The tool knows the source directory, and I guess I could put the whole declaration/definition in a header file).

Any insight on which implementation? Have you ever auto generated C++ code before and regretted it? Or is there an entirely better way?

\$\endgroup\$
  • 2
    \$\begingroup\$ Note that you could also compile the new entity on its own into a dynamically-loadable binary that exposes a factory function to create an instance of the entity. \$\endgroup\$ – Quentin Aug 10 '17 at 14:45
1
\$\begingroup\$

I like the first auto generating solution better, but I'm afraid of the maintenance and upkeeping it may require. Also I can't just keep overwriting their class source file so I'll need them to separate their custom logic in another source file. Would want a clean way to do that, and possibly a clean way to auto import the source code into the project (The tool knows the source directory, and I guess I could put the whole declaration/definition in a header file).

As @Quentin points out, several AAA game engines basically allow you to load .dll / .so files in their editors at runtime to expose custom objects.

The one aspect you need to consider is precisely how you intend to expose the metadata for your objects to the engine and editor.

One game engine I know uses a custom pre-processing step that essentially reads the compile units and translates macros into injectable code and then passes the final pre-processed compilation unit to the compiler.

In another engine, they use a special method the engine calls to build a hierarchical representation of the class, its methods, and properties so that the editor knows what is publicly exposed and can be changed but also what can be manipulated via other objects through scripts, etc.

After having used both approaches, I actually prefer the latter approach. It has one less overall step during compilation, less tools maintenance, while being just as easy to maintain code wise as the former.

\$\endgroup\$
  • \$\begingroup\$ I am familiar with the first approach that Unreal uses via the generated headers to inject code used for reflection. However the second approach sounds pretty vague - what engine is this from? Can you link the respective documentation? \$\endgroup\$ – Christian Ivicevic Oct 13 '17 at 9:27
  • \$\begingroup\$ The second approach is from Lumberyard, which is a customized version of CryEngine. \$\endgroup\$ – Naros Oct 13 '17 at 14:28
  • \$\begingroup\$ Thanks for the mention - I am currently planning to expose data to the editor and I can't easily do that since I am running a C#-based editor that should have access to information from C++ headers. That's why I wanted to mimick the Unreal style but luckily you pointed me to anonther approach I can study before deciding. \$\endgroup\$ – Christian Ivicevic Oct 13 '17 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.