Globals

Global variables are defined in the globals.json file present within every simulation. These variables are immutable while the simulation is running and are accessible to all agents simultaneously.

Accessing the properties of the simulation is as simple as using the context.globals() function in our agent behavior.

To change properties while the simulation is running, make sure to pause the simulation, make the appropriate changes, and resume.

If, for example, we wanted to cap the height of all trees in a forest simulation, we might introduce the global variable "maxTreeHeight". The globals.json file would contain something like:

{
"maxTreeHeight": 50,
...
}

The associated tree growth behavior would follow:

JavaScript
Python
JavaScript
function behavior(state, context) {
...
if (state.height + growth <= context.globals().maxTreeHeight) {
growtree()
}
...
}
Python
def behavior(state, context):
...
if state.height + growth <= context.globals()['maxTreeHeight']):
growtree()
...

Visual Globals

You can view and modify the parameters in globals.json either as code or as a form, with customizable inputs. Click the toggle visual globals button at the top right of the code editor pane.

Toggle between edit and input of globals

You can customize the type of field input for a global variable by adding a "schema" property to globals.

//globals.json
//adds a color picker selector to the color property
{
"color": "#ff0000",
"schema": {
"properties": {
"color": {
"type": "string",
"enum": "colors"
}
}
}
//...more properties
}

This would be rendered in the visual globals as:

Currently visual globals supports:

  • Color Picker:

"properties": {
"[property name]": {
"type": "string",
"enum": "colors"
}
}

By default a non-signed in viewer of a simulation will see and interact with the visual globals view.