Skip to content

Commit 687aba5

Browse files
authored
fix: float precision in config retrieval (#2889)
Description: This addresses the issue where floating point values retrieved from the configuration were not being rounded properly, leading to slight inaccuracies. This fix ensures that all float values are correctly rounded to two decimal places before being passed to Lua, thereby ensuring consistent behavior and data accuracy. Expected behavior: With the implemented changes, when floating point values are retrieved, they will now be rounded to two decimal places. For example, a configured value of `1.15` will correctly be returned as `1.15` in Lua scripts.
1 parent 5bcbc39 commit 687aba5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/lua/functions/core/game/config_functions.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,21 @@ int ConfigFunctions::luaConfigManagerGetBoolean(lua_State* L) {
7070
}
7171

7272
int ConfigFunctions::luaConfigManagerGetFloat(lua_State* L) {
73-
auto key = getNumber<ConfigKey_t>(L, -1);
73+
// configManager.getFloat(key, shouldRound = true)
74+
75+
// Ensure the first argument (key) is provided and is a valid enum
76+
auto key = getNumber<ConfigKey_t>(L, 1);
7477
if (!key) {
7578
reportErrorFunc("Wrong enum");
7679
return 1;
7780
}
7881

79-
lua_pushnumber(L, g_configManager().getFloat(key, __FUNCTION__));
82+
// Check if the second argument (shouldRound) is provided and is a boolean; default to true if not provided
83+
bool shouldRound = getBoolean(L, 2, true);
84+
float value = g_configManager().getFloat(key, __FUNCTION__);
85+
double finalValue = shouldRound ? static_cast<double>(std::round(value * 100.0) / 100.0) : value;
86+
87+
g_logger().debug("[{}] key: {}, finalValue: {}, shouldRound: {}", __METHOD_NAME__, magic_enum::enum_name(key), finalValue, shouldRound);
88+
lua_pushnumber(L, finalValue);
8089
return 1;
8190
}

src/lua/functions/core/game/config_functions.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ class ConfigFunctions final : LuaScriptInterface {
1717
static void init(lua_State* L);
1818

1919
private:
20+
/**
21+
* @brief Retrieves a float configuration value from the configuration manager, with an optional rounding.
22+
*
23+
* This function is a Lua binding used to get a float value from the configuration manager. It requires
24+
* a key as the first argument, which should be a valid enumeration. An optional second boolean argument
25+
* specifies whether the retrieved float should be rounded to two decimal places.
26+
*
27+
* @param L Pointer to the Lua state. The first argument must be a valid enum key, and the second argument (optional)
28+
* can be a boolean indicating whether to round the result.
29+
*
30+
* @return Returns 1 after pushing the result onto the Lua stack, indicating the number of return values.
31+
*
32+
* @exception reportErrorFunc Throws an error if the first argument is not a valid enum.
33+
*
34+
* Usage:
35+
* local result = ConfigManager.getFloat(ConfigKey.SomeKey)
36+
* local result_rounded = ConfigManager.getFloat(ConfigKey.SomeKey, false)
37+
*
38+
* Detailed behavior:
39+
* 1. Extracts the key from the first Lua stack argument as an enumeration of type `ConfigKey_t`.
40+
* 2. Checks if the second argument is provided; if not, defaults to true for rounding.
41+
* 3. Retrieves the float value associated with the key from the configuration manager.
42+
* 4. If rounding is requested, rounds the value to two decimal places.
43+
* 5. Logs the method call and the obtained value using the debug logger.
44+
* 6. Pushes the final value (rounded or original) back onto the Lua stack.
45+
* 7. Returns 1 to indicate a single return value.
46+
*/
2047
static int luaConfigManagerGetFloat(lua_State* L);
2148
static int luaConfigManagerGetBoolean(lua_State* L);
2249
static int luaConfigManagerGetNumber(lua_State* L);

0 commit comments

Comments
 (0)