Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize Cvar Values #1214

Open
DolceTriade opened this issue Jul 2, 2024 · 2 comments
Open

Normalize Cvar Values #1214

DolceTriade opened this issue Jul 2, 2024 · 2 comments

Comments

@DolceTriade
Copy link
Contributor

If I'm I'm setting a boolean cvar and do:

]/set g_bot_level4 0
CVAR VALUE CHANGED: g_bot_level4 = 0 (1) 
]/g_bot_level4 
"g_bot_level4" - "0" - bool - whether bots use Tyrant - default: "on" 
]/set g_bot_level4 off
CVAR VALUE CHANGED: g_bot_level4 = off (1) 
]/g_bot_level4 
"g_bot_level4" - "off" - bool - whether bots use Tyrant - default: "on"

0 and off are equivalent values yet a string compare won't catch them as such when doing Cvar::GetValue.

I'm sure similar problems exist for other cvar types too (maybe floating point?).

Currently, all of our normalization logic is only when you call the new C++ API in the engine.

We should normalize all cvars on set. I propose we do this by modifying the semantics of OnValueChanged to also return a normalized value and store that instead of the direct value the user passed in.

@slipher
Copy link
Member

slipher commented Jul 3, 2024

We should probably solve the float round trip problem first. You need a complicated algorithm to normalize a float without either losing precision or producing very ugly strings

    std::string SerializeCvarValue(float value) {
        // You'd need %.9g to guarantee a round trip but with 8 or 9 digits of precision you get
        // stuff like 0.65f -> "0.649999976"
        return Str::Format("%.7g", value);
    }

For the case of reading cvar values in Lua, I would suggest adding more APIs like Cvar.getBool, Cvar.getNumber in addition to the current one which gets a string. We don't have to care about the type, if any, the cvar is registered as; just parse it as the requested type. Similar to RocketConditionalElement

@DolceTriade
Copy link
Contributor Author

Yes that's my workaround for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants