This is now a fully-maintained project with a dedicated repo. You can find it at https://github.com/ClockworkSquirrel/BasicState.
This is the original v0.0.1 of BasicState. This code is still subject to the MIT license as per the official repository.
Creates a new state object. Accepts an optional InitialState
parameter, for defining the state before it is returned.
BasicState.new([ InitialState: Dictionary<any, any> ]): State
Sets the value of a given key in the state, and then fires off any Changed
signals. You should always use this when you need to change the state. Never modify state directly!
State:Set(Key: any, Value: any): void
Retrieves a value stored in the state. If DefaultValue
is specified, it will return that if the entry does not exist (or is equal to nil
).
State:Get(Key: any[, DefaultValue: any]): any
Returns the full state object. A new table is returned, rather than a reference to the internal state object. This prevents directly overwriting the state. You should always use the :Set()
method if you wish to mutate state.
State:GetState(): Dictionary<any, any>
Returns an RBXScriptSignal which is only fired when the value of the specified key is updated. The Event fires with the following values (in order):
Name | Type | Description |
---|---|---|
NewValue |
any |
The new value of the requested entry. |
OldValue |
any |
The value of the entry prior to mutation. |
OldState |
Dictionary<any, any> |
The entire state object prior to mutation. |
State:GetChangedSignal(Key: any): RBXScriptSignal
local State = BasicState.new({
Hello = "World"
})
State:GetChangedSignal("Hello"):Connect(function(NewValue, OldValue, OldState)
print(OldValue) --> "World"
print(NewValue) --> "Roblox"
end)
State:Set("Hello", "Roblox")
Destroys the current state and any RBXScriptConnections.
State:Destroy(): void
An RBXScriptSignal which is fired any time the state mutates. The Event fires with the following values (in order):
Name | Type | Description |
---|---|---|
OldState |
Dictionary<any, any> |
The entire state object prior to mutation. |
Key |
any |
The key of the entry which has mutated. |
State.Changed: RBXScriptSignal
local State = BasicState.new({
Hello = "World"
})
State.Changed:Connect(function(OldState, Key)
print(Key) --> "Hello"
print(OldState[Key]) --> "World"
print(State:Get(Key)) --> "Roblox"
end)
State:Set("Hello", "Roblox")