local Object = {}
--[[
Object definition
@return table : new Object
]]
function Object.new()
local self = require('script.object.gameobject').new()
--[[
Privates vars
--------------------------------]]
local update = 0
--[[
Publics vars
--------------------------------]]
self.publicVar = 'value'
--[[
Update
]]
function self.update()
--print('update')
end
--[[
Draw
]]
function self.draw()
--print('draw')
end
--[[
Delete
]]
function self.delete()
self = nil
end
return self
end
return Object
Then I create a player object and add it to a list like this :
Lua does automatic memory management. A program only creates objects (tables, functions, etc.); there is no function to delete objects. Lua automatically deletes objects that become garbage, using garbage collection.
You need to remove all references to the object, that means including the one in gameObjectList, and wait for the garbage collector (or run collectgarbage()). You could also make gameObjectList a weak table. In any case, make sure to read the PIL on that topic.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.
This just sets self, which is a variable local to the Object.new function, to nil. You didn't change anything about the reference you still have in gameObjectList.
Ok thank you. I'm used to languages where you can juste delete an object like PHP. I will look at weak table.
The idea behind gameObjectList is to call an update() method for each gameobject and I also plan to have other lists like a monster table for example. When a monster die I need to remove it from both table. But maybe I'm not doing it the right way?
grump wrote: ↑Sat Nov 04, 2017 11:31 am
You should also reconsider the name of your variables. self has very specific meaning in Lua.
Can you explain more cleary what I'm doing wrong? Is this a wrong way to do OOP ?