Message Passing betweens scripts

I am struggling to find a way to send a “message” with the msg.post() function. I want to send a score to another script using the piece of code "msg.post(“default:/gui#gameOver”, “score”). However, I’m not sure of the way to send the actual numerical score with this piece of code.
image
So far this is all that I have on the recieving end but I am not sure how to collect the score after its been sent either.

I would be happy to answer any questions and hope that someone can help me.

msg.post(“default:/gui#gameOver”, “score”,  {my_score = 100500})
function on_message(self, message_id, message)
  if message_id == hash(" score") then
    print(message.my_score)
  end
end

This exact example is from manual I asked you to read =) Message passing in Defold

3 Likes

Thanks man. I actually have that exact tab open right now, I’m just new so sometimes I miss the basics. I tried something like that but couldn’t get it to work, so I just asked on here.

1 Like

Is there any sort of thought process that I should have when i get “nil” returned?

{score = 10}
In this line of code that I used, i print(“message.score”) and it gets returned as nil. Sorry to start this thread back up again.

Please, copy/paste code and show me what you mean

	elseif message_id == hash("game_end") then
		msg.post("main:/terminal", "show gameOver", {score = "10"})
	end
			
elseif message_id == hash("show gameOver") then
		print("this is not ", message.new_score)
		msg.post(self.live_proxy, "unload")
		msg.post("#gameOver_proxy", "load")
		self.live_proxy = "#gameOver_proxy"

In your message you send score but read new_score, so it should be

print("this is not ", message.score)

3 Likes

in another area of my code, I have an error where it tells me that I have a “-1 in function post”. I’m not sure where to start debugging since i think there is something in the function post. Do you think that you could help me?

Many thanks.

We need to see the full error message for a start, along with the relevant code.

This is the error that I get when I run the program.

ERROR:SCRIPT: main/main.script:50: Could not send message 'score' from 'main:/terminal#main' to 'score:/script#score'.
stack traceback:
  [C]:-1: in function post
  main/main.script:50: in function <main/main.script:15>

This is hopefully the relevant code:

	elseif message_id == hash("show score") then
		msg.post(self.live_proxy, "unload")
		msg.post("#score_proxy", "load")
		self.live_proxy = "#score_proxy"
		msg.post("score:/script#score" ,"score", {score = message.score})	
	end

There is quite a lot score words which is probably confusing but I know that the URL is correct and as for the rest… I can’t seem to figure it out despite reading up on it.

If there is anymore you think i need to add i would be happy to

It looks like you are loading a collection proxy and instantly trying to message an object within it. This will not work, you must ensure the collection proxy is finished loading first.

Once you receive the “proxy_loaded” message, you can safely enable the collection proxy. It will be available the next frame after that.

3 Likes

Okay thanks :+1:

2 Likes