# Formula

** Formula** is a powerful unit that allows you to evaluate logical and mathematical expressions directly via a textual

__Formula__and a list of

__Arguments__.

**Performance**: At the moment, using the formula unit is significantly slower than using the operator units individually, because of the binary tree traversal overhead (despite caching attempts). It is preferable to avoid using this unit at every frame.

Most often, you'll want a formula to return either a __ boolean__ for logic, or a __ number__ for math, but formulas can return any type of value.

A new formula looks like this:

The first text field in the header is the formula itself.

The second text field is the amount of arguments. It's set to 2 by default, giving us A and B as inputs. Formulas can have up to 10 arguments, which are always ordered alphabetically. If you add more, they will be called B, C, D, E, and so forth.

For example, this formula returns a boolean indicating whether at least __10__ seconds have elapsed since the start of the game and the current object's name is __Player__: (*because... reasons*).

## Variable Names

You can also directly use variable names in the formula. For example, if you have a graph variable named __health__, you could return a boolean just by typing the formula __health > 50__. The argument names are evaluated in the following order of priority:

- Alphabetical argument names (
__a__-__z__) - Graph variable names
- Object variable names
- Scene variable names
- Application variable names
- Saved variable names

## Properties and Methods

You can retrieve the value of a property on an argument or variable by using the __[arg.prop]__ notation.

For example, if __position__ is a __ Vector 3__ object variable, you can check if it is equal to zero with: __[position.x] = 0__ .

You can also get the return value of parameterless methods with the __[arg.Method()]__ notation.

## Literals

You can use the following literals:

Literal | Description | Example |
---|---|---|

Number | An integer or float. | 3.5 |

String | A piece of text between apostrophes. | "Hello World!" |

Boolean | A boolean value. | true, false |

Null | The null constant. | a != null |

Delta Time | The Unity frame delta time. | 30 * dt |

Invert Delta Time | The inverse of the delta time. | 30 / second |

## Operators

You can use every common logical and mathematical operator in formulas, even the ones defined through custom operator overloading in script. Here's an overview:

Operator | Operation | Rank | Result | Example |
---|---|---|---|---|

not, ! |
Logical Negation | Unary | The opposite of the operand. | not true |

- |
Numerical Negation | Unary | The negative of the operand. | -5 |

and, && |
Logical And | Binary | True if both operands are true. | (a < 5) and (b > 3) |

or, || |
Logical Or | Binary | True if either operand is true | (a < 5) or (b > 3) |

=, == |
Equality | Binary | True if the two operands are equal. | a = b |

!=, <> |
Inequality | Binary | True if the two operands are not equal. | a != b |

<, <=, >, >= |
Numeric Comparison | Binary | The result of a numeric comparison | a >= 10 |

+ |
Addition | Binary | The sum of the two operands. | a + 5 |

- |
Subtraction | Binary | The difference between the two operands. | b - 3 |

* |
Multiplication | Binary | The product of the two operands. | 12 * a |

/ |
Division | Binary | The quotient of the two operands. | b / 2 |

% |
Modulo | Binary | The remainder of the division of the two operands. | a % 2 |

?: |
If | Ternary | The left operand if the condition is true, otherwise the right operand. | (health > 0) ? "Alive" : "Dead" |

All common bitwise operators like __~__ and __>>__ are also supported.

## Functions

You can also use any function from this table:

Name | Result | Example |
---|---|---|

abs |
The absolute value of a specified number. | abs(-1) |

acos |
The angle whose cosine is the specified number. | acos(1) |

asin |
The angle whose sine is the specified number. | asin(0) |

atan |
The angle whose tangent is the specified number. | atan(0) |

ceiling |
The smallest integer greater than or equal to the specified number. | ceiling(1.5) |

cos |
The cosine of the specified angle. | cos(0) |

exp |
e raised to the specified power. | exp(0) |

floor |
The largest integer less than or equal to the specified number. | floor(1.5) |

log |
The logarithm of a specified number. | log(1, 10) |

log10 |
The base 10 logarithm of a specified number. | log10(1) |

max |
The larger of two specified numbers. | max(1, 2) |

min |
The smaller of two numbers. | min(1, 2) |

pow |
A specified number raised to the specified power. | pow(3, 2) |

round |
Rounds a value to the nearest integer or specified number of decimal places. | round(3.222, 2) |

sign |
1 if the number is positive, -1 is if it negative. | sign(-10) |

sin |
The sine of the specified angle. | sin(0) |

sqrt |
The square root of a specified number. | sqrt(4) |

tan |
The tangent of the specified angle. | tan(0) |

truncate |
The integral part of a number. | truncate(1.7) |

v2 |
Creates a 2D vector. | v2(0, 0) |

v3 |
Creates a 3D vector. | v3(0, 0, 0) |

v4 |
Creates a 4D vector. | v4(0, 0, 0, 0) |

Customer support service by UserEcho

[position.x] = 0No Idea how it is used in a formular, no real example, well not really actually anywhere.

Typing this expression will return true if the X component of the position is zero, false otherwise.

Could you guys move the "Performance" box to the top -- and maybe when a newer Bolt is released you can let us know the performance issues are fixed? When I saw this I was really tempted to use it.

Moved to the top!

The "truncate" function doesn't work here. (ArgumentException: Function not found)

Also feeding in different values always gives me back the first result, when I use functions. If for example, I feed in the index (0-100) of a For Loop and then do the function "round (a)" I get "0" back a hundred times. Is that intended?