The Language

From GiderosMobile
Revision as of 16:54, 10 September 2018 by Anthony (talk | contribs)

Assignments and Variables

Lua is a dynamically-typed language, that is, variables don't have types, only the values. All values carry their own type. Consider the following as an example:

MyTable = wide
width = 3; height = 1.5
area = width * height
print (area)

You can nest several lines using semicolons, but it’s optional and generally not a good programming habit as it decreases the readability of your code.Be careful not to use reserved words. Gideros Studio will complain when you use a reserved word in Lua. Consider this example:

local = 1

Since Lua is a small language, it only has 8 basic data types:

  1. nil (null)
  2. Booleans
  3. Numbers
  4. Strings
  5. Functions
  6. Userdata
  7. Threads
  8. Tables

Global variables in Lua do not need to be declared. If you want to declare a variable, just assign a value to it. Giving it a nil value removes the variable. In fact, when a variable is removed, and it’s not referenced by another variable, it’s ready to be cleared up from memory, thanks to the wonderful Lua garbage collector.

Lua can support multiple assignments. Check the following assignment:

a, b, c = 1, 2, 3

-- Easy method to swap two variables
x, y = y, z      

-- Function can return multiple values
n, m = calculate (phi, zeta)

Control Structures

Lua provides a strong control mechanism between different variable types. As an example for logical decisions, consider the following:

Logical Decision Meaning
A and B If A is true, then return the result of expression B.

If A is false, then return A

A or B If A is true, then return the result of expression A

If A is false, then return B

Arrays

Arrays that can be indexed not only with numbers, but with any value. Arrays do not have to be defined a size, and they can grow as needed. When an array is defined, and you try to reach a value exceeding the boundary, you get a nil (instead of zero).

Arrays can have any index, e.g you can start with an array with -5, 0 or 1 - it’s all up to you. Consider the following example:

-- creates an array with indices from -2 to 2
array = {}
for i=-2, 2 do
  array[i] = "apples"
end

Lua also supports multi-dimensional arrays, which we call "matrices".

Consider the following example. A similar array syntax can be used to create string lists. The statement below:

animals = {"giraffe", "boar", "wolf", "frog"}

is equal to:

animals = {}
animals[1] = "giraffe";  animals[2] = "boar"
animals[3] = "wolf"; animals[4] = "frog"

Lua Tables (arrays)

In Lua, Tables and Arrays are terms so used interchangeably, However this is true to an extent.


Basic Functions

Lua has the ability to call its own functions or C functions, and it handles functions as a "data type". Note that functions can return multiple values, and you can use multiple assignments to collect these functions.

function calculate(w, d, h)
  if h > 4 then
    print ("height of the room cannot be more than 4”)
    return
  end
  volume = w * d * h
  return volume
end

-- Calculate the volume of the room
print (calculate (4,3,4))

Try the function above, this time with values 4,5,5.