In this tutorial, we learn how to use buttons and properties to create a functional calculator! This calculator has the 4 basic functions of math, a game display to show the most recent calculation, and a working number changer.
This is what you will need:
- 11x Buttons
- 5x Triggers
- 4x Properties
- 2x Counters
- Text to label the operations
- A way to show the result. I’m using a game overlay.
Part 1 - Updating Counters
Let’s set up the counters. Make them global, start at 0, and update properties for both of the counters. The properties should be numbers, and be called values 1 & 2. Put buttons around the counters. Wire the buttons so that the top one increases the counter, the side one resets it to zero, and the bottom one decreases it. Feel free to make the buttons describe their actions.
Part 2 - Setting the Operators
Create text to show which button sets which operator. Connect the buttons to the triggers with wires. The button should activate the trigger. GO into the blocks for the triggers and make it so that when the trigger is activated, set the property equationType to that operator.
Part 3 - Creating the calculator
Add a trigger under the button on the right. Connect the button to the trigger with wires. This button will run the equation. Go into the trigger and add a new block.
Create 3 variables: a, b, and operator. Set a and b to values 1 and 2 and operator to equationType.
Create 4 if statements. Each one will check if operator is adding, subtracting, multiplying, or dividing. The triggers from the last step will define what the if is looking for.
In each of the statement, use the set property block. We want to set the output to what the operator says based on the 2 values. Basically, the addition one will output a + b, the subtraction one will output a - b, and so on.
Part 4 - Showing the text
Make the trigger transmit on a new channel, showResult. Then, make a new block in the game overlay that shows the result as text.
That’s it! This calculator is quite simple. If you want, you can add more operators, like exponents, roots, and modular functions. Thanks for reading!
(Also, I don’t know if this bug happens for you, but I have to press the button twice for the result to update. Dunno how to fix it).