Is this a timer.delay bug or am I doing this wrong?

To make this easier to see I put the timer in the function as opposed to calling it outside the function. But they both result in the same issue. If I have two objects combine before the timer delay ends then my call to spawnVeggie doesn’t work. However, if I set it to True (on repeat timer) it will keep on spawning). I need a delay on the spawning, but if I don’t delay the spawn this issue doesn’t occur. it appears somehow the timer is being stopped/deleted by having the object it last spawned be deleted.

-- function to create one of 4 random starting veggies
function spawnVeggie(position)
	if gameover == false then 
	-- Start the timer to let veggie fall so they don't collide with existing and new object
	timer.delay(spawn_wait, false, function()
			--local rot = vmath.quat(0, 0, 0, 50)
			local rot = nil 
			local random_index = math.random(1, #factories)
			local selected_factory = factories[random_index]
			-- Spawn an object from the selected factory
			if selected_factory == "greenpepper" then	
				local objectID = factory.create("main:/factories#startgreenpepperfactory", position, rot, nil, .2)
			elseif selected_factory == "chilipepper" then
				factory.create("main:/factories#startchilipepperfactory", position, rot, nil, .2)
			elseif selected_factory == "redonion" then
				factory.create("main:/factories#startredonionfactory", position, rot, nil, .13)
			elseif selected_factory == "radish" then
				factory.create("main:/factories#startradishfactory", position, rot, nil, .13)
			end
		end)
	end
end

A timer will not trigger if the game object it was started from gets deleted.

1 Like

“Timers created within a script will automatically die when the script is deleted.”

Of course after a night of sleep…this makes perfect sense. Sometimes when your head is buried into making the game, you can’t see the forest because of all the trees.

Thank you.

And just to let people of the future know how I worked around this.

I no longer delay the spawning of objects. I spawn them behind the background by assigning it -1 on the z axis. Then I do a timer delay and move it to 0.2 on the z-axis. Works perfectly.

One last update. I found a better way to handle this. I created a persistent object in the collection and I use that to control when objects spawn.

2 Likes