Delete an object

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Davïd
Prole
Posts: 12
Joined: Sun Jan 29, 2017 4:12 pm

Delete an object

Post 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 ?
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Delete an object

Post 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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Delete an object

Post 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.
Davïd
Prole
Posts: 12
Joined: Sun Jan 29, 2017 4:12 pm

Re: Delete an object

Post 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 ?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest