Page 1 of 1

Delete an object

Posted: Sat Nov 04, 2017 10:54 am
by Davïd
Hello everyone.

Quite noob question but here is something I couldn't get.

I have an object which look like this :

Code: Select all

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 :

Code: Select all

player = Player.new()
gameObjectList[#gameObjectList + 1] = player
And then try to delete it :

Code: Select all

    player.delete()    
    player = nil
    
    print(Inspect(gameObjectList)) -- Nothing happen
Why player = nil do nothing ?
And Is there a way for an object/table to set itself as nil ?

Re: Delete an object

Posted: Sat Nov 04, 2017 11:26 am
by vrld
From the PIL, chapter 17:
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.

Re: Delete an object

Posted: Sat Nov 04, 2017 11:31 am
by grump
Davïd wrote: Sat Nov 04, 2017 10:54 am

Code: Select all

    function self.delete()
        
        self = nil
        
    end
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.

You want this:

Code: Select all

table.remove(gameObjectList, <index of player>)
You need to store the player index somewhere when you add it to gameObjectList, or search for it when you want to remove it.

You should also reconsider the name of your variables. self has very specific meaning in Lua.

Re: Delete an object

Posted: Sat Nov 04, 2017 12:53 pm
by Davïd
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 ?