I’ve read through what feels like every other question topic about this issue - and still can’t see what I’ve done wrong. When trying to load my level2 collection, this error pops up.
Here is my code which posts to the main controller to load it:
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
Lsettings.coin_count = Lsettings.coin_count + 1
sound.play("#coin_pickup")
go.delete()
end
if Lsettings.coin_count == 4 and Lsettings.current_level == 1 then
msg.post("main:/controller", "load level 2")
end
end
I have checked every other collection, and none of them have the property name level2 aside the actual level 2. Here is a screenshot of the search bar for ‘level2’
I did a little bit of testing and resolved it through changing my code to post to the main controller, and it works now.
Fixed code:
local cooldown = 0
function update(self, dt)
-- Update the cooldown timer
if cooldown > 0 then
cooldown = cooldown - dt -- Reduce the cooldown timer by the time elapsed since the last frame
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
Lsettings.coin_count = Lsettings.coin_count + 1
sound.play("#coin_pickup")
go.delete()
end
if Lsettings.coin_count == 4 and Lsettings.current_level == 1 and cooldown <= 0 then
msg.post("main:/controller", "load level 2")
cooldown = 100000
end
end
I assume my code was probably trying to repeatedly send the message to the controller.
Ah, yes, you are right. The conditional check for coin count and current level will evaluate to true each time you receive a message. Adding a cooldown like you did, or even better a boolean flag, will a void the problem.
But you could also move the conditional check inside the “collision_response” like this:
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
Lsettings.coin_count = Lsettings.coin_count + 1
sound.play("#coin_pickup")
go.delete()
if Lsettings.coin_count == 4 and Lsettings.current_level == 1 then
msg.post("main:/controller", "load level 2")
end
end
end