Lua Quick Start Guide

Book description

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.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Lua Quick Start Guide
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Code in Action
      4. Conventions used
    4. Get in touch
      1. Reviews
  6. Introduction to Lua
    1. Technical requirements
    2. What Lua is
      1. Source code and binaries
      2. Installing Lua on Windows 10
      3. Installing Lua on macOS
      4. Installing Lua on Linux
    3. Tools for Lua
      1. Installing VS Code on Windows 10
      2. Installing VS Code on macOS
      3. Installing VS Code on Linux
      4. Exploring VS Code
    4. Hello World!
    5. Summary
  7. Working with Lua
    1. Technical requirements
    2. Variables
      1. Creating variables
      2. Printing variables
      3. Assigning variables
    3. Comments
    4. Basic types
      1. nil
      2. Boolean
      3. number
      4. Finding a type
    5. String types
      1. String literals
      2. String length
      3. Concatenate strings
      4. String coercion
      5. Escape characters
      6. Console input
    6. Scope
      1. Scope access
      2. Global scope
      3. Shadowing
    7. Functions
      1. Defining a function
      2. Calling a function
      3. Function arguments
      4. Any number of arguments
      5. Returning a value
      6. Returning multiple values
    8. Operators
      1. Arithmetic operators
      2. Relational operators
      3. Logical operators
      4. Misc operators
      5. Operator precedence
    9. Control structures
      1. if
      2. elseif
      3. else
      4. Nesting if statements
    10. Loops
      1. while loops
      2. Infinite loops
      3. Breaking a loop
      4. Repeat until loop
      5. for loop
      6. Nested loops
    11. Summary
  8. Tables and Objects
    1. Technical requirements
    2. Introduction to tables
      1. Creating tables
      2. Storing values
      3. Table constructor
      4. Tables are references
    3. Arrays
      1. Array constructor
      2. Arrays are one-based
      3. Sparse arrays
      4. The size of an array
      5. Multidimensional arrays
    4. Iterating
      1. Understanding pairs
      2. Understanding ipairs
      3. Closures
      4. Iterator functions
    5. Meta tables
      1. setmetatable
      2. getmetatable
      3. __index
      4. __newindex
      5. rawget and rawset
      6. __call
      7. Operators
        1. Math operators
        2. Equivalence operators
        3. Other operators
    6. Objects
      1. Classes
      2. The : operator
      3. Tables inside of objects
    7. Inheritance
      1. Single inheritance
      2. Multiple inheritance
    8. Summary
  9. Lua Libraries
    1. Technical requirements
    2. The global table
      1. Explicit variables
      2. Dynamic variables
      3. Environment
    3. math
      1. Trigonometry
      2. Changing numbers
      3. Comparing numbers
      4. Randomness
      5. Constants
      6. Everything else
    4. File IO
      1. Opening a file
      2. Writing data
      3. Reading data
      4. Reading line by line
      5. Reading bits of data
      6. Closing a file
    5. Interfacing with the operating system
      1. Working with time
      2. Interacting with the shell
      3. Working with files
    6. More strings
      1. Searching for a substring
      2. Extracting a substring
      3. Case manipulation
    7. Creating and loading modules
      1. Creating a module
      2. Loading and using modules
      3. Avoiding parsing
      4. Executing files
    8. Summary
  10. Debugging Lua
    1. Technical requirements
    2. The debug library
      1. Introspective information
        1. The debug.getinfo return value
        2. Filtering the information
      2. Local variables
      3. Hooks
        1. Line ("l")
        2. Call ("c")
        3. Return ("r")
        4. Setting a counter
        5. Multiple hooks
      4. Traceback
      5. Debugger.lua
        1. Using debugger.lua
    3. Error-handling in Lua
      1. pcall and error
      2. assert
    4. Profiling
      1. The profile module
      2. Using the profile module
    5. Integrated development environments
      1. LuaEdit
        1. Breakpoints
        2. Locals and Watch
        3. Callstack
      2. Decoda
        1. Starting debugging
        2. Everything else
      3. Zero Brane Studio
        1. Selecting an interpreter
        2. Starting the debugger
      4. Visual Studio Code
    6. Summary
  11. Embedding Lua
    1. Technical requirements
    2. Working with the C API
    3. The stack
      1. Pushing to the stack
      2. Querying the stack
      3. Reading from the stack
      4. Stack size
    4. Reading Lua variables from C
      1. Loading a Lua file
      2. Reading global variables
      3. Example
    5. Creating Lua variables from C
    6. Calling Lua functions from C
    7. Calling C functions from Lua
    8. Working with tables in C
      1. Reading values from a table
      2. Writing values to a table
      3. Meta tables
    9. User data
    10. Lua C API reference
    11. Summary
  12. Lua Bridge
    1. Technical requirements
    2. API conventions
    3. Namespaces
    4. Variables
    5. Functions
    6. Properties
    7. Classes
      1. Constructor
      2. Subclass
      3. Member variables, properties, and functions
      4. Static variables, properties, and functions
    8. Calling C functions from Lua
    9. LuaRef
    10. LuaRef and tables
    11. Summary
  13. Next Steps
    1. Books
      1. Programming In Lua
      2. Learning Game AI Programming with Lua
      3. LÖVE for Lua Game Programming
      4. Lua Game Development Cookbook
      5. Game Development with Lua
      6. Beginning Lua Programming
      7. Lua Programming Gems
      8. Learn Lua for iOS Game Development
    2. Game engines
      1. LÖVE 2D
      2. Defold
      3. Corona
      4. Leadwerks
      5. Gideros
      6. Urho 3D
      7. Polycode
      8. ShiVa
    3. Game mods
      1. Roblox
      2. Garry's Mod
      3. World Of Warcraft
      4. Natural Selection 2
      5. Don't Starve
      6. Hack 'n' Slash
    4. Scriptable software
      1. CEGUI
      2. Conky
      3. Premake
      4. Moho
    5. Summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Lua Quick Start Guide
  • Author(s): Gabor Szauer
  • Release date: July 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789343229