Skip to main content

Dict

Observable

Inherits Observable

Writeable observable which refers to a dictionary of known key and value types.

Contains utility functions for observing and updating specific keys within this table, as well as cheaply observing specific keys within this table.

Constructor


Dex.Dict

Dex.Dict<K, V>(
    initialValue {[K]: V}
) -> Dict<K, V>

Creates a new Dict observable with the given initial value.

Functions

Set

Dict:Set(
keyK,
valueV
) → ()

Sets the value at a specific key.

Current

Dict:Current(keyK?) → V | {[KV]}

Gets the current value at a specific key, or the current value of the whole dictionary if no first argument is provided.

CAUTION

Currently, the value returned by StateRecord:Current() with no first parameter is mutable! Modifying this value directly may cause unexpected behavior!

Replace

Dict:Replace(newDict{[K]V}) → ()

Replaces the entire dictionary with a new value

Index

Dict:Index(keyK) → State<V>

Creates a new observable which observes only a specific key within the dictionary. Changes in other keys will not affect subscribers to this indexed state.

The returned observable is also a State object, and setting values in this state will set values in the original Dict object.

Example:

local playerCoinsDict = Dex.Dict({} :: {[Player]: number})

-- Use Index to observe states at a particular key. 
local player = game.Players.LocalPlayer
local ourCoins = playerCoinsDict:Index(player)

-- . . . Updates in dict will be reflected in ourCoins:
playerCoinsDict:Replace({ [player] = 100 })
print(coins:Current()) -- 100

-- . . . And vice versa!
coins:Set(42)
print(playerCoinsDict:Current()[player]) -- 42
Show raw api
{
    "functions": [
        {
            "name": "Set",
            "desc": "Sets the value at a specific key.",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "K"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "V"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 93,
                "path": "src/Observables/StateRecord.luau"
            }
        },
        {
            "name": "Current",
            "desc": "Gets the current value at a specific key, or the current value of the whole\ndictionary if no first argument is provided.\n\n:::caution\nCurrently, the value returned by `StateRecord:Current()` with no first\nparameter is mutable! Modifying this value directly may cause unexpected\nbehavior!\n:::",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "K?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "V | {[K: V]}"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 147,
                "path": "src/Observables/StateRecord.luau"
            }
        },
        {
            "name": "Replace",
            "desc": "Replaces the entire dictionary with a new value",
            "params": [
                {
                    "name": "newDict",
                    "desc": "",
                    "lua_type": "{[K]: V}"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 173,
                "path": "src/Observables/StateRecord.luau"
            }
        },
        {
            "name": "Index",
            "desc": "Creates a new observable which observes only a specific key within the\ndictionary. Changes in other keys will not affect subscribers to this\nindexed state.\n\nThe returned observable is also a State object, and setting values in this\nstate will set values in the original Dict object.\n\nExample:\n```lua\nlocal playerCoinsDict = Dex.Dict({} :: {[Player]: number})\n\n-- Use Index to observe states at a particular key. \nlocal player = game.Players.LocalPlayer\nlocal ourCoins = playerCoinsDict:Index(player)\n\n-- . . . Updates in dict will be reflected in ourCoins:\nplayerCoinsDict:Replace({ [player] = 100 })\nprint(coins:Current()) -- 100\n\n-- . . . And vice versa!\ncoins:Set(42)\nprint(playerCoinsDict:Current()[player]) -- 42\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "K"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "State<V>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 277,
                "path": "src/Observables/StateRecord.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Dict",
    "desc": "#### Inherits [Observable]\n\nWriteable observable which refers to a dictionary of known key and value\ntypes.\n\nContains utility functions for observing and updating specific keys within\nthis table, as well as cheaply observing specific keys within this table.\n\n## Constructor\n\n---\n\n### Dex.Dict\n```ts\nDex.Dict<K, V>(\n    initialValue {[K]: V}\n) -> Dict<K, V>\n```\n\nCreates a new [Dict] observable with the given initial value.",
    "tags": [
        "Observable"
    ],
    "source": {
        "line": 62,
        "path": "src/Observables/StateRecord.luau"
    }
}