Math Expression

2 min read

In the editor, press the (x) button in the toolbar to open Diagram parameters, press New parameter and choose Math expression (“Compute a value from a formula”). Use this variable type to define values based on functions available in the math.js library.

The New parameter dialog with Math expression selected: Name, a Formula box and Update on (Each Play or Each Step).The New parameter dialog with Math expression selected: Name, a Formula box and Update on (Each Play or Each Step).

Name

Use the variable’s name to reference it in the Formula fields of nodes and connections throughout your diagrams. See the example below.

The Properties panel of a Resource Connection with Type set to Formula and a parameter name typed in the formula box.The Properties panel of a Resource Connection with Type set to Formula and a parameter name typed in the formula box.
Using a parameter name in a Formula field

Formula

Machinations supports functions from the math.js library.

Every formula box has a Formula builder button (sparkle). Use it to search functions, insert a component’s value or a parameter, and ask AI to suggest a formula.

You can also reference other parameters (called custom variables in older guides) in the formula. In the example below, our Modifier variable will add +1 to the existing variable RNG.

The New parameter dialog with the name loot_bonus and the formula loot_value + 1, which refers to another parameter.The New parameter dialog with the name loot_bonus and the formula loot_value + 1, which refers to another parameter.

Update on – when the value is generated.

  • Each Play – at the beginning of each Play. The value will be the same throughout the execution of the diagram.
  • Each Step – the value will be randomly generated at the beginning of each execution step.

Description

Use this field to describe what the formula computes.

Using Arrays

Arrays are a formidable way to declare variables with multiple values inside them. Machinations allows for the use of one- and two-dimensional arrays.

One-dimensional arrays

One-dimensional arrays only contain numbers separated by comma inside squared brackets like this:

The New parameter dialog with the name oneDimensional and the formula [1, 2, 3, 5, 8, 13].The New parameter dialog with the name oneDimensional and the formula [1, 2, 3, 5, 8, 13].

The syntax to fetch data from this array is ArrayName[position]

Example:

Using the Array and sub-array example abovey you can access its values like this: oneDimensional[5]. The result will be 8.

Two-dimensional arrays

Two-dimensional arrays contain multiple arrays seperated by a comma inside squared brackets:

The New parameter dialog with the name twoDimensional and the formula [[1, 2, 3], [4, 5, 8]].The New parameter dialog with the name twoDimensional and the formula [[1, 2, 3], [4, 5, 8]].

The syntax to fetch data from this two-dimensional array is ArrayName[sub-array,position]

Example:

Using the Array and sub-array example abovey you can access its values like this: twoDimensional[2,3]. The result will be 8.