July 2018
Beginner
202 pages
5h 42m
English
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: ...