CyberBasic now includes a modern BASIC-style state system that provides intuitive, readable syntax for managing game state machines. The system uses traditional BASIC keywords and block structures while providing powerful state management capabilities.
Declares a named state block with optional animation, hooks, and transitions.
STATE Idle
ANIMATION "idle_anim"
TRANSITION TO Walking WHEN INPUT("move")
END STATEDefines a rule for moving between states.
TRANSITION Idle TO Walking WHEN INPUT("move")
TRANSITION Walking TO Idle WHEN NOT INPUT("move")
TRANSITION Walking TO Jumping WHEN INPUT("jump") PRIORITY=1Event hooks tied to a state's lifecycle.
STATE Jumping
ON ENTER
PLAY SOUND "jump.wav"
SET VELOCITY 0, -10, 0
END ON
ON UPDATE
MOVE PLAYER INPUT("direction"), SPEED=5
END ON
ON EXIT
STOP ANIMATION "jump_anim"
END ON
END STATEBinds an animation to the state.
ANIMATION "run_anim", BLEND=0.2Forces a minimum duration before leaving a state.
STATE Attacking
ANIMATION "attack_anim"
WAIT 0.3
TRANSITION TO Idle WHEN ANIMATION DONE
END STATEAllows multiple states to run concurrently.
PARALLEL
STATE Movement
TRANSITION TO Walking WHEN INPUT("move")
END STATE
STATE Emotion
TRANSITION TO Happy WHEN HEALTH > 50
END STATE
END PARALLELCreates hierarchical states.
GROUP Movement
STATE Walking
ANIMATION "walk_anim"
END STATE
STATE Running
ANIMATION "run_anim"
END STATE
END GROUPDeclares a trigger that can be listened to.
EVENT EnemySpotted
TRANSITION Idle TO Alert WHEN EVENT("EnemySpotted")Declares a custom state system container.
DEFINE STATE SYSTEM PlayerSystem
STATE Idle
ANIMATION "idle_anim"
END STATE
STATE Walking
ANIMATION "walk_anim"
END STATE
END SYSTEMAssigns a state system to an entity.
VAR player = scene.createEntity("Player")
ATTACH SYSTEM PlayerSystem TO playerDynamically adds a new state to an existing system.
ADD STATE "Crouching" TO PlayerSystem
ANIMATION "crouch_anim"
TRANSITION TO Idle WHEN NOT INPUT("crouch")
END STATERemoves a state from a system.
REMOVE STATE "Crouching" FROM PlayerSystemRedefines behavior of a state without rewriting the whole system.
OVERRIDE STATE "Walking"
ANIMATION "fast_walk_anim"
SPEED = 6
END STATETweaks transition conditions globally.
SET TRANSITION RULE Idle TO Walking PRIORITY=1Temporarily toggle states for debugging or special modes.
DISABLE STATE "Attacking"
ENABLE STATE "Jumping"Prints or visualizes the current state system.
DEBUG STATES PlayerSystemAllows sharing or reusing state systems across projects.
EXPORT SYSTEM PlayerSystem TO "player_states.json"
IMPORT SYSTEM EnemySystem FROM "enemy_states.json"// Define player state system
DEFINE STATE SYSTEM PlayerSystem
STATE Idle
ANIMATION "idle_anim"
TRANSITION TO Walking WHEN INPUT("move")
TRANSITION TO Jumping WHEN INPUT("jump")
END STATE
STATE Walking
ANIMATION "walk_anim"
ON UPDATE
MOVE PLAYER INPUT("direction"), SPEED=3
END ON
TRANSITION TO Idle WHEN NOT INPUT("move")
TRANSITION TO Jumping WHEN INPUT("jump")
END STATE
STATE Jumping
ANIMATION "jump_anim"
ON ENTER
PLAY SOUND "jump.wav"
SET VELOCITY 0, -10, 0
END ON
ON UPDATE
MOVE PLAYER INPUT("direction"), SPEED=5
END ON
WAIT 0.5
TRANSITION TO Idle WHEN GROUNDED()
END STATE
STATE Attacking
ANIMATION "attack_anim"
ON ENTER
PLAY SOUND "attack.wav"
END ON
WAIT 0.3
TRANSITION TO Idle WHEN ANIMATION DONE
END STATE
END SYSTEM
// Create player entity
VAR scene = Scene("Game")
VAR player = scene.createEntity("Player")
player.addComponent("Transform")
player.addComponent("Sprite", {textureId = 1})
// Attach state system to player
ATTACH SYSTEM PlayerSystem TO player
// Game loop
WHILE NOT GAME.shouldClose()
GAME.beginFrame()
VAR delta = Clock.getDelta()
// Update state system
UPDATE_STATE_SYSTEM("PlayerSystem", delta)
// Update scene
scene.update(delta)
// Draw scene
scene.draw()
GAME.endFrame()
WENDDEFINE STATE SYSTEM ComplexSystem
PARALLEL
STATE Movement
TRANSITION TO Walking WHEN INPUT("move")
END STATE
STATE Combat
TRANSITION TO Attacking WHEN INPUT("attack")
END STATE
END PARALLEL
END SYSTEMDEFINE STATE SYSTEM MovementSystem
GROUP GroundMovement
STATE Walking
STATE Running
STATE Crouching
END GROUP
GROUP AirMovement
STATE Jumping
STATE Falling
END GROUP
END SYSTEM// Add state at runtime
ADD STATE "Dashing" TO PlayerSystem
ANIMATION "dash_anim"
TRANSITION TO Idle WHEN ANIMATION DONE
END STATE
// Override existing state
OVERRIDE STATE "Walking"
SPEED = 8
ANIMATION "sprint_anim"
END STATE
// Disable state temporarily
DISABLE STATE "Attacking"
// Re-enable
ENABLE STATE "Attacking"EVENT EnemySpotted
EVENT LowHealth
EVENT PowerUpCollected
DEFINE STATE SYSTEM AISystem
STATE Patrol
TRANSITION TO Alert WHEN EVENT("EnemySpotted")
END STATE
STATE Alert
TRANSITION TO Flee WHEN EVENT("LowHealth")
END STATE
STATE Flee
TRANSITION TO Patrol WHEN NOT EVENT("EnemySpotted")
END STATE
END SYSTEM- Use meaningful state names -
Idle,Walking,Jumpingare better thanState1,State2 - Keep transitions simple - Complex conditions should be in functions
- Use WAIT for animation timing - Prevents state changes during animations
- Group related states - Use GROUPs for organization
- Debug frequently - Use
DEBUG STATESto visualize state flow - Export reusable systems - Save common state systems for reuse
The state system integrates seamlessly with the ECS system:
VAR player = scene.createEntity("Player")
player.addComponent("Transform")
player.addComponent("Sprite")
player.addComponent("Health", {maxHealth = 100, currentHealth = 100})
ATTACH SYSTEM PlayerSystem TO player
// State system can access entity components
STATE LowHealth
ON ENTER
VAR health = player.getComponent("Health")
IF health.currentHealth < 20 THEN
PLAY SOUND "low_health.wav"
END IF
END ON
END STATE- State transitions are O(n) where n is number of transitions
- Use PRIORITY to optimize transition checking
- Disable unused states to reduce overhead
- Parallel states run concurrently but share update time
Planned features:
- State history/undo
- State blending
- State machine visualization
- Hot-reload state systems
- State machine templates
- Conditional state groups