Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I would like to know the best way/best practice to handle the following situation. Suppose the project you are working on is split in two sub-projects:

  1. game engine lib
  2. editor gui.

Now, you have a method bool Method( const MethodParams &params ) that will be called during game-level initialization. So it is a method belonging to the game engine lib. Now, the parameters of this method, passed as a reference the structure MethodParams can be decided via the editor, in the level design phase. Suppose the structure is the following:

enum Enum1
{
    E1_VAL1,
    E1_VAL2,
};
enum Enum2
{
    E2_VAL1,
    E2_VAL2,
    E2_VAL3,
};

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

The editor should present a dialog that will let the user set the MethodParams struct. A text control for the field value. Furthermore, the editor needs to let the user set the fields e1 and e2 using, for example, two combo boxes (a combo box is a window control that has a list of choices). Obviously, every enum should be mapped to a string, so the user can make an informed selection (i have used E1_VAL1 etc.., but normally the enum would be more meaningful). One could even want to map every enum to a string more informative (E1_VAL1 to "Image union algorithm", E1_VAL2 to "Image intersection algorithm" and so on...).

The editor will include all the relevant game egine lib files (.h etc...), but this mapping is not automatic and i am confused on how to handle it in a way that, if in future i add E1_VAL3 and E1_VAL4, the code change will be minimal.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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