Stater
Stater is a finite state machine module with the purpose of making simple ai.
local States = {}
function States.DoSomethingEnd(Data)
-- this will fire when the machine switches from the "DoSomething" state
end
function States.DoSomething(Data)
-- do something with the data
end
function States.DoSomethingStart(Data)
-- this will fire when the machine switch to "DoSomething"
Data.Stater:SetState("SomethingDifferent")
end
local Data = {
Something = "Something",
}
Data.Stater = Stater.new(States, 0, Data)
Data.Stater:Start("DoSomething")
Types
State
Functions
new
Stater.new(Tick: number?,--
Optional tick to be set.
Return: T?--
Determines what to return in the first parameter of each state.
) → Stater<T>Types
interface Stater {States: {[string]: State}--
The Provided States Table, if theres a "Init" state then that function will execute each time the Stater Starts.
Info: {any?}--
A table that you can add anything in, this is more recommended than directly inserting variables inside the object.
Tick: number?--
The time it takes for the current state to be called again after a function is done. Default is 0
Return: any--
This is the thing that returns as the first parameter of every single state. Default is the Stater object itself.
StateConfirmation: boolean--
If this is enabled, the state MUST return a boolean indicating if the function ran properly.
Changed: RBXScriptSignal--
A signal that fires whenever the State changes. Returns Current State and Previous State
StatusChanged: RBXScriptSignal--
Fired whenever the Stater starts or closes. Returns the current status as a boolean.
StateRemoved: RBXScriptSignal--
A signal that fires whenever a state is added via the Stater:AddState() method. Returns the State Name.
StateAdded: RBXScriptSignal--
A signal that fires whenever a state is removed via the Stater:RemoveState() method. Returns the State Name.
}Returns a new Stater Object.
Errors
| Type | Description |
|---|---|
| "No States" | Happens when no States are provided |
RemoveState
Stater:RemoveState(Name: string--
The name of the removing state.
) → ()Removes a state inside the states table.
AddState
Stater:AddState(Name: string,--
The name that the state will go by.
) → ()Adds a state inside the states table. If there is a Start after the State name inside the States, that will play. If there is a End after the State name inside the States, that will play after the state changes.
Errors
| Type | Description |
|---|---|
| "Existing State" | Happens when the name of the state is already inside the table. |
GetCurrentState
Stater:GetCurrentState() → string?Returns the current state the Stater is on indicated by a string. If none then nil. This is currently the same as self.State.
IsWorking
Stater:IsWorking() → booleanReturns a boolean indicating if the State currently is on.
SetState
Stater:SetState(State: string--
The function name inside States represented by a string
) → ()Returns a boolean indicating if the State currently is on.
Errors
| Type | Description |
|---|---|
| "No State" | Happens when no State is provided. |
| "Invalid State" | Happens when the state provided doesn't exist. |
Start
Stater:Start(StartingState: string--
The function name inside States represented by a string, this state will be set at the start.
) → ()Begins the Stater.
Errors
| Type | Description |
|---|---|
| "No State" | Happens when no State is provided. |
Stop
Stater:Stop() → ()Stops the stater and its state.
Destroy
Stater:Destroy() → ()Gets rid of the Stater Object.