DevTools Surf logoDevTools Surf
AI / Modern DevAnimation / CSSAPI / Config
Sign in
DevTools Surf logoDevTools Surf
AI / Modern DevAnimation / CSSAPI / Config
Sign in
HomeGame DevState Machine Generator

Example: State Machine Generator

A worked example, rendered from real sample data. Sign in to run the tool on your own input.

Input
initial: Idle
Idle -> Walking : move [hasInput]
Walking -> Running : sprint
Running -> Walking : walk
Walking -> Idle : stop
Idle -> Jumping : jump
Jumping -> Idle : land
Any -> Dead : die
Output
export type State = "Idle" | "Walking" | "Running" | "Jumping" | "Dead";
export type Trigger = "move" | "sprint" | "walk" | "stop" | "jump" | "land" | "die";
export type Guard = "hasInput";

export interface Transition { from: State | "Any"; to: State; trigger: Trigger; guard?: Guard }

export const TRANSITIONS: Transition[] = [
  { from: "Idle", to: "Walking", trigger: "move", guard: "hasInput" },
  { from: "Walking", to: "Running", trigger: "sprint" },
  { from: "Running", to: "Walking", trigger: "walk" },
  { from: "Walking", to: "Idle", trigger: "stop" },
  { from: "Idle", to: "Jumping", trigger: "jump" },
  { from: "Jumping", to: "Idle", trigger: "land" },
  { from: "Any", to: "Dead", trigger: "die" },
];

export const INITIAL_STATE: State = "Idle";

export const guards: Record<Guard, () => boolean> = {
  hasInput: () => true,
};
export interface StateHooks {
  onEnter?: Partial<Record<State, () => void>>;
  onExit?: Partial<Record<State, () => void>>;
  onUpdate?: Partial<Record<State, (deltaSeconds: number) => void>>;
}

export class StateMachine {
  state: State = INITIAL_STATE;
  constructor(private hooks: StateHooks = {}) {}

  can(trigger: Trigger): boolean {
    return this.match(trigger) !== undefined;
  }

  send(trigger: Trigger): boolean {
    const next = this.match(trigger);
    if (!next) return false;
    this.hooks.onExit?.[this.state]?.();
    this.state = next.to;
    this.hooks.onEnter?.[this.state]?.();
    return true;
  }

  update(deltaSeconds: number): void {
    this.hooks.onUpdate?.[this.state]?.(deltaSeconds);
  }

  private match(trigger: Tr
…

About State Machine Generator

State Machine Generator preview - Game Dev tool

Generate finite state machine code from state/transition definitions. Part of the DevTools Surf developer suite. Browse more tools in the Game Dev collection.

Use Cases

  • Game AI programmers defining NPC behavior state transitions
  • UI developers managing complex multi-step form workflows
  • Game developers implementing character animation state machines
  • Backend developers generating state machines for order processing

Tips

  • Define all valid state transitions to catch illegal state changes
  • Add entry and exit actions to each state for side effects
  • Generate a state diagram to visualize the machine before coding

Fun Facts

  • Finite state machines were formalized by Warren McCulloch and Walter Pitts in their 1943 paper on neural networks — the concept predates modern computers.
  • The game AI in Pac-Man (1980) uses a simple FSM with four states — Chase, Scatter, Frightened, and Eaten — creating the illusion of intelligent ghost behavior with minimal logic.
  • Statecharts, an extension of FSMs by David Harel in 1987, added hierarchy and concurrency — they directly inspired libraries like XState, which has over 25,000 GitHub stars.

FAQ

What does it output?
Finite state machine code in C#, JS, Python, or generic pseudocode. States, transitions, entry/exit callbacks, and optional guards.
Does it visualize transitions?
Yes — a state diagram (text-based ASCII art or SVG) showing states and transitions. Helps verify logic before coding.
Hierarchical states?
Basic parent/child state nesting. For complex hierarchical state machines (HSM), use a dedicated library (XState, SMACH).
Game AI use case?
Classic — enemy behavior trees, NPC dialogue states, player input handling. FSMs are the foundational game AI pattern.

Related Game Dev Tools

Unity Icon GeneratorAndroid Icon GeneratoriOS Icon GeneratorUnity C# Class GeneratorUnity Prefab InspectorSprite Sheet CalculatorGame Math CalculatorGame Color Palette Generator
New · Flagshipsimple REST client

REST Handler — Collections, env vars, history, cURL converter

Send requests, save collections (nested), swap environments, and convert between cURL / Collection JSON / REST Handler YAML.

Open

Popular tools

The most-used tools on DevToolsSurf, one click away.

Encoding & crypto

  • Base64 Encode
  • Base64 Decode
  • URL Encoder
  • URL Decoder
  • Hash Generator
  • JWT Decoder
  • JWT Encoder
  • UUID Generator
  • ULID Generator
  • Password Generator
  • Bcrypt Hash Tester

Converters

  • CSV to JSON
  • JSON to CSV
  • XML to JSON
  • JSON to XML
  • HTML → Markdown
  • HTML → React JSX
  • cURL to Code
  • Collection JSON → cURL
  • Swagger to Collection JSON
  • JSON → Go Struct
  • JSON → TypeScript Types

JSON & YAML

  • JSON Formatter
  • JSON Validator
  • JSON Viewer
  • JSON Minifier
  • JSON Diff
  • JSONPath Tester
  • YAML Formatter
  • YAML to JSON
  • JSON to YAML

Text & regex

  • Regex Tester
  • Text Diff
  • Case Converter
  • Word Counter
  • Markdown Preview
  • Slug Generator
  • Lorem Ipsum Generator
  • Markdown → PDF

CSS & color

  • CSS Beautifier
  • Minify CSS
  • Color Converter
  • Gradient Generator
  • Contrast Checker
  • Color Palette Generator
  • Flexbox Playground
  • Tailwind → CSS

Generators

  • QR Code Generator
  • Mock Data Generator
  • Favicon Generator
  • .gitignore Builder
  • README.md Generator
  • Dockerfile Generator
  • Sitemap Generator

API & networking

  • REST Handler
  • HTTP Header Analyzer
  • IP Address Lookup
  • CIDR Calculator
  • User-Agent Parser
  • HTTP Status Reference
  • OpenAPI Viewer

Date & time

  • Timestamp Converter
  • Timezone Converter
  • Cron Expression Parser
  • Duration Calculator
  • Age Calculator
  • Date Format Converter

Images

  • Image Converter
  • Image Resizer (Batch)
  • SVG Optimizer
  • Base64 ↔ Image
  • WebP ↔ AVIF Converter
  • Image Compressor

PDF tools

  • PDF Merger
  • PDF Splitter
  • PDF Compressor
  • Markdown → PDF
  • EPUB → PDF
  • MOBI / AZW → PDF
  • DOCX → PDF
  • HTML → PDF

Resources

  • Community feed
  • Themes marketplace
  • Pricing & credits
  • Privacy policy
  • Terms of service
  • Sitemap
  • robots.txt

Your account

  • Sign in
  • Dashboard
  • Run history
  • My profile
  • Settings
DevTools Surf logo
DevTools Surf919+ tools

Fast · privacy-first · client-side · © 2026

Home·Feed·ThemesPricing·Sign inPrivacy·Sitemap Feedback