Rhai Function Reference
Built-in functions and scope variables available inside simulation Rhai scripts.
Agent Reference
agent.get_propbothRead a property's value on the current agent. The simplest form returns the value at the current timestep. Add a negative offset to look back in time — useful for self-referential formulas where the new value depends on the old one. The return type matches the property's data type: a number for Number properties, true/false for Boolean, a string for everything else.
agent.has_propbothCheck whether a property exists on the agent. Returns true if the property is defined on the agent's node type, false otherwise. Useful when working with optional properties or when writing formulas that adapt to different agent types in the same model.
agent.set_propact_methodWrite a value to one of the agent's properties at the current timestep. Only available inside an act method — property formulas don't use set_prop, they return their result instead. set_prop can override values that property formulas just computed for the same timestep, so use it when behavior touches multiple properties or depends on state-machine-like logic.
Aggregation
count_propbothCounts how many agents in an array have a specific value for a given property. Common pattern: counting connected agents in a particular state (e.g., active members of a team, completed steps of a process). The comparison is exact — string equality for text properties, numeric equality for numbers.
sum_propbothSums a numeric property across an array of agents. Most often used in combination with get_connected to roll up values from connected agents — total payroll, total inventory, total revenue. Non-numeric or missing properties evaluate to 0, so a partially-filled set still produces a clean number.
Time Context
__dtbothThe scenario's step size — for example, 1.0 if start and end times are in months and dt is 1, or 1/12 if start and end are in years and you're stepping monthly. Use it to scale rates that are expressed per unit of natural time, so a formula stays correct even if the scenario's granularity changes later.
__timestepbothThe current timestep's index — 0 at the start of the scenario, 1 at the next step, and so on. Available as a scope variable inside every formula and act method. Useful for periodic logic ('once per year'), startup conditions, or any rule that depends on absolute time within the scenario.