Lua Quick Start Guide
eBook - ePub

Lua Quick Start Guide

The easiest way to learn Lua programming

Gabor Szauer

Condividi libro
  1. 202 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Lua Quick Start Guide

The easiest way to learn Lua programming

Gabor Szauer

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Lua Quick Start Guide è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Lua Quick Start Guide di Gabor Szauer in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Programming Languages. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781789340136
Edizione
1

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...

Indice dei contenuti