Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Creating a module

A module is just a normal Lua table; a module file is a Lua file which returns a table. For us, this means returning an anonymous table. We are going to create a new file, character.lua, and declare a character class in this file. The definition of the character class is as follows:

-- It's important that the table retuned be local!local character = {}character.health = 20character.strength = 5character.name = ""character.new = function (self, object)     object = object or {} -- Use provided table, or create new one    local provided = ""    if type(object) == "string" then        provided = object        object = {}    end    setmetatable(object, self) -- Assign meta table    self.__index = self    if provided ~= "" then        object.name = provided    end return ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content