Skip to main content

TableState

This class is used to observe and modify values in a TableManager easier. It is not feature complete and is subject to change.

Functions

new

TableState.new(
managerTableManager,
PathPath
) → TableState

Creates a new TableState. This is used to observe and modify values in a TableManager easier. Equivalent to tblMngr:ToState(Path)

local tbl = {
    Coins = 0;
    Inventory = {
        "Sword";
        "Shield";
    };
}

local tblMngr = TableManager.new(tbl)

local coinsState = TableState.new(tblMngr, "Coins")
print( coinsState == tblMngr:ToTableState("Coins") ) -- true

coinsState:Set(100) -- equivalent to `tblMngr:SetValue("Coins", 100)`

local inventoryState = TableState.new(tblMngr, "Inventory")
inventoryState:Insert("Potion") -- equivalent to `tblMngr:ArrayInsert("Inventory", "Potion")`
States with array values

You should avoid setting states to be a particular index in array because if the array is shifted then the state can potentially be pointing to the wrong value.

Set

TableState:Set(...any) → ()

Sets the value this state is associated with.

:Set(999) -- Sets the value itself to 999
:Set(1, 999) -- Sets the value at index 1 to 999 (State must be an array)

Get

TableState:Get(indexnumber?) → any

Gets the value this state is associated with. Takes an optional argument to specify the index of the array to get.

:Get() -- Gets the value itself
:Get(1) -- Gets the value at index 1 of the state (State must be an array) (Equivalent to :Get()[1])

Increment

TableState:Increment(...any) → any

Increments the value this state is associated with.

:Increment(999) -- Increments the value itself by 999
:Increment(1, 999) -- Increments the value at index 1 by 999 (State must be an array)

Insert

TableState:Insert(...any) → ()

Inserts a value into the array this state is associated with.

:Insert(999) -- Appends 999 onto the array
:Insert(5, 999) -- Inserts 999 at index 5 of the array

Remove

TableState:Remove(indexnumber) → any--

The removed value.

Removes the value at the given index from the array this state is associated with.

RemoveFirstValue

TableState:RemoveFirstValue(valueToFindany) → number--

The index of the removed value.

Removes the first value that matches the given value from the array this state is associated with.

Observe

TableState:Observe(fn(newany) → ()) → () → ()

Observes changes to the value this state is associated with. Also fires immediately. See TableManager:Observe for more information.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Creates a new TableState. This is used to observe and modify values in a TableManager easier.\nEquivalent to `tblMngr:ToState(Path)`\n```lua\nlocal tbl = {\n    Coins = 0;\n    Inventory = {\n        \"Sword\";\n        \"Shield\";\n    };\n}\n\nlocal tblMngr = TableManager.new(tbl)\n\nlocal coinsState = TableState.new(tblMngr, \"Coins\")\nprint( coinsState == tblMngr:ToTableState(\"Coins\") ) -- true\n\ncoinsState:Set(100) -- equivalent to `tblMngr:SetValue(\"Coins\", 100)`\n\nlocal inventoryState = TableState.new(tblMngr, \"Inventory\")\ninventoryState:Insert(\"Potion\") -- equivalent to `tblMngr:ArrayInsert(\"Inventory\", \"Potion\")`\n```\n:::warning States with array values\nYou should avoid setting states to be a particular index in array because if the array is shifted\nthen the state can potentially be pointing to the wrong value.\n:::",
            "params": [
                {
                    "name": "manager",
                    "desc": "",
                    "lua_type": "TableManager"
                },
                {
                    "name": "Path",
                    "desc": "",
                    "lua_type": "Path"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "TableState\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 61,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "_new",
            "desc": "",
            "params": [
                {
                    "name": "manager",
                    "desc": "",
                    "lua_type": "TableManager"
                },
                {
                    "name": "Path",
                    "desc": "",
                    "lua_type": "Path"
                }
            ],
            "returns": [],
            "function_type": "static",
            "private": true,
            "source": {
                "line": 68,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "Set",
            "desc": "Sets the value this state is associated with.\n```lua\n:Set(999) -- Sets the value itself to 999\n:Set(1, 999) -- Sets the value at index 1 to 999 (State must be an array)\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 113,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "Get",
            "desc": "Gets the value this state is associated with.\nTakes an optional argument to specify the index of the array to get.\n```lua\n:Get() -- Gets the value itself\n:Get(1) -- Gets the value at index 1 of the state (State must be an array) (Equivalent to :Get()[1])\n```",
            "params": [
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "number?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 126,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "Increment",
            "desc": "Increments the value this state is associated with.\n```lua\n:Increment(999) -- Increments the value itself by 999\n:Increment(1, 999) -- Increments the value at index 1 by 999 (State must be an array)\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 137,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "Insert",
            "desc": "Inserts a value into the array this state is associated with.\n```lua\n:Insert(999) -- Appends 999 onto the array\n:Insert(5, 999) -- Inserts 999 at index 5 of the array\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 148,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "Remove",
            "desc": "Removes the value at the given index from the array this state is associated with.",
            "params": [
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "The removed value.",
                    "lua_type": "any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 156,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "RemoveFirstValue",
            "desc": "Removes the first value that matches the given value from the array this state is associated with.",
            "params": [
                {
                    "name": "valueToFind",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "The index of the removed value.",
                    "lua_type": "number"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 164,
                "path": "src/tablemanager/src/TableState.lua"
            }
        },
        {
            "name": "Observe",
            "desc": "Observes changes to the value this state is associated with. Also fires immediately.\nSee [TableManager:Observe](TableManager.md#observe) for more information.",
            "params": [
                {
                    "name": "fn",
                    "desc": "",
                    "lua_type": "(new: any) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "() -> ()"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 172,
                "path": "src/tablemanager/src/TableState.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "TableState",
    "desc": "This class is used to observe and modify values in a TableManager easier.\nIt is not feature complete and is subject to change.",
    "source": {
        "line": 9,
        "path": "src/tablemanager/src/TableState.lua"
    }
}