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

ConfigAPI: Difference between revisions

From FiguraMC
PenguinEncounter (talk | contribs)
Heading.
Tag: 2017 source edit
PenguinEncounter (talk | contribs)
Heading capitalization
Tag: 2017 source edit
Line 56: Line 56:
end
end
</syntaxhighlight>
</syntaxhighlight>
== Data Types ==
 
== Data types ==
Only some data types are supported by ConfigAPI. All other data types will be converted to {{type|nil}}. The supported data types are:
Only some data types are supported by ConfigAPI. All other data types will be converted to {{type|nil}}. The supported data types are:
* {{type|nil}}
* {{type|nil}}
Line 65: Line 66:
* all types of {{type|Vector}}
* all types of {{type|Vector}}
* all types of {{type|Matrix}}
* all types of {{type|Matrix}}
== ConfigAPI Methods ==
== ConfigAPI methods ==


=== Configuration Switching ===
=== Configuration switching ===


==== setName ====
==== setName ====
Line 102: Line 103:
</syntaxhighlight>
</syntaxhighlight>


=== Reading and Writing Data ===
=== Reading and writing data ===
==== load ====
==== load ====
----
----

Revision as of 21:15, 28 September 2024

This API is only available on the host.

For more information, see Pings.

The ConfigAPI allows storing and loading data between reloads of your avatar.

ConfigAPI is available as the global config variable.

Tutorial

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")
local hatVisible = config:load("hatVisible")
local legLength = config:load("legLength")
local pantsColor = config:load("pantsColor")

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.

-- Check if host:isHost() to ensure that the config:save only runs for the host
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

Data types

Only some data types are supported by ConfigAPI. All other data types will be converted to nil. The supported data types are:

ConfigAPI methods

Configuration switching

setName


Also available as ConfigAPI:name.

Sets the name of the configuration file to read and write data to. By default, the name of the configuration file is the same as the name of the currently loaded avatar.

Arguments Return Type
setName(name string) self ConfigAPI
config:setName("my_config_file_name")

getName


Returns the name of the active configuration file.

Arguments Return Type
getName() string
-- Print the active configuration file's name.
print(config:getName())

Reading and writing data

load


Read a saved value from the active configuration file (see setName) by key and return it. If no key is given, return all of the saved keys and values in a table.

Arguments Return Type
load(key string) any | nil
load() table
-- Print all saved values.
printTable(config:load())
-- Print one specific value.
print(config:load("my_value"))

save


Associate a key with a value in the active configuration file (see setName.) Only some types of values are allowed; see Data Types for details.

If nil is provided as the value, then the key is removed from the file.

Arguments Return Type
save(key string, value any | nil) self ConfigAPI
-- Save a value to be used later.
config:save("my_value", "Figura Wiki")

Navigation

Documentation
Globals
APIs (Types)
Visuals (Types)
Other Types
Enums