Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Tutorials/Persisting-Variables: Difference between revisions

From FiguraMC
Move from configapi
 
m Clarify host:isHost checks
 
Line 27: Line 27:
pings.updateFromConfig(hatVisible, legLength, pantsColor)
pings.updateFromConfig(hatVisible, legLength, pantsColor)
</syntaxhighlight>Finally, make some code that updates the config when you change values.<syntaxhighlight lang="lua">
</syntaxhighlight>Finally, make some code that updates the config when you change values.<syntaxhighlight lang="lua">
-- Check if host:isHost() to ensure that the config:save only runs for the host
-- The host:isHost() checks are not necessary, but they help explicitly show that their content is host-only
function pings.toggleHat(state)
function pings.toggleHat(state)
     models.model.root.Head.Hat:setVisible(state)
     models.model.root.Head.Hat:setVisible(state)

Latest revision as of 01:13, 7 January 2025

Avatars can use the ConfigAPI to make their variables persist across reloads. This can be used for things such as saving settings across avatars or saving information about an avatar's state.

To begin with, create a config file. This can be done with either a script or a /figura run command; it's safest to use a script in an IDE to have syntax highlighting and avoid errors.

config:setName("variablePersistence")
-- All keys must be strings!
config:save("pantsColor", vec(0.5, 0.8, 0.1))
config:save("hatVisible", true)
config:save("legLength", 5)

Run this script by selecting the avatar it's under. Once it's run, check the existence of the variablePersistence.json file at figura/config. If it's there, that code can be deleted. Next, make some code to read the data in that file when the script is loaded.

config:setName("variablePersistence")
-- The "or (value)" syntax sets the default value if the config:load is nil
local hatVisible = config:load("hatVisible") or true
local legLength = config:load("legLength") or 1
local pantsColor = config:load("pantsColor") or vec(0.9, 0.1, 0.9)

function pings.updateFromConfig(hat, leg, pants)
    models.model.root.Head.Hat:setVisible(hat)

    models.model.root.LeftLeg:setScale(1, leg, 1)
    models.model.root.RightLeg:setScale(1, leg, 1)
    
    models.model.root.LeftLeg["Left Pants"]:setColor(pants)
    models.model.root.RightLeg["Right Pants"]:setColor(pants)
end

pings.updateFromConfig(hatVisible, legLength, pantsColor)

Finally, make some code that updates the config when you change values.

-- The host:isHost() checks are not necessary, but they help explicitly show that their content is host-only
function pings.toggleHat(state)
    models.model.root.Head.Hat:setVisible(state)
    if host:isHost() then
        config:save("hatVisible", state)
    end
end

function pings.scaleLegs(scale)
    models.model.root.LeftLeg:setScale(1, scale, 1)
    models.model.root.RightLeg:setScale(1, scale, 1)
    if host:isHost() then
        config:save("legLength", scale)
    end
end

function pings.colorPants(rgb)
    models.model.root.LeftLeg["Left Pants"]:setColor(rgb)
    models.model.root.RightLeg["Right Pants"]:setColor(rgb)
    if host:isHost() then
        config:save("pantsColor", rgb)
    end
end