Factory creation vs movement efficiency (SOLVED)

Hi there,
I’m pretty new to Defold and started my first game. I’m creating about a 100 small objects and after some time, all of them need to be at a different place.
What would be more efficient:
1, creating them with a factory, delete all and create new ones at different location
2, creating them, moving them out off frame and move them back to the new position
(pls note that the play area needs to be cleared before the position)
So it’s basically create/delete/create vs create/move/move.
Thanks in advance!
Adam

You can simply disable them and then enable them and move them to the new position.

Creation / deletion is very fast and efficient in Defold. Otherwise moving would probably be better.

To give best answer need a better picture of what you are actually doing. For example, are the game piece animated as they move?

2 Likes

The game is about a maze and its inner walls change position at some point. I didn’t thought about animation yet but it’s a good idea.

If you are generating a new maze out of pieces go ahead and create/delete. It should be fine!

3 Likes

Recreating with factory is fine.

1 Like

Hi again,
I think I’m doing something wrong…
Even after deleting the all factory created objects, I got an error that the sprite buffer is full (left on default 128).
I’m checking if the new wall object is not on the players position and also avoid to create multiple ones on the same place.
First creation is fine, the error comes on the second run when creating the 28th object (reach buffer).
Thank you in advance!
Here is the code snippet:

function shiftMaze()
	local counter = NULL 
	removeAll()
	repeat
		local location = vmath.vector3()
		location.x = math.random(FIRST_INNER_COLUMN, LAST_INNER_COLUMN) * TILE_SIZE - HALF_TILE_SIZE
		location.y = math.random(FIRST_INNER_ROW, LAST_INNER_ROW) * TILE_SIZE - HALF_TILE_SIZE
		local wallId = factory.create("#maze", location)
		if location ~= go.get_position("/player") and innerWallSet[location] == nil then
			innerWallSet[location] = wallId
			counter = counter + ONE
		else
			go.delete(wallId)
		end 
	until(counter == DENSITY)
end

function removeAll()
	for k, v in pairs(innerWallSet) do
		go.delete(v)
		innerWallSet[k] = nil
	end
end

If you have a game object with a sprite component and you delete 100 instances of this object and in the same frame create another 100 instances then you need a sprite buffer that can hold 200 sprites. Any game objects that you delete during a frame will exist in the engine until the end of the frame when they are removed.

PS You are using a lot of global functions (and variables). This can cause serious problems with functions and variables overwriting each other. Use the local keyword to avoid this.

Thank you, now I understand. :smiley: Didn’t know that much about go life cycle…
Regarding variables they are local, they are declared at the beginning of the script, just didn’t copied in the whole to make it more clear. :slight_smile:

The application lifecycle is explained in one of our manuals and the post-update step where game objects are deleted is here:

Ok, got it. But the functions are global though.

Thank you for the link and the advice regarding local functions. :+1: