Basic math

From Goodblox Wiki
Revision as of 15:59, 22 June 2020 by Pizzaboxer (talk | contribs) (Created page with "== Introduction == This tutorial is intended for beginners. It is intended to show you how to do mathematical calculations with GoodBlox Studio. If you haven't already,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Introduction

This tutorial is intended for beginners. It is intended to show you how to do mathematical calculations with GoodBlox Studio. If you haven't already, please see Your first script as a beginner tutorial.

What this tutorial will teach you

  • How to do basic arithmetic with a script
  • What the arithmetical operators are
  • The use of parentheses

Setup

You will need to open GoodBlox Studio. Once you have done that, you will need to click "My GoodBlox", select your map, and click Edit

You will need some essentials in GoodBlox Studio to make this work:

  • The Output window:

  • The Explorer window:

and

  • The Command bar:

  • To make sure you have the Output window, click View / Output.
  • To make sure you have the Explorer window visible, click View / Explorer.
  • To make sure you have the Command bar visible, click View / Toolbars / Command.

Arithmetical operators

Lua and GoodBlox can do mathematical calculations, much like a calculator. In Lua, there is:

  • + Addition (e.g., 2+3=5)
  • - Subtraction (e.g., 5-2=3)
  • * Multiplication (e.g., 5*2=10)
  • / Division (e.g., 10/2=5)
  • - Negation (e.g., -(2)=-2)

Script

A script of:

print(2+3)
print(5-2)
print(5*2)
print(10/2)
print(-(2))

will therefore yield the results of 5, 3, 10, 5, and -2. You can work with decimals and negative numbers as well. Try inserting this script into Workspace and running it to confirm this.

You can assign numbers to Variables and do these equations as well:

x=2
y=3
print(x+y)
print(x-y)
print(x*y)
print(x/y)

Which gives us 5, -1, 6, and 0.66666666666667. These are very simple examples (i.e., you don't have to limit yourself to just two variables).

Parentheses

Parentheses can be used in Lua much as they are used in algebra. If you want something calculated first, put that in parentheses.

print((10/20)/5) -- is equal to .5/5, which is .1
print(10/(20/5)) -- is equal to 10/4, which is 2.5

Following the associative properties of multiplication and addition, it does not matter how you organize the parentheses in certain instances:

print((10+20)+5) -- is equal to 30+5, which is 35
print(10+(20+5)) -- is equal to 10+25, which is 35

print((10*20)*5) -- is equal to 200*5, which is 1000
print(10*(20*5)) -- is equal to 10*100, which is 1000

See Also

Arithmetic Operators