1
\$\begingroup\$

I would like to know the best practices for handling the following situation:


Suppose the project you are working on is split in to two sub-projects; the game engine library, and the editor GUI.

You have the method bool Method( const MethodParams &params ) that will be called during game-level initialization; it is a method that belongs to the game engine library. The parameters of this method, passed as a reference to the structure MethodParams, can be decided via the editor in the level design phase. The MethodParams structure is as follows:

enum Enum1
{
    E1_VAL1,
    E1_VAL2,
};

enum Enum2
{
    E2_VAL1,
    E2_VAL2,
    E2_VAL3,
};

struct MethodParams
{
    float   value;
    Enum1   e1;
    Enum2   e2;
    // some other members
}

The editor should present a dialog that will allow the user to set the values in the MethodParams structure. The editor needs to present a text box for value, and let the user set the fields e1 and e2 using a combo box, for example. Every enum option should be mapped to a string, so the user can make an informed selection (normally, the names used in the enum would be more meaningful). One could even want to map every enum to a more informative string (so, for example, E1_VAL1 could map to "Image union algorithm", and E1_VAL2 could map to "Image intersection algorithm").


The editor will include all the relevant game engine library files (.h, etc.), but this mapping is not automatic, and I am confused by how to handle it in a way that where code change will be minimal, if I add further enum options in the future.

\$\endgroup\$
1
\$\begingroup\$

The absolute easiest way to map this is to use an std::map. That's what it's for.

std::map<Enum1, std::string> map_enum1;

map_enum1.insert<std::pair<Enum1, std::string>>(E1_VAL1, "Image union algorithm");
map_enum1.insert<std::pair<Enum1, std::string>>(E1_VAL2, "Image intersection algorithm");

This has several advantages:

  • Uses tried-and-tested tools.
  • No hacky precompiler trickery to map the values at compile-time.
  • Able to change values at run-time.
  • Easy to translate to other languages.

Adding new enums will continue to be difficult though, because it requires a recompile. However, if you step down from enums to simple int's, you can always add new values without requiring recompilation.

\$\endgroup\$
0
\$\begingroup\$

If you just need to map enumerations to strings, you can use the following question on SO: How to easily map c++ enums to strings.

\$\endgroup\$

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.