State
This class wraps a value in getters and setters, its main purpose is to allow primatives to be passed as objects. Constructors for this class are available in Iris
local state = Iris.State(0) -- we initialise the state with a value of 0
-- these are equivalent. Ideally you should use `:get()` and ignore `.value`.
print(state:get())
print(state.value)
state:set(state:get() + 1) -- increments the state by getting the current value and adding 1.
state:onChange(function(newValue)
print(`The value of the state is now: {newValue}`)
end)
CAUTION
Never call :set()
on a state when inside the :onChange()
callback of the same state. This will cause a continous callback.
Never chain states together so that each state changes the value of another state in a cyclic nature. This will cause a continous callback.
Types
State<T>
type
State<T> =
{
value:
T
,
get:
(
self
)
→
T
,
set:
(
self
,
newValue:
T
)
→
T
,
onChange:
(
self
,
callback:
(
newValue:
T
)
→
(
)
)
→
(
)
,
ConnectedFunctions:
{
(
newValue:
T
)
→
(
)
}
}
Functions
get<T>
State:
get<T>
(
) →
T
Returns the states current value.
set<T>
State:
set<T>
(
newValue:
T
,
force:
boolean?
--
force an update to all connections
) →
T
Allows the caller to assign the state object a new value, and returns the new value.
onChange<T>
State:
onChange<T>
(
callback:
(
newValue:
T
)
→
(
)
) →
(
)
→
(
)
Allows the caller to connect a callback which is called when the states value is changed.
CAUTION
Calling :onChange()
every frame will add a new function every frame.
You must ensure you are only calling :onChange()
once for each callback for the state's entire lifetime.
changed<T>
State:
changed<T>
(
) →
boolean
Returns true if the state was changed on this frame.