Deleting GOs

Hi,

I’ve implemented composite objects as collections of hierarchical GOs created by collection factories. Once such parnt object is hit, I decompose it, and re-parent all child objects to another GO in the main collection.
Should Ianually release child GOs when I remove ttheir new parent?

Thanks

What do you mean by release? If you delete a game object you have the option to either:

  1. Delete the game object and all children using go.delete(id, true)
  2. Delete the game object and leave children alone using go.delete(id). The child objects will then have no parent.

Here’s parts of my code in the main collection:

    function init(self)
        --[[ set up the plumbing ]]--
        self.board_id = hash("/board")
        msg.post(".", "acquire_input_focus")
    end

board is a GO defined in the main collection, so I assume there’s no need to create or delete it manually

Then I have this part:

local function handle_collision(self, obj_id)
    local children = get_children_go(self, obj_id)	
    for _, child_id in ipairs(children) do
        go.set_parent(child_id, self.board_id)
    end
    go.delete(obj_id)
end

called for a collision, so that all child objects are parented under board.
Should I delete all the child objects I set board as a parent in final() or they will be deleted as its children automatically ?

You do not have to manually delete game objects when the game shuts down or when a collection is unloaded. This is taken care of by the engine.

I guess it is the case of board being defined in the collection, right?

But if it was an object created by a tactory, would

go. delete(self. board_id, true)

delete objects I appended to it in handke_collision ?

Yes, go.delete(id, true) will delete any child game objects at the time of deletion.

1 Like

Thank you! Appreciate your time and effort