RobloxStateMachine - A Simple State Machine implementation

Documentation | Source

:school: Introduction

This is a Roblox implementation of a simple State Machine! I personally hate the concept of over-engineering things so instead I kept it simple and easy to use as all things should be! But in case you are wondering… what is a State Machine?

A state machine is a way of organizing and controlling the behavior of objects in a program. It’s like a flowchart that defines the different states an object can be in and the transitions between those states.

In game development, state machines are often used to manage the behavior of characters, enemies, and other objects in the game world. For example, a state machine might define the states “idle,” “walking,” “running,” and “attacking” for a player character. When the player presses a button to move, the state machine would transition the character from the “idle” state to the “walking” state, and then to the “running” state if the player holds down the movement button for a longer period of time.

State machines can help make game development more efficient and easier to manage by providing a clear structure for controlling the behavior of objects. They also make it easier to add new states and transitions as the game evolves and new features are added.

:file_folder: Installation

There are 3 different ways of installing this package into your project! You can either download it from the releases page, download the Roblox model or install it via Wally!

Wally: Wally

Roblox Model: RobloxStateMachine - Roblox

Releases: Releases · prooheckcp/RobloxStateMachine · GitHub

:camera:Tutorial

RobloxStateMachine Tutorial (Studio) - YouTube

:orange_book: Example

:star: Contributing

Please leave a star on GitHub, it helps a lot!

Pull requests are welcome. For major changes, please open an issue first

to discuss what you would like to change.

Please make sure to update tests as appropriate.

:page_facing_up: License

MIT

50 Likes

Would a Tutorial Video be worth it for this?

7 Likes

Great, thank you
Tutorial video would be great!

Yes, this can be used for any kind of states. I’ll be making a YouTube video and posting it here on Sunday (I’m currently on a vacation off my country until Saturday night)

1 Like

I added a tutorial video to help people pick up this library. The tutorial consists of an NPC that can chase and attack the player (without any animations). If anyone has any questions please write them here!

Tutorial: RobloxStateMachine Tutorial (Studio) - YouTube

2 Likes

Awesome library! I have implemented something like this it’s just not properly organized and clean. Would definitely be using this for our re-work on our game!

1 Like

RobloxStateMachine - V.1.1

Major polishing of the library

:wrench: Changes

  • Remove useless OnHeartbeat
  • Adding error contexts to the developers
  • Improve LoadDirectory
  • Major improvement to the API website
  • Added tutorials
  • Updated ReadMe file to a cleaner and simpler file
  • Fixing types of issues
  • Improved performance
  • Added new support to index data in different ways
4 Likes

If I put an object of a class as data, can that also trigger OnDataChanged?

OnDataChanged gets called every frame so it will accept any format of data that is fed into the state machine

1 Like

Ah, I see. Thank you.

Do you know how the state machine does performance wise? I’m trying to make NPCs, and each have their own state machine, so I am curious how quickly performance may degrade.

Also, this module is great. I really appreciate how easy it is to use!

It will be fine, it’s very light. We have it in a project with nearly 100 NPCs (each NPC has its own state machine) and it doesn’t even use 10% of the total performance usage of the project.

What would make the state machine heavy is if you do anything like raycasting on transitions or something very expensive. With normal usage it will be fine

1 Like

Thanks,i am looking for a good statemachine for a long time.Pls be simple enough for me,bcs i am a noob.

Nevermind, I found a destroy function in the module. There should probably be somewhere in the documentation that shows that there is a destroy function.

Original post: Is there anyway to destroy a state machine or disconnect state machine events?

Sorry, it seems there was an error building up the page API. I will get this fixed once I’m back to my country (this weekend).

2 Likes

Any update on the API - Page, it would be much appreciated.

It’s now fixed on live: RobloxStateMachine (prooheckcp.github.io)

Is this a Hierarchical state machine?

Oh my god! I’ve waiting for something like this :slight_smile: Thank you very much!!!

:rocket:RobloxStateMachine V.1.1.7 IS NOW OUT!

:wrench: Changes

  • Added:

    • Added OnDestroy virtual Method on Transition
    • Added OnDestroy virtual method on State
    • State:Extend to extend the class
    • Transition:Extend to extend the class
    • Added State:CanChangeState method
  • Deprecated:

    • Transition:CanChangeState has been deprecated
  • QOL improvement:

    • Improved documentation
    • Can now Call class constructors without .new
--Example:
local StateMachine = require(ReplicatedStorage.RobloxStateMachine)
local GoToRed = require(script.Parent.Parent.Transitions.GoToRed)

local State = StateMachine.State

local ChangeColorState = State "ChangeColorState"
ChangeColorState.Transitions = {GoToRed}

HI, awesome stuff, thank you for making it Open Source.

I’m trying to make NPCs communicate with each other, in order to avoid the player getting overwhelmed, and to make custom behaviours depending on how they’re spread out ect.
I understand I can change a State Machine’s data to update this kind of info, within the state/transition scripts (might be more pertinent within the npc script?),
But I think I need to use bindable events to communicate data (I think it only makes sense to listen to those events within the npcs’ script).

Its prob a dumb concern but I want to be sure on what the optimal way to make state machines communicate with each other, as well as updating that kind of info is.