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

Tables inside of objects

The object system described only works if a class contains values; it falls apart when the class contains a reference (like a table). This happens because tables are passed by reference not value. When the __index meta function returns a table contained in a class, there is no new copy of that table, just a reference that is shared among every instance of the class. The following code demonstrates this:

Character = {    alive = true}Character.position = {  x = 10, y = 20, z = 30}Character.new = function(self, object)  object = object or {}  setmetatable(object, self)  self.__index = self  return objectendplayer1 = Character:new()player2 = Character:new()player1.position.x = 0player2.position.y = 10print ("Player 1, position: ...
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