Hello,
i am having some trouble passing messages that contains a table between 2 scripts in my game. When the player object dies, it passes the message “dead” to the score script, which then should pass the score to an endscreen script, however i am receiving this error:
Could not send message ‘scorePass’ from ‘loader:/go#loader’ to ‘endscreen:/go#endscreen’.
stack traceback:
[C] in function post
Initially i thought this was because the score script was no longer loaded as collections had been switched, but i tried not unloading the collection which did not help.
Any help with this would be greatly appreciated
Skipping the parentheses on a function call with a single argument is actually ok!
@Tang please check the url. Add a print(msg.url()) to the init function of the receiving script and confirm that it is indeed “endscreen:/go#endscreen”
just tried this - printed a message url in both the score script and the endgame script init functions, score is loaded directly after the menus and the endgame opens directly after the main collection(which contains the score script), which i think means that the message cant be sent because they aren’t open at the same time?
I’m unable to download the project, I get a message saying “Shared files no longer available”.
Try zipping your project again, this time excluding the .internal folder and the build folder. That should make the project small enough to share. You can also try emailing me at bjorn@defold.se.
Thanks. I’ve received the project. The problem was indeed that the endscreen wasn’t loaded when you tried to send the message. This is what your code looked like:
local function load_endscreen(self)
msg.post("go#endscreen", "load")
msg.post("go#endscreen", "enable")
end
function on_message(self, message_id, message, sender)
if message_id == hash("scorePass") then
load_endscreen(self)
msg.post("endscreen:/go#endscreen", hash("scorePass"), scoreTable)
unload_endscreen(self)
end
end
The call to msg.post("go#endscreen", "load") will send a message to load the endscreen BUT since msg.post() is an asynchronous operation it means that it won’t complete immediately. Your code will continue to execute and you will send an “enable” message (also async) and then you will immediately proceed to also send a “scorePass” message.
The next things that happens is that the engine will process the messages in order:
“load”
“enable”
“scorePass”
The problem here is that you have not given the loaded collection and the game objects and components inside time to initialize. Your “enable” message will most likely be ignored and your “scorePass” message will not be able to reach “endscreen:/go#endscreen” since the components haven’t been initialized.
What you need to do is to wait until you receive a “proxy_loaded” message before you proceed to send the “scorePass” message.
You read more about loading collection proxies in the manual: