Lua Quick Start Guide
eBook - ePub

Lua Quick Start Guide

The easiest way to learn Lua programming

Gabor Szauer

Partager le livre
  1. 202 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Lua Quick Start Guide

The easiest way to learn Lua programming

Gabor Szauer

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Lua Quick Start Guide est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Lua Quick Start Guide par Gabor Szauer en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming Languages. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
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 des matiĂšres