[SOLVED] Collection factory objects aren't being deleted

I’m writing a chunk loader which uses collection factories to spawn level chunks. These chunks consist of a tilemap, as well as start and end “marker” objects to position the chunks correctly. I keep a table that holds references to these newly created collections so that I can delete them later.

My chunk loader code is in a pastebin here. There are comments.

The loader starts by spawning two chunks, the first one and then the second one in order, and then placed in self.chunks. When the first chunk passes camera_x, what should happen is that the first chunk is deleted and a new chunk is spawned. Instead, the first chunk stays where it is and the second chunk is deleted, which causes an error when trying to spawn and reposition a new chunk.

I can’t seem to figure out why this is happening. Any ideas?

PS: If you need me to share the project I’ll need to DM it, since I’m using paid assets that can’t be shared online.

I am not sure I understand correctly but I may try to guess something from your code.

collectionproxy.create doesn’t return a game object but a table of game objects. My suggestion is to add a root GO in the collection and insert in the list this root GO

local root = collectionfactory.create("/chunks#chunk1", vmath.vector3(x, y, 0))[hash("/root")]

then later you delete the whole collecton by deleting this GO with

go.delete(root, true)

I hope this helps but maybe I have missed something…

Ciao!

I’m aware that it’s a table, which is why I’m destroying it like this (probably should be doing it differently):

-- delete the oldest chunk
for k, v in pairs(oldest_chunk) do
    go.delete(v)
end
table.remove(self.chunks, 1)

Sorry for the poor suggestion…

Then I will try to print the table returned by collectionfactory.create and the one deleted when you delete the oldest chunk. They should match.

Solved it. My chunks were being very sneaky.

It looks like the second chunk was being destroyed and the oldest chunk stayed where it is. What actually happened is that the oldest chunk was being destroyed, and instead of repositioning the newly spawned chunk, I was repositioning the second chunk.

The second chunk was moved somewhere offscreen, so it looked like it was deleted. The newly spawned chunk was left at (0, 0), which was the exact same position as the first chunk that was deleted, which made it look like it wasn’t deleted.

I was originally using the exact same tilemap for each chunk, so there was no visual difference. Changing the tilemap for each chunk revealed the answer.

1 Like