Skip to main content

State

The State class is used to manage state for anything. It can be used to store data for the game, UI, or any other feature.

Usage example

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

local connection: RBXScriptConnection = state.Changed:Connect(function(newState, prevState)
	print(prevState, newState)
end)

state:Set("val", 11)

print(state:Length()) -- expect 3

state:Set(function(state)
	state.val = 111
end)

state:Set({
	val = 1111
})

state.val = 11111

state:Remove("val") -- Removes val from the state

state:Reset() -- Reset the state to the initial state

connection:Disconnect()

state:Destroy()

local success = pcall(function()
	state:Set("val", 1)
end)

print("state destroyed:", not success) -- expect: state destroyed: true

Signals

State.Changed :: Signal.Signal<newState: T, prevState: T> Signal that is fired when the state changes

Install with wally by adding the following to your wally.toml:

State = "dig1t/state@1.2.1"

Functions

new

State.new(
initialStateT--

Initial state values

) → State<T>

Creates a new state instance

Length

State:Length() → number

Returns the size of the state if the state is a table.

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

print(state:Length()) -- expect 3

Get

State:Get(
pathstring | number?--

The path to the value to get. Leave empty to get the entire state

) → any

Returns the value at the given path. The path parameter can only be used if the state is a table.

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

print(state:Get("val")) -- expect 1
print(state:Get("nest.val")) -- expect 123123

-- True as the argument returns the entire state
print(state:Get()) -- expect { val = 1, time = tick(), nest = { val = 123123 } }

Push

State:Push(
keyOrValueany,--

The key of the pushed value or the value itself if the state is an array

valueany?--

The value to push

) → any--

The key of the pushed value

Pushes a new value to the state.

The state must be a table to use this method.

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

state:Push("val", 2)

print(state:Get("val")) -- expect 2

Reset

State:Reset() → ()

Resets the state to the initial state

Example:

local state = State.new({
	val = 1
})

state:Set("val", 2)

state:Reset()

state:Get() -- expect { val = 1 }

Set

State:Set(
newStateT--

The new state value

) → ()

Sets the state with a new value.

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

state:Set({
	val = 1111
}) -- Change val to 1111

SetCallback

State:SetCallback(modifier(T) → ()) → ()

Sets the state with a new value.

The modifier function should NOT yield as it can cause issues or data loss. This function should be used as a shortcut to modify the state.

For example

local value = state:Get()

value.number += 1

state:Set(value)

Turns into:

state:SetCallback(function(context)
	context.number += 1
end)

Example:

type NumberState = {
	val: number
}

local state: State.State<NumberState> = State.new({
	val = 1
})

state:SetCallback(function(prevState: NumberState)
	prevState.val = 111
end) -- Change val to 111

SetPath

State:SetPath(
pathStringstring,--

The path to set

valueany?--

The value to set

) → ()

Sets the state with a new value

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

state:SetPath("nest.val", 11111) -- Change nest.val from 123123 to 11111

Remove

State:Remove(
pathstring | number--

The path to the value to remove

) → ()

Removes a value from the state

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

state:Remove("val") -- Removes val from the state

state:Get() -- expect { time = tick(), nest = { val = 123123 } }

Destroy

Cleanup
State:Destroy() → ()

Destroys the state

Example:

local state = State.new({
	val = 1,
	time = tick(),
	nest = {
		val = 123123
	}
})

state:Destroy()

local success = pcall(function()
	-- This should throw an error since the state object was destroyed
	state:Set("val", 1)
end)

