Traits
Trait definitions and state machine types for Almadar
How traits work in the Almadar/Orbital architecture - state machines, guards, effects, and cross-orbital communication.
Related: Entities
Overview
In Almadar, a Trait is a state machine that defines behavior for an entity. The fundamental composition is:
Orbital Unit = Entity + Traits + Pages
While Entities define the shape of data, Traits define how that data changes over time through states, transitions, guards, and effects.
Trait Definition
A trait is defined in the .orb schema with the following structure:
{
"name": "TaskManagement",
"category": "interaction",
"linkedEntity": "Task",
"description": "Manages task lifecycle and status changes",
"emits": [
{ "event": "TASK_COMPLETED", "scope": "external" }
],
"listens": [
{ "event": "USER_ASSIGNED", "triggers": "ASSIGN" }
],
"stateMachine": {
"states": [
{ "name": "idle", "isInitial": true },
{ "name": "active" },
{ "name": "completed", "isTerminal": true }
],
"events": [
{ "key": "START", "name": "Start Task" },
{ "key": "COMPLETE", "name": "Complete Task" }
],
"transitions": [
{
"from": "idle",
"to": "active",
"event": "START",
"effects": [["set", "@entity.id", "status", "active"]]
},
{
"from": "active",
"to": "completed",
"event": "COMPLETE",
"guard": ["=", "@entity.assigneeId", "@user.id"],
"effects": [
["set", "@entity.id", "status", "completed"],
["emit", "TASK_COMPLETED", { "taskId": "@entity.id" }]
]
}
]
}
}
Trait Properties
| Property | Required | Description |
|---|---|---|
name | Yes | Trait identifier (PascalCase) |
category | No | Trait category (see below) |
linkedEntity | No | Entity this trait operates on |
description | No | Human-readable description |
emits | No | Events this trait can emit |
listens | No | Events this trait listens for |
stateMachine | Yes | State machine definition |
ticks | No | Scheduled/periodic effects |
config | No | Configuration schema |
Trait Categories
Traits are categorized by their primary purpose:
| Category | Purpose | Typical Effects |
|---|---|---|
interaction | Client-side UI event handling | render-ui, navigate, notify |
integration | Server-side operations | persist, fetch, call-service |
lifecycle | Entity lifecycle management | persist, emit |
gameCore | Game loop and physics | set, emit, ticks |
gameEntity | Game entity behaviors | set, emit, render-ui |
gameUi | Game UI, HUD, controls | render-ui, notify |
Category Examples
Interaction Trait - Handles UI events:
{
"name": "FormInteraction",
"category": "interaction",
"stateMachine": {
"transitions": [{
"event": "SUBMIT",
"effects": [
["render-ui", "main", { "type": "form", "loading": true }],
["emit", "FORM_SUBMITTED", "@payload"]
]
}]
}
}
Integration Trait - Handles server operations:
{
"name": "DataPersistence",
"category": "integration",
"stateMachine": {
"transitions": [{
"event": "SAVE",
"effects": [
["persist", "update", "Task", "@entity.id", "@payload"],
["emit", "DATA_SAVED", { "id": "@entity.id" }]
]
}]
}
}
State Machine
Every trait has a state machine that defines its behavior.
States
States represent the possible conditions of a trait:
{
"states": [
{ "name": "idle", "isInitial": true, "description": "Waiting for input" },
{ "name": "loading", "description": "Fetching data" },
{ "name": "active", "description": "Ready for interaction" },
{ "name": "error", "isTerminal": true, "description": "Error state" }
]
}
| Property | Description |
|---|---|
name | State identifier (lowercase) |
isInitial | Starting state (exactly one required) |
isTerminal | No outgoing transitions expected |
description | Human-readable description |