Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aceworks-studio/state-machine

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aceworks-studio/state-machine

A Luau utility to easily create simple state machines

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

checks version GitHub top language license npm

@aceworks-studio/state-machine

A Luau utility to easily create simple state machines.

Installation

Add @aceworks-studio/state-machine in your dependencies:

yarn add @aceworks-studio/state-machine

Or if you are using npm:

npm install @aceworks-studio/state-machine

Content

create

function StateMachine.create(config: StateMachineConfiguration): () -> ()

type StateController = {
    addCleanup: (...Teardown) -> () -> (),
    next: (nextState: string, nextConfig: any) -> never,
}

type StateEvent = { state: string, config: any, time: number }
type StateHistory = { StateEvent }

type StateMachineConfiguration = {
    defaultState: string,
    defaultStateConfig: any,
    states: {
        [string]: (StateController, config: any) -> (),
    },
    onStateChange: (state: string, config: any) -> ()?,
    onStateHistoryChange: (StateHistory) -> ()?,
}
Example
create({
	defaultState = "startup",
	defaultStateConfig = { mapName = "defaultMap" },
	states = {
		startup = function(controller, config)
			controller.addCleanup(task.spawn(function()
				local model = loadMap(config.mapName)
				task.wait(2)
				controller.next("match", { model = model })
			end))
		end,
		match = function(controller, config)
			teleportAllPlayersInMap(config.model)

			controller.addCleanup(
				task.delay(30, function()
					controller.next("expired")
				end),
				task.spawn(function()
					Time.yieldUntil(0.2, function()
						return not hasEnoughPlayer()
					end)

					local remaining = getRemainingPlayers()
					local remainingCount = #remaining

					if remainingCount == 1 then
						controller.next("winner", { winner = remaining[1] })
					else
						controller.next("startup", { mapName = pickRandomMap() })
					end
				end)
			)
		end,
		winner = function(controller, config)
			print("winner!", config.winner)
			controller.addCleanup(task.delay(3, function()
				controller.next("startup")
			end))
		end,
		expired = function(controller)
			print("expired!")
			controller.addCleanup(task.delay(3, function()
				controller.next("startup")
			end))
		end,
	},
})

License

This project is available under the MIT license. See LICENSE.txt for details.

Other Lua Environments Support

If you would like to use this library on a Lua environment where it is currently incompatible, open an issue (or comment on an existing one) to request the appropriate modifications.

The library uses darklua to process its code.

Keywords

FAQs

Package last updated on 18 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc