Lua Quick Start Guide
eBook - ePub

Lua Quick Start Guide

The easiest way to learn Lua programming

Gabor Szauer

Share book
  1. 202 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Lua Quick Start Guide

The easiest way to learn Lua programming

Gabor Szauer

Book details
Book preview
Table of contents
Citations

About This Book

The easiest way to learn Lua programming

Key Features

  • The easiest way to learn Lua coding
  • Use the Lua standard libraries and debug Lua code
  • Embed Lua as a scripting language using the Lua C API

Book Description

Lua is a small, powerful and extendable scripting/programming language that can be used for learning to program, and writing games and applications, or as an embedded scripting language. There are many popular commercial projects that allow you to modify or extend them through Lua scripting, and this book will get you ready for that. This book is the easiest way to learn Lua. It introduces you to the basics of Lua and helps you to understand the problems it solves. You will work with the basic language features, the libraries Lua provides, and powerful topics such as object-oriented programming. Every aspect of programming in Lua, variables, data types, functions, tables, arrays and objects, is covered in sufficient detail for you to get started. You will also find out about Lua's module system and how to interface with the operating system.

After reading this book, you will be ready to use Lua as a programming language to write code that can interface with the operating system, automate tasks, make playable games, and much more. This book is a solid starting point for those who want to learn Lua in order to move onto other technologies such as Love2D or Roblox.

A quick start guide is a focused, shorter title that provides a faster paced introduction to a technology. It is designed for people who don't need all the details at this point in their learning curve. This presentation has been streamlined to concentrate on the things you really need to know.

What you will learn

  • Understand the basics of programming the Lua language
  • Understand how to use tables, the data structure that makes Lua so powerful
  • Understand object-oriented programming in Lua using metatables
  • Understand standard LUA libraries for math, file io, and more
  • Manipulate string data using Lua
  • Understand how to debug Lua applications quickly and effciently
  • Understand how to embed Lua into applications with the Lua C API

Who this book is for

This book is for developers who want to get up and running with Lua. This book is ideal for programmers who want to learn to embed Lua in their own applications, as well as for beginner programmers who have never coded before.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Lua Quick Start Guide an online PDF/ePUB?
Yes, you can access Lua Quick Start Guide by Gabor Szauer in PDF and/or ePUB format, as well as other popular books in Informatica & Linguaggi di programmazione. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789340136

Working with Lua

In Chapter 1, Introduction to Lua, you learned how to set up Lua and Visual Studio Code. At the end of the chapter, we created a simple Hello World application. In this chapter, you will learn the basics of Lua programming. Topics such as variables, function data types, and loops are all going to be covered. By the end of this chapter, you should be familiar enough with Lua as a language to put together some simple programs.
If this is your first time programming, the syntax of Lua can get overwhelming fast. Many resources can be found on the official Lua site at https://www.lua.org/. For a quick example of Lua, check out http://tylerneylon.com/a/learn-lua/.
By the end of this chapter, you will will have a solid understanding of the following:
  • Using variables
  • Data types
  • Working with functions
  • Operators
  • Code blocks
  • Variable scope
  • Code flow

Technical requirements

You will be required to have JavaScript programming language. Finally, to use the Git repository of this book, the user needs to install Git.
The code files of this chapter can be found on GitHub:
https://github.com/PacktPublishing/Lua-Quick-Start-Guide/tree/master/Chapter02
Check out the following video to see the code in action:
http://bit.ly/2LDVPd0

Variables

Variables are labels that provide a descriptive name for some data that a program can read or modify. You can literally think of a variable as a label.
For example, let's assume there are a number of jars containing different colored jam. How do you know what flavor a specific jar contains? Hopefully, there is a label on the jar that is descriptive of its content.
The labels on the jar can change over time. For example, a jar might contain strawberry jam, but after that's gone it might be filled with peach jam. When the contents of the jar changes, a different label can be used to describe what's in it. Variables work in a similar fashion.

Creating variables

To create a variable, you need to do two things:
  • Declare the variable
  • Assign a value (data) to the variable
As an example, let's make a variable, foo, and assign it the value bar. The code to do this would be:
foo = "bar"
That single line of code declares a variable and assigns a string value to the variable. If you break it into several parts, the actual line of code consists of the following pieces:
Why are there quote marks around bar? What is a string value? These questions will be answered in the coming two sections, Basic types and String types.

Printing variables

How can you tell what the value of a variable is? One way is to print the value out to the console. To print the value of a variable, you first type the keyword print, then the name of the variable between parentheses (). The full syntax is:
print (<variable>)
For example, we can check the value assigned to foo with the following code:
foo = "bar"
print (foo)
The first line of code creates a variable named foo and assigns it the string value "bar". The second line prints the value of the foo variable. This means bar will be printed to the console:
Where does the print keyword come from? What's a keyword? Why do we use parentheses when using print? These questions will be answered in the Functions section of this chapter.

Assigning variables

Since a variable is just a description of the underlying data, the data can change. For example, if you have a variable named time, you would expect its value to change every second. At any point, you can use the assignment operator = to assign a new value to a variable.
This code snippet explores this by creating a single variable, color, and assigning it three different values. The value of color is printed after each assignment:
color = "red"
print (color)
color = "green"
print (color)
color = "blue"
print (color)
The output from this program should look like this:

Comments

In Lua, any time you see --, the rest of that line is considered a comment. Comments are there to help you read and understand code, but they are never executed. This example demonstrates how comments are used:
foo = "bar"
-- print (foo)
-- The above statement never prints
-- because it is commented out.

Basic types

In the last section, you were introduced to the concepts of a variable and a value. This section explores the concept of what a value is. Every value has a data type, which intuitively describes what kind of data the value holds. Lua supports eight basic value types:
  • nil: The absence of data. This type represents literal nothingness. If a certain piece of data is invalid or unknown, nil is usually the best way to represent that it is invalid or unknown.
  • Boolean: A value of true or false. A Boolean value is binary and can only ever be in one of two states, true or false.
  • number: A number can represent any real number: 0, -1, 5, or even decimals such as 3.14159265359.
  • string: A string is an array of characters. When declaring a string literal, it must be "enclosed within quotation marks."
  • function: A function is some code that is referred to by a name and can be executed any time.
  • table: A table contains information using key-value pairs. Tables will be covered in depth in Chapter 3, Tables and Objects.
  • userdata: Complex data structures defined in the C programming language.
  • thread: Threads can be used to execute code in parallel. Instead of your code running one set of commands, it can run several sets of commands at the same time.
This section will explore the nil, Boolean, and number types. The string and function types will get their own sections in this chapter. The table type is so important it will have its own chapter.
Lua uses loose, implicit types. That means a variable can have any type. Once a variable is assigned a type, it can be assigned any other type. For example, it is valid to assign a number to a variable that holds a string. After the assignment, the variable will simply hold a number.

nil

A nil value represents the absence of data. If you try to access a variable that has not been created yet, its value will be nil. If you are done using a variable, you should assign it to be nil. This code first prints nil because nothing is assigned to the variable foo. Then, the string bar is assigned, and after this the code prints b...

Table of contents