Data and Variables

Overview

Computer programs perform computations just like a calculator, but also allow you to keep track of meaningful values that change. These labeled changing values are called variables.

Variables

What a Variable Is

A variable is a label paired with some data. Making one allows you to write code using that value throughout your program.

Creating a Variable

To create a variable, we use the special keyword local, give the variable a name, and then use = to assign it to a value.

Examples:

local myCoolVariable = 13 
local variableLabel = "variable data"Code language: Lua (lua)

Naming Conventions

Variable names cannot have spaces, so in the above examples, we used capitalization to visual separate new words. This style of word separation is called CamelCase.Besides spaces, you cannot have other punctuation (besides _ which you would use if you want to use snake_case instead), and can’t use be of lua’s special keywords (like local, for example).

Most of the time, variables will start with a lower-case letter, unless they are never supposed to change. Then people generally use ALLCAPITALLETTERS to name them.

Data Types

Now that you know how to name your variables, you can start thinking about the kinds of information that they will label. There are three types of data that we will focus on in this lesson, numbers, strings, and Booleans.

Numbers

Numbers are any of the numerical values you may want to save, from negative to positive to tiny decimal values to massive ones.

Examples:

Lua recognizes numbers automatically, and they do not need to be surrounded by any special punctuation.

local aSmallNumber = 3 
local aBiggerNumber = 9999999 
local aTinyDecimal = 0.0000001 
local aNegativeNumber = -700Code language: Lua (lua)

Decimal numbers less than 1 will need to start with a 0.

Integers and Floating Point Decimals

Many programming languages treat integer numbers (whole numbers without any decimals) as entirely different from floating point decimal numbers (any number with a decimal), and won’t let you just add or multiple them. Usually they are called ints and floats. Lua does see these as distinct types, but will do the work of converting between them for you.

Strings

Strings are the data type that you use to save any word, sentence, or really anything that doesn’t fit in another category.

Examples:

To make a string, surround the text in ""

local aString = "Hello, world!" 
local someonesName = "Elijah" 
local errorMessage = "Error: My program doesn't know what to do here"Code language: Lua (lua)

Special Characters

Some characters are more difficult to add to strings than others. The " character is a good example, because as soon as you use one, you end the string.

You can use the \ to add those characters back in:

  • \" will let you write “
  • \\ will let you write \
  • \n is a new line character, what you get when you press Enter.

Booleans

Booleans are a data type that can only ever be either true or false. They are very useful for scenarios where you need to flip between two states, like a light switch.We will discuss Booleans in more depth in the next lesson when we talk about Conditionals.

Nil

Nil is the value you assign to variables that haven’t been assigned real values yet. It is a placeholder and often shows up in errors when a variable hasn’t been defined, or is misspelled, but you can also use it intentionally to only give something a value if something else happens.

Example:

local winner = nil -- We don't know who won yet, so we don't give the variable a value.Code language: Lua (lua)

Operating on Data

Math

You can do any operation you could do with a calculator in code with Lua, but some use different characters than you might expect so that they can be easily typed.

  • Add with +
  • Subtract with -
  • Multiply with *
  • Divide with /

Modulus

Another useful operation is % called modulus. This give you the remainder when you divide. You can use it to check if a number is divisible by another.
Example:

-- checking if a number is odd or even 

local myNumber = 13 print (myNumber%2) 
-- would be 0 if the number were even (divisible by 2), but it was 1 because 13 is odd.Code language: PHP (php)

Lua Math Library

Lua has a special set of functions (we will discuss these more later) that you can use for more complex math.

A few examples;

  • math.pi is the value of pi, 3.14…
  • math.floor(yourNumberHere) will round your number down. This is very useful when you just need to chop the decimal off of a number.

You can see a complete list of these operations here.

Special Core Math Operations

Core has a couple of math operations that were added to make your life easier.

  • CoreMath.Round(yourNumberHere) will round your number.

See the full list in the Core Documentation.

Strings

Strings don’t have as many operations as numbers, but can still be manipulated and changed in many different ways. You can use .. to combine two strings into one, called concatenation.

Example:

local newString = "Now is the time. " .. "You have the power"Code language: Lua (lua)

Special Core String Operations

Core also adds a few string operations, which you can read about on Core documentation.

Changing a Number to a String, String to a Number.

You can change a string of numbers into a number data value, and a number into a string:

Examples:

local someNumbers = "123" 
local numberVersion = tonumber(someNumbers) -- will be the number 123 

local aNumber = 5678 
local stringVersion = tostring(aNumber) -- will be "5678"Code language: Lua (lua)
Lesson Content
2 Comments
Collapse Comments

What does the two .. mean? I see this in further examples.

CommanderFoo (Administrator) June 16, 2022 at 4:45 am

It’s is the concatenation operator in Lua.

https://www.lua.org/pil/3.4.html

Leave a Comment

Scroll to Top