We’re entering the summer season, and as the sun comes out and the weather warms up, there’s only one thing on everyone’s mind: math! School may be wrapping up, but that doesn’t mean you’re done with classes yet.

There have been many changes to features and functions in GB Studio since our last entry in the Basics series, and today we’ll be covering some of the new functions introduced in GB Studio 3.0+.

## What’s changed for Math Functions?

Earlier in the Basics series, we covered the many ways you can manipulate your variables with standard events like incrementing or decrementing variables, as well as the more robust [Math Functions] event. If you’re new to GB Studio, you may want to review that article first. If you’re already familiar with these events, you’re in luck, very little has changed for the core math functions in GB Studio 3.0+. The main change that has impacted math in newer versions has been the addition of 16 bit variables, which increases the minimum/maximum values of your variables. Where necessary, older articles in the Basics series will be updated to make note of which functions are no longer limited to the previous cap of 255.

## New for 3.0: Math Expression Events

GB Studio 3.0 introduced the following new Math events:

- [Evaluate Math Expression]
- [If Math Expression]

These two events can be powerful new tools to add to your scripting. By using Math Expressions, you can write a single formula to handle math that previously may have required multiple stacked events to run!

## Writing Math Expressions

Both of these new events allow you to write a “math expression” to manipulate your variables. An expression is a combination of numbers or variables and at least one mathematical operation. For example, “2 + X” (or, in GBStudio terms, 2 + $Variable 1), is an expression.

Operations are the functions we use to manipulate numbers such as addition, subtraction, multiplication, etc. In GB Studio, we can use the following operations:

Operation | Symbol |

Addition | + |

Subtraction | – |

Multiplication | * |

Division | / |

Equal To | == |

Not Equal To | != |

Greater Than | > |

Less Than | < |

Greater Than or Equal To | >= |

Less Than or Equal To | <= |

There are also some functions you can use in your expressions as well:

Function | Results / Use |

min(a,b) | Returns the lower value between a or b |

max(a,b) | Returns the higher value between a or b |

abs(a) | Returns the absolute value of a |

You can also combine multiple expressions for more advanced cases:

Operation | Symbol | Syntax Example |

OR | || | $Local 1 == 5 || $Local 2 == 5 |

AND | && | $Local 1 > 0 && $Local 1 < 2 |

Now that you know the expressions you can use, let’s dive into the two events and see some examples of how we could start using these in our games!

### Evaluate Math Expression

On its own, the [Evaluate Math Expression] event is quite similar to [Math Functions], but now you have more control over the length or complexity of the event. This event has two main components: the Variable you want to use, and the Expression you want to run.

When this event is used in a script, the expression will be calculated, and the result is stored in the variable you selected. With [Math Functions], this may have required multiple events before achieving your result, but now you can write an expression to do this in a single event!

#### Example:

If your game has an HP mechanic, you know you’ll need to track your character’s health in a variable. When your character uses an item to restore their HP, you need to make sure your variable doesn’t go over a limit, too.

Previously, you probably had a script like this:

But, with [Evaluate Math Expression], we can reduce this entire check into a single line using the “Min” operator and some addition!

This will now set the $HP variable to whichever is lower: your HP plus 5, or your $Max HP variable.

### If Math Expression

Similar to the [If Variable Compare to Value] and [If Variable Compare to Variable] events, the [If Math Expression] event does not directly change a variable. Instead, it will run the contained script if the expression it checks is True, or will run a different result if false.

To use this event, you’ll need to write an expression that can be True or False.

Usually you’ll want this to contain an Equals (==) operator (or variation, like <=, !=, etc) so that you can have a result. Without that, the event will only check the value, and if you recall from earlier Basics articles, any non-zero value is considered True.

With [If Math Expression], you can create conditions for more advanced comparisons. For example, if you had a puzzle where a player needs to collect multiple pieces, you could use [Compare to Variable] or [Compare to Value] when checking your result to see if your variable was equal to the correct amount. But, what if the player could pick up more pieces than they needed for the solution?

With a Math Expression, we can easily write that in one event. Let’s say your NPC already has 2 pieces, and they need 5 total. If we allowed the player to collect more than 3 $Pieces before returning to the NPC, we might write something like this:

This is just one simple example. As you can see, these can easily get more complex!Express yourself!

## Express yourself!

With these new events, you can use expressions to create more advanced item logic or damage formulas, or simply use them to reduce the number of scripts needed for simple checks like these. Now that you’ve added two new events to your scripting skills, you can spend your summer vacation having fun with math!

Friendly Neighborhood Game Designer, Podcaster, and (ex)Librarian (he/him)

GB Studio Games | Tabletop Games