At the internet I have found two different solutions for make table based oop in Lua. I saw that the method calls are similar but the 'constructors' are quiet different.
I would to know which solution has more advantages/is the better one. I know there is a also a closure based oop in Lua, but here I'm only interested in the table based solutions.
Version 1 (I prefer this syntax)
Citizen = {name}
--Constructor
function Citizen:new(o, name)
o = o or {}
setmetatable(o, self)
self.__index = self
o.name = name
return o
end
--Getters--
function Citizen:getName()
return self.name
end
...
Version 2
Citizen = {}
Citizen.__index = Citizen
setmetatable(Citizen, {
__call = function(cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,
})
--Constructor--
function Citizen:_init(name)
self.name = name
end
--Getters--
function Citizen:getName()
return self.name
end
...
Related
I'm still fairly new to OOP but I can't figure out what I'm doing wrong here.
-- TestModule
testClass = {}
local thingClassModule = require(script["ThingModule"])
function testClass:new()
setmetatable({}, self)
self.__index = self
self.Thing = thingClassModule:new(10, 15)
self.Thing2 = thingClassModule:new(30, 70)
end
return testClass
the thing class module:
-- ThingModule
thing = {}
function thing:new(a, b)
local obj = {}
setmetatable(obj, self)
self.__index = self
self.A = a or 0
self.B = b or 0
return obj
end
return thing
The issue is that Thing gets overriden by Thing2.
I believe your problem is not assigning self to the metatable of the object instead it's being assigned to the module instead, therefore it's overwriting the data instead of creating a new object.
Here's a working example of what you're trying to achieve:
-- Thing Class
local Thing = {}
Thing.__index = Thing
function Thing.new(a, b)
local self = {}
setmetatable(self, Thing)
self.A = a
self.B = b
return self
end
return Thing
local Thing = require(script.Parent.Thing)
local TestClass = {}
TestClass.__index = TestClass
function TestClass.new()
local self = {}
setmetatable(self, TestClass)
self.Thing = Thing.new(10, 15)
self.Thing2 = Thing.new(30, 70)
return self
end
return TestClass
You can learn more about Object-oriented through this great article:
https://devforum.roblox.com/t/all-about-object-oriented-programming/8585
In a method, self is whatever comes before the colon in the method call. In thing.new, you create a new table called obj, but you then assign A and B within self, which is the thing table (or thingClassModule; they're both the same table). In testClass.new, you set the metatable of a new table, but you don't store it to a variable. You then go on to modify self, which is testClass.
I am building a game with enemies and players, all of which are set up to have various states for animation and behavior. The parent class for both is Entity.lua, from which they inherit variables and methods. However, while both enemies and players are inheriting the variables, for some reason the enemies do not inherit the methods. So, if I try to call snakey:changeState('search'), for example, it gives me an error message "Attempt to call method 'changeState' (a nil value)".
I have used the same sequence for creating entities in several games in the past and never had this problem. In fact, if I create the Player in the same way, file and location as the enemies, I receive no error messages.
Here is the code where I create the entities.
local snakey
snakey = Snakey {
platform = platform,
player1 = self.player1,
player2 = self.player2,
stateMachine = StateMachine {
['search'] = function() return SnakeySearchState(snakey) end,
['chasing'] = function() return SnakeyChasingState(snakey) end,
['idle'] = function() return SnakeyIdleState(snakey) end
}
}
-- snakey:changeState('search')
-- snakey.stateMachine:change('search', params)
table.insert(self.entities, snakey)
The two coded out lines are where I noticed the problem. The first line gives and error and the second does work, but is not satisfactory because it is a work-around.
Here is the code for Entity.lua: I don't include details of the functions for brevity, but all are working properly for when player calls them.
Entity = Class{}
function Entity:init(def)
-- position
self.x = def.x
self.y = def.y
self.gravity = 6
-- many more variables
end
function Entity:changeState(state, params)
self.stateMachine:change(state)
end
function Entity:update(dt)
self.stateMachine:update(dt)
end
function Entity:collides(entity)
-- do something
end
function Entity:onDamage()
-- do something
end
function Entity:render()
- renders sprite
end
Player code (in brief)
Player = Class{__includes = Entity}
function Player:init(def)
Entity.init(self, def)
-- more variables
end
function Player:update(dt)
Entity.update(self, dt)
end
function Player:render()
Entity.render(self)
end
And perhaps the trouble spot, one one enemy's script
Snakey = Class{__includes = Entity}
function Snakey:init(def)
Entity.init(self, def)
-- yet more variables
end
function Snakey:update(dt)
Entity.update(self, dt)
-- entity behavior (works fine, so omitted)
end
function Snakey:render()
Entity.render(self)
end
Thank you very much for your help. I'm feeling quite frustrated because this sequence has worked in the past and I would really like to know why it's not calling those Entity methods.
Adding the class library
--Copyright (c) 2010-2013 Matthias Richter
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
It turns out that sequence matters. In trying to create the minimum reproducible code I could not reproduce the error. After some searching (and a little frustration), I noticed that in Dependencies.lua I was requiring the enemies BEFORE Entity.lua, but Player.lua was required after. I would have thought this wouldn't matter, since everything was imported into the program on frame 1 and I was creating entities on something like frame 1000, but alas. Anyway, problem solved! Always require parent classes before child classes... Lesson learned. :)
There some problems with my OOP.
I have parent with clear table and child with same table.
When i'm trying to add object to table of child, object adds to parent's table.
Simple example:
Account = {}
Account.__index = Account
Account.kit = {}
function Account.create(balance)
local acnt = {} -- our new object
setmetatable(acnt,Account) -- make Account handle lookup
acnt.balance = balance -- initialize our object
return acnt
end
function Account:withdraw(amount)
self.balance = self.balance - amount
end
-- create and use an Account
acc = Account.create(1000)
acc:withdraw(100)
table.insert(acc.kit, "1")
print(#Account.kit)
print(#acc.kit)
Result is 1 and 1.
But must be 0 and 1.
How i can isolate child table from parent?
In Lua using acc.kit where acc is a table with the metatable Account will first search the key kit from the table acc and then from table Account.
In your code acc does not have anything with key kit, so the Account.kit will be accessed.
You can solve this simply by defining the kit table for the acc on creation
Account = {}
Account.__index = Account
Account.kit = {} -- You can now remove this if you do not use it - I preserved it to make the test prints to still work.
function Account.create(balance)
local acnt = {} -- our new object
setmetatable(acnt,Account) -- make Account handle lookup
acnt.balance = balance -- initialize our object
acnt.kit = {} -- define kit to be a subtable
return acnt
end
Example:
https://repl.it/B6P1
I'd recommend using closures to implement OOP:
local Animal = {}
function Animal.new(name)
local self = {}
self.name = name
function self.PrintName()
print("My name is " .. self.name)
end
return self
end
--now a class dog that will inherit from animal
local Dog = {}
function Dog.new(name)
-- to inherit from Animal, we create an instance of it
local self = Animal.new(name)
function self.Bark()
print(self.name .. " barked!")
end
return self
end
local fred = Dog.new("Fred")
fred.Bark()
fred.PrintName()
output:
Fred barked!
My name is Fred
I don't know how make classes in lua so I used code which was recommended on forum.
But always only one object works. First one have coordinates x,y and the other object share his coordinates. Can you explain what I´m doing wrong in this code.
Thank you for your advice.
My code:
require("class")
asteroid = class:new()
function asteroid:init(x,y)
asteroid.b = love.physics.newBody(world, x ,y , "dynamic")
asteroid.s = love.physics.newCircleShape(35)
asteroid.f = love.physics.newFixture(asteroid.b, asteroid.s)
end
function love.load()
world = love.physics.newWorld(0, 50, true)
asteroid1= asteroid:new(100,100)
asteroid2= asteroid:new(700,100)
end
function love.update(dt)
world:update(dt)
end
function love.draw()
love.graphics.circle("line", asteroid1.b:getX(),asteroid1.b:getY(), asteroid1.s:getRadius(), 35)
love.graphics.circle("line", asteroid2.b:getX(),asteroid2.b:getY(), asteroid2.s:getRadius(), 35)
end
Recommended code:
__HAS_SECS_COMPATIBLE_CLASSES__ = true
local class_mt = {}
function class_mt:__index(key)
return self.__baseclass[key]
end
class = setmetatable({ __baseclass = {} }, class_mt)
function class:new(...)
local c = {}
c.__baseclass = self
setmetatable(c, getmetatable(self))
if c.init then
c:init(...)
end
return c
end
Here is a demo code for you
local MyClass = {}
MyClass.__index = MyClass
setmetatable(MyClass, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function MyClass.new(init)
local self = setmetatable({}, MyClass)
self.value = init
return self
end
-- the : syntax here causes a "self" arg to be implicitly added before any other args
function MyClass:set_value(newval)
self.value = newval
end
function MyClass:get_value()
return self.value
end
local instance = MyClass(5)
-- do stuff with instance...
I would suggest you to follow these tutorials
http://lua-users.org/wiki/ObjectOrientationTutorial
http://lua-users.org/wiki/TutorialDirectory
The : syntax causes an implicit self to be available as a local, referring to the object instance. But you are assigning to b at class level. Use self.b = instead of asteroid.b = so that the assignment is specific to the instance.
I have two classes in Lua. One inherits another.
test1 = {test1Data = 123, id= {0,3}}
function test1:hello()
print 'HELLO!'
end
function test1:new (inp)
inp = inp or {}
setmetatable(inp, self)
self.__index = self
return inp
end
test2 = {}
function test2:bye ()
print ('BYE!', self.id)
end
function test2:new_inst_test (baseClass, inp)
inp = inp or {}
setmetatable(inp, self)
self.__index = self
if baseClass then
setmetatable( inp, { __index = baseClass } )
end
return inp
end
a = test1:new({passData='abc1'})
b = test1:new({passData='ghyrty'})
c = test2:new_inst_test(a,{temp = '123343321135'})
d = test2:new_inst_test(b, {temp = '1'})
print (c.temp, c.test1Data, c.passData)
print (d.temp, d.test1Data, d.passData)
c:bye()
c:hello()
I want test2 not just inherit test1, but save own methods ('bye').
Is it possible?
Thanks!
you should set a metatable with __index=baseclass on the class metatable I think. But that will change the metatable for all objects in the test2 class. Doing it this way, you will use the methods from the class itself, and only use methods from the parent when the method does not exists in the current class, or it's metatable.
So it should be like
if baseClass then
setmetatable( self, { __index = baseClass } )
end
On the other hand it's kind of weird that you only specify the baseclass when making a new instance, instead of specifying it when creating the new class.
So I'd rethink how you inherit between classes instead of between instances and classes.
As a small wizardry themed example:
--oop.lua Example of OOP and inheritance in Lua
Person={
age=0,
className='Person'
}
-- needed if needed add comparisons, operations, ...
mtPerson={}
mtPerson.__index={
getClassName=function(self)
return self.className
end,
new=function(self,t)
return setmetatable(t or {},{__index=self})
end,
inherit=function (self,t,methods)
-- This is the heart of the inheritance: It says:
-- Look it up in the methods table, and if it's not there, look it up in the parrent class (her called self)
-- You pass this function the parent class (with :), a table of attributes and a table of methods.
local mtnew={__index=setmetatable(methods,{__index=self})}
return setmetatable(t or {},mtnew)
end,
introduce=function(self)
print(("Hi! I'm %s, I'm a %s and I'm %d years old"):format(self.instanceName,self.className,self.age))
end
}
setmetatable(Person,mtPerson)
-- Wizard inherits from the class Person, and adds some default values and methods
Wizard=Person:inherit({
className="Wizard",
knownSpells={},
},
{
listSpells=function(self)
print("known spells:",self)
if #self.knownSpells==0 then
print'none'
else
for k,v in ipairs(self.knownSpells) do
print(k,v)
end
end
end
}
)
i1=Person:new{
inventory={'wallet'},
instanceName="John",
}
i2=Wizard:new{ -- inherited method "new"
inventory={'wallet','wand','cloak of invisibility'},
instanceName="Harry",
age=20,
knownSpells={'Avada kavedra', 'Sesame open'}
}
i1:introduce() -- inherited method "introduce" notice that qge is the default value of 0
i2:introduce() --
i2:listSpells() -- method only in class 2
i1.age=26
i1:introduce() -- changed age of instance
print(Person.age) -- didn't change class defaults
print(Wizard.age)
i1:listSpells() -- Error.
While writing this, I came tho the conclusion that OOP in Lua is at the same time very simple, and very complicated. You simply have to really think things through before writing code, and stick with the plan afterwards. As such, here I chose to put attributes in the class and instance tables themselves, and put all methods in their respective metatables. I did this because now it's easy to iterate through all attributes, without encountering methods, but any choice that works is valid. You just have to pick one.