Tuesday, October 9, 2012

The 8 Basic Variable Types + Some

In Lua there are 8 basic variable types that you will find yourself using a lot. These are as follows:

Numbers
String
Boolean
Tables
Functions
Nil Values
User Data
Threads
Dynamic Typing
Querying Type

This may seem like a lot but individually they are very manageable.

Numbers

We will start with the easiest type of variable: Numbers.
To start we can do simple operations such as 2+2 by using the "print" command.
input- print ("2+2")
output- 4

We can use this method to add, subtract, divide, or any other mathematical function you may want to use.

We can easily assign a letter variable to a number by doing the following:
input- x = 2
output- none

To display the value of the variable we will need to use the print command again:
input- print (x)
output- 2

REMEMBER- you obviously have to define the variable before using the print command.

To change a variable you can simply just assign it a new value but in a complicated program it makes more sense to do the following:
x = x + 2
This way x is dependent on what x was instead of just some random value.

REMEMBER- doing this:
input- x =2
input- print (x*2)
output- 4
Does NOT change the value of x.

Strings

Similar to numbers you can assign variables the value of a string:
input- hi = "hello"
input- print (who)
output- hello
In this case hi would be the variable and "hello" would be the value assigned to it.

REMEMBER- Its important to remember that strings MUST be surrounded by either single quotes ', double quotes ", or square brackets []. We will elaborate on this later.

To link two strings together, or a string to a variable, use the .. operator.
input- print ("I want to say" .. hi)
output- I want to say hello

NOTE- The variable is NOT in quotes.

This can also be used to link variables that are numbers to strings:
input- x = 2
input- print ("I have ".. x .." apples")
output- I have 2 apples

NOTE- You can surround the variable with two strings as long as you have "" around the strings.

Boolean

Boolean values are more or less just true or falses. 
input- x = true
output- print (x)
true

NOTE- Even true wasnt a string it recognized it as a boolean so it reprinted true.

A useful operator when working with booleans is the not operator. This pretty much inverts the trues or falses.
input- print (not true)
output- false

There are several more operators that are essential to booleans. These are == equals, ~= does not equal, > greater than, < less than, >= greater than or equal , and <= less than or equal to. I will only show the first two in action because I believe that you all must have an understanding of the others.
input- print (1==0)
output- false

input-print(1~=0) 
output- true

Tables

Tables can be made using the curly brackets {}.
input- x = {}
input- print (x)
output- Table:34k233h5i

The odd code you will get is the table identifier. Do not worry if its not the same as above because its different for everyone. 

Now to make a table with some values:
input- x = { name = "pi", value = 3.14 }
input- print (x.name)
output- pi

REMEMBER- You have to have the table (in this case x) before the value in the table (table.element).
ALSO REMEMBER- Each element in the table must be separated by a comma.

Now lets do a table in a table:
input- x = { name = "pi", y = { value = 3.14 } }

How do we call upon the value element? Its simple follow the same syntax as above. 

input- print (x.y.value)
output- 3.14

Here you are defining first the table then the element in that table (which in this case is another table) and then again what value you want in the sub-table.

Functions

The basic syntax for a function is:
function nameoffunction whatthefunctiondoes end
So for example:
input- function boo() print ("bean bag") end
input- boo()
output- bean bag

If you assign a function to a variable it will act similarly to a table:
input- x = function boo() print ("this will give a code") end
output- Function: 345k345j345

Again, dont worry if you are not getting the same code.

Nil Values

Nil simply means no value. This can be useful if you are looking to destroy a variable or delete its previous assignment:
input- x = 2
input- print (x)
output- 2

Nil is also almost interchangeable with false in more advanced booleans but we will get into that later.

You can test if something is nil like this: 
input- (print x == nil)
true (or false if it has a value)

Userdata

Userdata is something foreign to Lua. This can be like something that was implemented in C. They are treated like a table. But again, we will get into this later.

Threads

Threads are any line of executable code.

Dynamic Typing

Dynamic typing means that you dont need to define what type of variable you are using when assigning them. In C you would need to do something like:
input- int x = 2
To ensure that the program knows that x is going to be an integer. In Lua we have dynamic typing so the program detects that the variable you are using is what you are using for example an integer.

Querying Type

Querying type just means that you can ask the program what type of variable any variable is. For example:
input- x = "234" <-- a string
input- print (type(x))
string
This is because the 234 was in quotes and was therefore a string. If you were to take the quotes away it would be defined as a number.



No comments:

Post a Comment