Book Image

Lua Quick Start Guide

By : Gabor Szauer
4 (1)
Book Image

Lua Quick Start Guide

4 (1)
By: Gabor Szauer

Overview of this book

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.
Table of Contents (10 chapters)

Calling C functions from Lua

All of the code that we have written so far has been about exposing C to Lua using Lua Bridge. Any C function exposed through Lua Bridge can be called from Lua. If a function is in a namespace and not a class, it is called with the dot syntax: Math.Sqrt(16). But, if a function is in a class, it needs to be called with the colon syntax: vector:Normalize(). The following code shows how to expose a C function to Lua and how to call it from Lua.

The C code needs to declare the appropriate vector 3 class, a Normalize member function, and a global dot product function. Next, the Register function exposes all of these functions to Lua, using Lua Bridge:

class Vec3 {
public:
float x, y, z;

float Normalize() {
float dot = x * x + y * y + z * z;
if (dot == 0) {
return 0;
}
return sqrt(dot);
}
}

float Dot(Vec3...