Add: [Script] Labels for negative values of a setting

This commit is contained in:
SamuXarick 2023-02-04 14:24:10 +00:00 committed by rubidium42
parent 803c523735
commit fedb77a56d
2 changed files with 12 additions and 2 deletions

View File

@ -254,13 +254,16 @@ public:
* user will see the corresponding name.
* @param setting_name The name of the setting.
* @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
* is a short description of that value.
* To define labels for a setting named "competition_level" you could
* for example call it like this:
* AddLabels("competition_level", {_0 = "no competition", _1 = "some 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
* include it in your Script but should just call it from GetSettings.

View File

@ -252,7 +252,14 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
/* Because squirrel doesn't support identifiers starting with a digit,
* 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));
/* !Contains() prevents stredup from leaking. */