I do most of my Python programming, and I use OOP methods for most of my projects. Recently, I began to study the library of games and the Love2D engine. I managed to tweak some settings, and then thought about creating a GameObject class. But what is it? Lua has no classes! It has tables, meta tags, and other similar things. I'm having problems with heads or tails, even after reading the documentation several times.
Consider the following example:
catClass = {}
catClass.__index = catClass
catClass.type = "Cat"
function catClass.create(name)
local obj = setmetatable({}, catClass)
obj.name = name
return obj
end
cat1 = catClass.create("Fluffy")
print(cat1.type)
cat2 = catClass.create("Meowth")
cat1.type = "Dog"
print(cat1.type)
print(cat2.type)
print(catClass.type)
The result of this is as follows:
Cat
Dog
Cat
Cat
I don’t understand why changing cat1.type in "Dog" does not cause the same changes in cat2 and catClass. Does metadata create a copy of the table? Google did not provide useful results (very few good Lua explanations).