Skip to content

[Suggestion] lib.convar #91

@D4isDAVID

Description

@D4isDAVID

Is your feature request related to a problem? Please describe.
Nowadays, convars in FiveM are mostly calculated once, and never again after resources are already started.
Meaning when someone wants to change a resource's configuration using convars, most resources require a restart as the convars are only calculated when initially started.

A solution is available, called AddConvarChangeListener. However, it requires additional boilerplate.
Another issue is tables/objects/arrays, which are not natively supported by convar natives.

Describe the solution you'd like
My proposal is a new lib.convar API that allows developers to quickly define their convars, with a change listener added under the hood. In addition, a custom parser can be used to return values of various types.

Example proposal:

-- typical string convar
local strConvar = lib.convar.string('myResource:myStringConvar', 'default value')

print(strConvar.name) -- myResource:myStringConvar
print(strConvar.value) -- default value
print(strConvar.defined) -- false

ExecuteCommand('set myResource:myStringConvar "some custom value"')

print(strConvar.name) -- myResource:myStringConvar
print(strConvar.value) -- some custom value
print(strConvar.defined) -- true

-- other common types
local intConvar = lib.convar.int('myResource:myIntConvar', 123)
local floatConvar = lib.convar.float('myResource:myFloatConvar', 0.2)
local boolConvar = lib.convar.bool('myResource:myBoolConvar', true)

-- custom types
local tableConvar = lib.convar('myResource:myTableConvar', {
    foo = 'bar',
}, function(value)
    local tble = json.decode(value)

    if type(tble) ~= 'table' then
        error('bad type!')
    end

    return tble
end)

print(strConvar.value.foo) -- bar

ExecuteCommand('set myResource:myTableConvar "some custom value"') -- error parsing convar 'myResource:myTableConvar': bad type!

Additional context
I'm willing to implement this myself, but opening an issue beforehand for some feedback.

Here are a few links related to convars:

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions