Act-Methoden schreiben
ExperteWriting Act Methods
An act method is a Rhai script attached to a node type that runs once per agent per timestep. Where property formulas compute one value at a time, an act method can update several properties together, drive state transitions, and react to the values formulas just produced.
You don't need an act method for most simulations. If property formulas can do the job, use them — they're easier to read, easier to debug, and faster to evaluate. Reach for an act method when:
- Multiple properties update together based on a shared condition (e.g., "when state is Approved, set role and bump salary")
- State transitions depend on connected agents' values (e.g., "wait for all predecessors to be Completed, then go Active")
- Conditional logic spans several properties and would be awkward as separate formulas
Where act methods live
Act methods are defined on the node type, just like property formulas:
- Select the node type in the Tree View
- Open the Properties Panel
- Find the Act Method section and click to expand the editor
- Write your script
Every agent of this type runs the script at every timestep — after all property formulas have finished.
A first act method: process step completion
Imagine a process step that progresses while it's Active and transitions to Completed when progress reaches 100%. After completion, downstream steps that were Waiting check whether their predecessors are all done and become Active themselves.
// While Active, accumulate progress
if agent.get_prop("state") == "Active" {
let new_progress = agent.get_prop("progress") + (1.0 / agent.get_prop("duration"));
agent.set_prop("progress", new_progress);
if new_progress >= 1.0 {
agent.set_prop("state", "Completed");
}
}
// While Waiting, check if all predecessors are Completed
if agent.get_prop("state") == "Waiting" {
let predecessors = get_connected(agent, "follows");
let all_done = true;
for pred in predecessors {
if pred.get_prop("state") != "Completed" {
all_done = false;
}
}
if all_done && predecessors.len() > 0 {
agent.set_prop("state", "Active");
}
}
A few things to notice:
agent.set_prop(...)is how an act method writes values. Property formulas don't use it — they just return a value. Act methods useset_propbecause they often update several properties.- The act method reads
statewithagent.get_prop("state")(no offset) — that's the value at the current timestep, including anything property formulas just computed for it. - The graph is the communication medium: a Waiting step looks at its connected predecessors via the
followslink, instead of waiting for an event.
Per-agent overrides (coming soon)
By default, every agent of a type runs the same act method. Per-agent overrides — where one specific agent in one specific scenario gets different behavior, e.g., "Alice gets a one-off retention bonus at month 6" — are coming in a future release. They'll let you customize behavior for individual instances without polluting the metamodel with one-off types.
Keeping things simple
The temptation with a scripting language is to put everything in one big block. Resist it. The clearest models stay close to property formulas and reach for an act method only where there's no good alternative — and when they do, the act method stays focused on one concern (a state machine, a multi-property update). Several short act methods on different node types beat one giant one.
Next steps
- Writing Property Formulas — the simpler companion to act methods
- Reading Connected Agents in Rhai — graph traversal in scripts
- Diagnosing Simulation Errors and Warnings — when scripts misbehave
- Rhai Function Reference — every method and helper available to scripts