print('state destroyed?', not success) -- expect: true
Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Creates a new state instance",
            "params": [
                {
                    "name": "initialState",
                    "desc": "Initial state values",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "State<T>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 100,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Length",
            "desc": "Returns the size of the state if the state is a table.\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nprint(state:Length()) -- expect 3\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 135,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Get",
            "desc": "Returns the value at the given path.\nThe path parameter can only be used if the state is a table.\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nprint(state:Get(\"val\")) -- expect 1\nprint(state:Get(\"nest.val\")) -- expect 123123\n\n-- True as the argument returns the entire state\nprint(state:Get()) -- expect { val = 1, time = tick(), nest = { val = 123123 } }\n```",
            "params": [
                {
                    "name": "path",
                    "desc": "The path to the value to get. Leave empty to get the entire state",
                    "lua_type": "string | number?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 167,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "_pushUpdates",
            "desc": "Fires the Changed signal with the new state",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "State<T>"
                },
                {
                    "name": "newState",
                    "desc": "",
                    "lua_type": "T"
                },
                {
                    "name": "prevState",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "static",
            "private": true,
            "source": {
                "line": 194,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Push",
            "desc": "Pushes a new value to the state.\n\nThe state must be a table to use this method.\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nstate:Push(\"val\", 2)\n\nprint(state:Get(\"val\")) -- expect 2\n```",
            "params": [
                {
                    "name": "keyOrValue",
                    "desc": "The key of the pushed value or the value itself if the state is an array",
                    "lua_type": "any"
                },
                {
                    "name": "value",
                    "desc": "The value to push",
                    "lua_type": "any?"
                }
            ],
            "returns": [
                {
                    "desc": "The key of the pushed value",
                    "lua_type": "any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 226,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Reset",
            "desc": "Resets the state to the initial state\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1\n})\n\nstate:Set(\"val\", 2)\n\nstate:Reset()\n\nstate:Get() -- expect { val = 1 }\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 258,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Set",
            "desc": "Sets the state with a new value.\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nstate:Set({\n\tval = 1111\n}) -- Change val to 1111\n```",
            "params": [
                {
                    "name": "newState",
                    "desc": "The new state value",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 286,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "SetCallback",
            "desc": "Sets the state with a new value.\n\nThe `modifier` function should NOT yield as it can cause issues or data loss.\nThis function should be used as a shortcut to modify the state.\n\nFor example\n```\nlocal value = state:Get()\n\nvalue.number += 1\n\nstate:Set(value)\n```\n\nTurns into:\n```\nstate:SetCallback(function(context)\n\tcontext.number += 1\nend)\n```\n\nExample:\n```lua\ntype NumberState = {\n\tval: number\n}\n\nlocal state: State.State<NumberState> = State.new({\n\tval = 1\n})\n\nstate:SetCallback(function(prevState: NumberState)\n\tprevState.val = 111\nend) -- Change val to 111\n```",
            "params": [
                {
                    "name": "modifier",
                    "desc": "",
                    "lua_type": "(T) -> ()"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 338,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "SetPath",
            "desc": "Sets the state with a new value\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nstate:SetPath(\"nest.val\", 11111) -- Change nest.val from 123123 to 11111\n```",
            "params": [
                {
                    "name": "pathString",
                    "desc": "The path to set",
                    "lua_type": "string"
                },
                {
                    "name": "value",
                    "desc": "The value to set",
                    "lua_type": "any?"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 371,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Remove",
            "desc": "Removes a value from the state\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nstate:Remove(\"val\") -- Removes val from the state\n\nstate:Get() -- expect { time = tick(), nest = { val = 123123 } }\n```",
            "params": [
                {
                    "name": "path",
                    "desc": "The path to the value to remove",
                    "lua_type": "string | number"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 422,
                "path": "src/State/init.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Destroys the state\n\nExample:\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nstate:Destroy()\n\nlocal success = pcall(function()\n\t-- This should throw an error since the state object was destroyed\n\tstate:Set(\"val\", 1)\nend)\n\nprint('state destroyed?', not success) -- expect: true\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Cleanup"
            ],
            "source": {
                "line": 478,
                "path": "src/State/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "State",
    "desc": "The State class is used to manage state for anything.\nIt can be used to store data for the game, UI, or any other feature.\n\nUsage example\n\n```lua\nlocal state = State.new({\n\tval = 1,\n\ttime = tick(),\n\tnest = {\n\t\tval = 123123\n\t}\n})\n\nlocal connection: RBXScriptConnection = state.Changed:Connect(function(newState, prevState)\n\tprint(prevState, newState)\nend)\n\nstate:Set(\"val\", 11)\n\nprint(state:Length()) -- expect 3\n\nstate:Set(function(state)\n\tstate.val = 111\nend)\n\nstate:Set({\n\tval = 1111\n})\n\nstate.val = 11111\n\nstate:Remove(\"val\") -- Removes val from the state\n\nstate:Reset() -- Reset the state to the initial state\n\nconnection:Disconnect()\n\nstate:Destroy()\n\nlocal success = pcall(function()\n\tstate:Set(\"val\", 1)\nend)\n\nprint(\"state destroyed:\", not success) -- expect: state destroyed: true\n```\n\n### Signals\n\n`State.Changed :: Signal.Signal<newState: T, prevState: T>`\nSignal that is fired when the state changes\n\nInstall with wally by adding the following to your `wally.toml`:\n```toml\nState = \"dig1t/state@1.2.1\"\n```",
    "source": {
        "line": 61,
        "path": "src/State/init.luau"
    }
}