Skip to content

Commit

Permalink
Add: [Script] Labels for negative values of a setting
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuXarick committed Feb 18, 2023
1 parent 420098a commit eaf65c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/script/api/script_info_docs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,16 @@ class ScriptInfo {
* 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.
Expand Down
9 changes: 8 additions & 1 deletion src/script/script_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit eaf65c4

Please sign in to comment.