| 1 | = !ConfigValueIncludes = |
| 2 | [[TracNav(TracNav/TOC_Development)]] |
| 3 | [[TOC]] |
| 4 | |
| 5 | == Description == |
| 6 | core/ConfigValueIncludes.h is a header file, defining several useful macros to create and modify config-values. |
| 7 | |
| 8 | Related pages: |
| 9 | * How to create a config-value: [wiki:howto/ConfigValue] |
| 10 | * How to use the config-file: [wiki:howto/ConfigFile] |
| 11 | |
| 12 | == Macros == |
| 13 | === SetConfigValue === |
| 14 | |
| 15 | '''SetConfigValue('''''varname''''', '''''defvalue''''')''' defines a new config-value with a default value. ''varname'' must be a member-variable of a class. ''defvalue'' is the default value that will be initially used until the user configuers another value. |
| 16 | |
| 17 | '''Important''': SetConfigValue '''must''' be used within setConfigValues(). This is a function that gets called to assign the configured values to an object. Additionally setConfigValues() '''must''' be called within the constructor of the class. |
| 18 | |
| 19 | It's possible to overwrite config-values in derived classes by just declaring the value again with SetConfigValue. |
| 20 | |
| 21 | You can describe a config-value by adding '''.description('''''string''''')''' at the end of the macro. |
| 22 | |
| 23 | You can set a callback function that gets called as soon as the config-value changes by adding '''.callback('''''this, &classname::functionname''''')''' at the end of the macro. |
| 24 | |
| 25 | === ResetConfigValue === |
| 26 | '''ResetConfigValue('''''varname''''')''' sets the given config-value '''and''' the value in the config-file back to the default value. ''varname'' is the name of the config-value. |
| 27 | |
| 28 | === ModifyConfigValue === |
| 29 | '''ModifyConfigValue('''''varname, modifier, ...''''')''' modifies a given config-value. ''varname'' is the name of the config-value. ''modifier'' is a function of [wiki:ConfigValueContainer]. By calling this macro, the specified function get's called in the container of the given config-value. ''...'' are arguments passed to the modifier-function. |
| 30 | |
| 31 | Possible modifier functions are: |
| 32 | * Simple config-values: |
| 33 | * '''set''', ''value'': Sets the config-value to a given value, changes the entry in the config-file and assigns the value to all existing instances of the values class. |
| 34 | * '''tset''', ''value'': Does the same like '''set''' but without changing the config-file (therefore the change will be lost if you restart Orxonox). |
| 35 | |
| 36 | * Vectors: |
| 37 | * '''set''', ''index'', ''value'': Does the same like '''set''' for simple config-values but only for a given element of the vector |
| 38 | * '''tset''', ''index'', ''value'': Does the same like '''tset''' for simple config-values but only for a given element of the vector |
| 39 | * '''add''', ''value'': Adds a new element with a given value to the vector |
| 40 | * '''remove''', ''index'': Removes a given element from the vector |
| 41 | |
| 42 | * Both: |
| 43 | * '''reset''': Sets the given config-value '''and''' the value in the config-file back to the default value (just like [wiki:ConfigValueIncludes#ResetConfigValue ResetConfigValue]). |
| 44 | * '''update''': Reloads the config-value from the config-file and assigns the value to all existing instances of the values class. |
| 45 | |
| 46 | == Example == |
| 47 | Definition of a class in the header-file: |
| 48 | {{{ |
| 49 | #!cpp |
| 50 | class MyClass : public BaseObject |
| 51 | { |
| 52 | public: |
| 53 | MyClass(); // Constructor |
| 54 | void setConfigValues(); // Inherited function |
| 55 | |
| 56 | const std::string& getName() |
| 57 | { return this->name_; } |
| 58 | |
| 59 | float getVersion() |
| 60 | { return this->version_; } |
| 61 | |
| 62 | private: |
| 63 | std::string name_; |
| 64 | float version_; |
| 65 | }; |
| 66 | }}} |
| 67 | |
| 68 | Implementation of the class source-file: |
| 69 | {{{ |
| 70 | #!cpp |
| 71 | MyClass::MyClass() |
| 72 | { |
| 73 | // Macro call to create an Identifier |
| 74 | RegisterObject(MyClass); |
| 75 | |
| 76 | // Function call to assign the config-values to the new object |
| 77 | this->setConfigValues(); |
| 78 | } |
| 79 | |
| 80 | void MyClass::setConfigValues() |
| 81 | { |
| 82 | SetConfigValue(name_, "Orxonox").description("The name of the game"); |
| 83 | SetConfigValue(version_, "1.0").description("The version-number"); |
| 84 | } |
| 85 | }}} |
| 86 | |
| 87 | Extract of orxonox.ini: |
| 88 | {{{ |
| 89 | [MyClass] |
| 90 | name_="Orxonox" |
| 91 | version_=1.1 // We have changed this value from 1.0 to 1.1 |
| 92 | }}} |
| 93 | |
| 94 | Some other code: |
| 95 | {{{ |
| 96 | #!cpp |
| 97 | MyObject orxonoxobject; |
| 98 | std::cout << "Name: " << orxonoxobject.getName() << std::endl; |
| 99 | std::cout << "Version: " << orxonoxobject.getVersion() << std::endl; |
| 100 | }}} |
| 101 | |
| 102 | Output: |
| 103 | {{{ |
| 104 | Name: Orxonox |
| 105 | Version: 1.1 |
| 106 | }}} |
| 107 | |