Skip to main content

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

type State = (Stater | any) → boolean?

Functions

new

Stater.new(
States{[string]State<T>},--

The Table that will have all the States

Ticknumber?,--

Optional tick to be set.

ReturnT?--

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.

Ticknumber?--

The time it takes for the current state to be called again after a function is done. Default is 0

Returnany--

This is the thing that returns as the first parameter of every single state. Default is the Stater object itself.

StateState--

The current state that the Stater is on.

StateConfirmationboolean--

If this is enabled, the state MUST return a boolean indicating if the function ran properly.

ChangedRBXScriptSignal--

A signal that fires whenever the State changes. Returns Current State and Previous State

StatusChangedRBXScriptSignal--

Fired whenever the Stater starts or closes. Returns the current status as a boolean.

StateRemovedRBXScriptSignal--

A signal that fires whenever a state is added via the Stater:AddState() method. Returns the State Name.

StateAddedRBXScriptSignal--

A signal that fires whenever a state is removed via the Stater:RemoveState() method. Returns the State Name.

}

Returns a new Stater Object.

Errors

TypeDescription
"No States"Happens when no States are provided

RemoveState

Stater:RemoveState(
Namestring--

The name of the removing state.

) → ()

Removes a state inside the states table.

AddState

Stater:AddState(
Namestring,--

The name that the state will go by.

StateState<any>--

The State function itself.

) → ()

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

TypeDescription
"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() → boolean

Returns a boolean indicating if the State currently is on.

SetState

Stater:SetState(
Statestring--

The function name inside States represented by a string

) → ()

Returns a boolean indicating if the State currently is on.

Errors

TypeDescription
"No State"Happens when no State is provided.
"Invalid State"Happens when the state provided doesn't exist.

Start

Stater:Start(
StartingStatestring--

The function name inside States represented by a string, this state will be set at the start.

) → ()

Begins the Stater.

Errors

TypeDescription
"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.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Returns a new Stater Object.",
            "params": [
                {
                    "name": "States",
                    "desc": "The Table that will have all the States",
                    "lua_type": "{[string]: State<T>}"
                },
                {
                    "name": "Tick",
                    "desc": "Optional tick to be set.",
                    "lua_type": "number?"
                },
                {
                    "name": "Return",
                    "desc": "Determines what to return in the first parameter of each state.",
                    "lua_type": "T?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Stater<T>\r\n"
                }
            ],
            "function_type": "static",
            "errors": [
                {
                    "lua_type": "\"No States\"",
                    "desc": "Happens when no States are provided"
                }
            ],
            "source": {
                "line": 112,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "RemoveState",
            "desc": "Removes a state inside the states table.",
            "params": [
                {
                    "name": "Name",
                    "desc": "The name of the removing state.",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 144,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "AddState",
            "desc": "Adds a state inside the states table. If there is a Start after the State name inside the States, that will play.\nIf there is a End after the State name inside the States, that will play after the state changes.",
            "params": [
                {
                    "name": "Name",
                    "desc": "The name that the state will go by.",
                    "lua_type": "string"
                },
                {
                    "name": "State",
                    "desc": "The State function itself.",
                    "lua_type": "State<any>"
                }
            ],
            "returns": [],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "\"Existing State\"",
                    "desc": "Happens when the name of the state is already inside the table."
                }
            ],
            "source": {
                "line": 166,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "GetCurrentState",
            "desc": "Returns the current state the Stater is on indicated by a string. If none then nil.\nThis is currently the same as self.State.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string?\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 181,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "IsWorking",
            "desc": "Returns a boolean indicating if the State currently is on.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 188,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "SetState",
            "desc": "Returns a boolean indicating if the State currently is on.",
            "params": [
                {
                    "name": "State",
                    "desc": "The function name inside States represented by a string",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "\"No State\"",
                    "desc": "Happens when no State is provided."
                },
                {
                    "lua_type": "\"Invalid State\"",
                    "desc": "Happens when the state provided doesn't exist."
                }
            ],
            "source": {
                "line": 199,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "Start",
            "desc": "Begins the Stater.",
            "params": [
                {
                    "name": "StartingState",
                    "desc": "The function name inside States represented by a string, this state will be set at the start.",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "\"No State\"",
                    "desc": "Happens when no State is provided."
                }
            ],
            "source": {
                "line": 227,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "Stop",
            "desc": "Stops the stater and its state.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 265,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Gets rid of the Stater Object.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 292,
                "path": "src/Stater/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "State",
            "desc": "",
            "lua_type": "(Stater | any) -> boolean?",
            "source": {
                "line": 54,
                "path": "src/Stater/init.luau"
            }
        },
        {
            "name": "Stater",
            "desc": "",
            "fields": [
                {
                    "name": "States",
                    "lua_type": "{[string]: State}",
                    "desc": "The Provided States Table, if theres a \"Init\" state then that function will execute each time the Stater Starts."
                },
                {
                    "name": "Info",
                    "lua_type": "{any?}",
                    "desc": "A table that you can add anything in, this is more recommended than directly inserting variables inside the object."
                },
                {
                    "name": "Tick",
                    "lua_type": "number?",
                    "desc": "The time it takes for the current state to be called again after a function is done. Default is 0"
                },
                {
                    "name": "Return",
                    "lua_type": "any",
                    "desc": "This is the thing that returns as the first parameter of every single state. Default is the Stater object itself."
                },
                {
                    "name": "State",
                    "lua_type": "State",
                    "desc": "The current state that the Stater is on."
                },
                {
                    "name": "StateConfirmation",
                    "lua_type": "boolean",
                    "desc": "If this is enabled, the state MUST return a boolean indicating if the function ran properly."
                },
                {
                    "name": "Changed",
                    "lua_type": "RBXScriptSignal",
                    "desc": "A signal that fires whenever the State changes. Returns Current State and Previous State"
                },
                {
                    "name": "StatusChanged",
                    "lua_type": "RBXScriptSignal",
                    "desc": "Fired whenever the Stater starts or closes. Returns the current status as a boolean."
                },
                {
                    "name": "StateRemoved",
                    "lua_type": "RBXScriptSignal",
                    "desc": "A signal that fires whenever a state is added via the Stater:AddState() method. Returns the State Name."
                },
                {
                    "name": "StateAdded",
                    "lua_type": "RBXScriptSignal",
                    "desc": "A signal that fires whenever a state is removed via the Stater:RemoveState() method. Returns the State Name."
                }
            ],
            "source": {
                "line": 69,
                "path": "src/Stater/init.luau"
            }
        }
    ],
    "name": "Stater",
    "desc": "Stater is a finite state machine module with the purpose of making simple ai.\n\n```lua\n    local States = {}\n\n    function States.DoSomethingEnd(Data)\n        -- this will fire when the machine switches from the \"DoSomething\" state \n    end\n\n    function States.DoSomething(Data)\n        -- do something with the data\n    end\n\n    function States.DoSomethingStart(Data)\n        -- this will fire when the machine switch to \"DoSomething\"\n        Data.Stater:SetState(\"SomethingDifferent\")\n    end\n\n    local Data = {\n        Something = \"Something\",\n    }\n\n    Data.Stater = Stater.new(States, 0, Data)\n\n    Data.Stater:Start(\"DoSomething\")\n```",
    "source": {
        "line": 38,
        "path": "src/Stater/init.luau"
    }
}