Skip to main content
Version: 🚧 Alpha 🚧

Defining user interaction

GAML lets the user act on a running simulation: catching clicks and key presses in a display, adding buttons that trigger an action, or asking for values through a dialog. This chapter describes the event and user_command statements and the user_input_dialog operator. Agents whose whole behaviour is organised around user interaction are described in the user control architecture.

Catch Mouse Event​

Mouse events can be caught during the simulation with the statement event. The trigger is written after event. It can be a specific mouse or keyboard event, a character or key combination, or a generic #mouse_listener or #key_listener trigger. The optional action: facet names an action to call; alternatively, add a block of statements after the event declaration.

event mouse_down action: my_action;

The event statement is defined inside a display. An action called by action: cannot take arguments. In a mouse event, #user_location gives the cursor location in model coordinates; a spatial query can find agents at that location, for example my_species overlapping #user_location.

global {
action my_action() {
write "do action";
}
}

species my_species {
}

experiment my_experiment mode: gui {
output {
display my_display {
species my_species;
event mouse_down action: my_action;
}
}
}

Define User command​

Anywhere in the global block, in a species or in a GUI experiment, a user_command statement declares a command the user can run from the interface. It either calls an existing action (with or without arguments) or is followed by a block describing what to do when the command is run. For a command declared directly in an experiment, of: selects whether it belongs to the current simulation (the default) or the experiment controller. Its syntax can be:

user_command cmd_name action: action_without_arg_name;
//or
user_command cmd_name action: action_name with: (arg1: val1, arg2: val2);
//or
user_command cmd_name {
// statements
}

For instance:

user_command kill_myself action: die;
//or
user_command kill_myself action: some_action with: (arg1: 5, arg2: 3);
//or
user_command kill_myself {
do die();
}

The name of the command is what appears on the button or in the pop-up menu: it can be a simple identifier (the command is then labelled with that name) or a string, as in user_command "Create agents". Other facets control how it is displayed: category: groups commands under a heading, color: sets the colour of the button, and icon: gives the path of an image. See Recipes > User Interaction > models > Parameters and Commands.gaml for a model that uses them.

Defining User command in GUI Experiment scope​

A user command can be declared directly inside the experiment block. By default, it targets the current simulation. Use of: experiment when the command should call experiment actions or update experiment attributes. The selected owner also determines how names in the command body and its action:, with:, when:, color: and continue: facets are resolved; there is no implicit fallback to the other owner. A simulation-owned command applies to the selected simulation, not to every simulation managed by the experiment. To affect all of them, iterate over simulations explicitly.

model quick_user_command_model

global {
action createAgent() {
create my_species;
}
}

species my_species {
aspect base {
draw circle(1) color: #blue;
}
}

experiment expe mode: gui {
user_command cmd_inside_experiment action: createAgent;
output {
display my_display {
species my_species aspect: base;
}
}
}

Commands declared in the experiment appear as buttons in the Parameters view

Defining User command in a global or regular species​

The command can also be declared inside a species, either the global one or a regular one. Here is a quick example:

model quick_user_command_model

global {
init {
create my_species number: 10;
}
}

species my_species {
user_command cmd_inside_species action: die;
aspect base {
draw circle(1) color: #blue;
}
}

experiment expe mode: gui {
output {
display my_display {
species my_species aspect: base;
}
}
}

Commands declared for a species are available in the interface for agents of that species. Commands declared in global or in an experiment are associated with those contexts. The available menu and panel locations can vary with the interface.

user_input​

Values that are not defined as parameters can be asked to the user at runtime with the user_input_dialog operator. It takes a message, displayed as the title of the dialog, and a list of input elements built with the enter and choose operators: enter("Label", default_value) for a free entry, choose("Label", type, default_value, possible_values) for a selection among a list. The operator returns a map containing the values entered by the user, keyed by their labels. The dialog is modal: the execution of the simulation is interrupted until the user dismisses or accepts it. It can be used, for instance, in an init section to force the user to input new values instead of relying on the initial values of the parameters:

model quick_user_command_model

global {
init {
map values <- user_input_dialog("Choose a number of agents to create", [enter("Number", 100)]);
create my_species number: int(values at "Number");
}
}

species my_species {
aspect base {
draw circle(1) color: #blue;
}
}

experiment expe mode: gui {
output {
display my_display {
species my_species aspect: base;
}
}
}

When running this model, the user first has to input a number:

images/input_cmd.png

Working examples are given in the model library, under Recipes > User Interaction > models > User input.gaml and Recipes > User Interaction > models > User Command.gaml.

User Control Architecture​

The other way to define user interaction is to use the user control architecture, in which an agent is driven by a panel of commands offered to the user. See user control architecture for a description of this architecture, and Recipes > User Interaction > models > User Control Architecture.gaml for an example.