Results 1 to 4 of 4

Thread: Tutorial: Programming in Lua

Threaded View

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Tutorial: Programming in Lua

    Introduction

    Lua is a dynamically typed multi-paradigm programming language written in ANSI C. Because Lua is written in ANSI C, it is cross-platform which is very beneficial to Unix users. Lua is known for it's small footprint and quick speed. Best of all, Lua is completely free.

    Installation

    In order for you to compile Lua applications, you must have the Lua run-time installed. This can be found on Lua's official site here. However, if you are a Windows user, I suggest Lua For Windows found here.

    Basics

    Comments in Lua are ignored by the compiler. They simply represent a body of text that gives information to the programmer, but doesn't actually execute. There are two types of comments in Lua, the single line comment and the block comment.

    A single line comment is represented by two hyphens:
    Code:
    -- this text is ignored by the compiler
    A block comment starts with two hyphens, followed by two inner square brackets and is finished with two hyphens and two outer square brackets:
    Code:
    --[[ This
    text is ignored by
    the compiler --]]
    Data types in Lua are: nil, boolean, number, string, user data, function, thread, and table. I will not go over all of these in this chapter, but I will go over a few.

    A nil value simply represents a non value. It is considered the absence of a value and all global variables declared in Lua are nil by default.

    A boolean value is either a True or False value.

    A number represents any number including decimals.

    A string represents text. Strings are enclosed in double quotation marks:
    Code:
    "This is a string"
    String concatenation is a way to join to strings together. String concatenation in Lua is represented by two dots:
    Code:
    "hello" .. " world!" -- this is the same as "hello world!"
    Variables

    Variables in Lua represent a value by using an identifier. Because Lua is a dynamically typed language, you do not specify the data type when declaring a variable; the compiler converts the value to the data type. The simplest way of declaring a variable is to type the variable name(the identifier), use the equal sign, and set the variable's value:

    Declaring a boolean value:
    Code:
    isTrue = true
    Declaring a number value:
    Code:
    age = 22
    Declaring a string value:
    Code:
    name = "David"
    Variables also have the option of using an access modifier. Access modifiers set the accessibility of the variable. In Lua, there are only two access modifiers: global and local.

    There is no keyword for the global access modifier. Whenever you declare a variable, it's automatically considered a global variable in that code block.

    The keyword for the local access modifier is: local. Whenever you declare a local variable, that variable can only be used within it's scope. For example, if I declare a local variable in one chunk of code, I cannot access that variable within a different chunk of code. Here is an example of declaring a variable with the local access modifier:
    Code:
    local age = 22
    Input and Output
    To print out a statement on the Lua terminal, you call the io.write() function. To read user input, you call the io.read() function.

    The io.write() function takes a string input and prints it out on the terminal. Here is an example:
    Code:
    io.write("hello world!")
    Because Lua is a dynamically typed language, if you try to pass something that is not a string in io.write(), the compiler will try to convert that data type to a string. So it is acceptable to pass a number in the io.write() function. Here is one example:
    Code:
    age = 22
    io.write("You are " .. age .. " years old.")
    The io.read() function takes a string that the user inputted into the terminal. Here is an example of setting a variable to user input:
    Code:
    io.write("Enter in your name: ")
    name = io.read()
    io.write("Hello " .. name .. "!")
    Last edited by dday9; Jan 16th, 2015 at 06:24 PM. Reason: formatting
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width