Why timer isn't working? (SOLVED)

Hi there!

What am I doing wrong?

function on_message(self, message_id, message, sender)
	-- if collide
	-- then play particle effects
	-- delete game object
	-- wait 1.5 seconds
	-- game over
	if message_id == hash("collision_response") then
		particlefx.play("#player_particle")
		go.delete()
		self.timeout = timer.delay(1.5, false, function()
			msg.post("controller:/controller", "game_over")
		end)
	end
end

Just guessing here, but since you delete the current game object, and it’s script, both the timer (which belongs to the script) and the callback are also deleted.

If you instead put the go.delete() is inside the callback function, it should work.

2 Likes

Yes! Thank you.

I’ve tried this solution and it works:

function final(self)
	msg.post(".", "release_input_focus")
	msg.post("controller:/controller", "game_over")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		particlefx.play("#player_particle")
		msg.post(".", "disable")
		self.timeout = timer.delay(1, false, function()
			go.delete()
		end)
	end
end

Is it fine to post msg.post("controller:/controller", "game_over")at the final function?

If I place it to:

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		particlefx.play("#player_particle")
		msg.post(".", "disable")
		self.timeout = timer.delay(1, false, function()
			go.delete()
			msg.post("controller:/controller", "game_over")
		end)
	end
end

It works, but I have errors in console:

ERROR:GAMESYS: The collection /main/game/game_over.collectionc could not be loaded since it was already. Message 'load' sent from controller:/controller#script to controller:/controller#game_over_proxy.
ERROR:GAMESYS: The collection /main/game/game.collectionc could not be unloaded since it was never loaded. Message 'unload' sent from controller:/controller#script to controller:/controller#game_proxy.
DEBUG:SCRIPT: proxy_loaded	url: [controller:/controller#game_over_proxy]
DEBUG:SCRIPT: proxy_unloaded	url: [controller:/controller#game_proxy]

Feels like it called twice.

1 Like

Share the controller script.

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("#main_menu_proxy", "load")
end

function final(self)
	msg.post(".", "release_input_focus")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("start_game") then
		msg.post("#game_proxy", "load")
		msg.post("#main_menu_proxy", "unload")
	elseif message_id == hash("game_over") then
		msg.post("#game_over_proxy", "load")
		msg.post("#game_proxy", "unload")
	elseif message_id == hash("restart_game") then
		msg.post("#game_proxy", "load")
		msg.post("#game_over_proxy", "unload")
	elseif message_id == hash("show_menu") then
		msg.post("#main_menu_proxy", "load")
		msg.post("#game_over_proxy", "unload")
	elseif message_id == hash("proxy_loaded") then
		print("proxy_loaded", sender)
		msg.post(sender, "enable")
	elseif message_id == hash("proxy_unloaded") then
		print("proxy_unloaded", sender)
	end
end

The first option should work fine.

I’m curious about the second case though, since by the loks of the console output, it seems the message gets sent twice?
What if you put a print next to the delete, do you get one or two printouts?

go.delete()
print("DELETED")

Yes, it is. I’ll try to debug “collision_response”.

1 Like

Does that mean “once” or “twice”?

Twice.

1 Like

I forgot that I have to use “trigger_response” instead “collision_response”. Now it’s all right.

function on_message(self, message_id, message, sender)
	if message_id == hash("trigger_response") and message.enter then
		particlefx.play("#player_particle")
		msg.post(".", "disable")
		self.timeout = timer.delay(1, false, function()
			go.delete()
			msg.post("controller:/controller", "game_over")
		end)
	end
end

Thank you @Mathias_Westerdahl!

4 Likes