mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-11 01:49:50 +00:00
Add: [Script] Labels for negative values of a setting
This commit is contained in:
parent
376820c0b6
commit
8351b97f52
@ -254,13 +254,16 @@ public:
|
|||||||
* user will see the corresponding name.
|
* user will see the corresponding name.
|
||||||
* @param setting_name The name of the setting.
|
* @param setting_name The name of the setting.
|
||||||
* @param value_names A table that maps values to names. The first
|
* @param value_names A table that maps values to names. The first
|
||||||
* character of every identifier is ignored and the rest should
|
* character of every identifier is ignored, the second character
|
||||||
|
* could be '_' to indicate the value is negative, and the rest should
|
||||||
* be an integer of the value you define a name for. The value
|
* be an integer of the value you define a name for. The value
|
||||||
* is a short description of that value.
|
* is a short description of that value.
|
||||||
* To define labels for a setting named "competition_level" you could
|
* To define labels for a setting named "competition_level" you could
|
||||||
* for example call it like this:
|
* for example call it like this:
|
||||||
* AddLabels("competition_level", {_0 = "no competition", _1 = "some competition",
|
* AddLabels("competition_level", {_0 = "no competition", _1 = "some competition",
|
||||||
* _2 = "a lot of competition"});
|
* _2 = "a lot of competition"});
|
||||||
|
* Another example, for a setting with a negative value:
|
||||||
|
* AddLabels("amount", {__1 = "less than one", _0 = "none", _1 = "more than one"});
|
||||||
*
|
*
|
||||||
* @note This is a function provided by OpenTTD, you don't have to
|
* @note This is a function provided by OpenTTD, you don't have to
|
||||||
* include it in your Script but should just call it from GetSettings.
|
* include it in your Script but should just call it from GetSettings.
|
||||||
|
@ -252,7 +252,14 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
|
|||||||
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
|
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
|
||||||
/* Because squirrel doesn't support identifiers starting with a digit,
|
/* Because squirrel doesn't support identifiers starting with a digit,
|
||||||
* we skip the first character. */
|
* we skip the first character. */
|
||||||
int key = atoi(key_string + 1);
|
key_string++;
|
||||||
|
int sign = 1;
|
||||||
|
if (*key_string == '_') {
|
||||||
|
/* When the second character is '_', it indicates the value is negative. */
|
||||||
|
sign = -1;
|
||||||
|
key_string++;
|
||||||
|
}
|
||||||
|
int key = atoi(key_string) * sign;
|
||||||
StrMakeValidInPlace(const_cast<char *>(label));
|
StrMakeValidInPlace(const_cast<char *>(label));
|
||||||
|
|
||||||
/* !Contains() prevents stredup from leaking. */
|
/* !Contains() prevents stredup from leaking. */
|
||||||
|
Loading…
Reference in New Issue
Block a user