Dict
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
(
key:
K
,
value:
V
) →
(
)
Sets the value at a specific key.
Current
Dict:
Current
(
key:
K?
) →
V
|
{
[
K:
V
]
}
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
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