Passing message to another object (SOLVED)

So I’m struggling to understand how I tell a script where to send the message. I get the idea is msg.post(“[socket:][path][#fragment]”, message) But I’m don’t think I’m understanding what in the editor corresponds to what.

For example


I’m trying to send a message to my button.script (The first thing under the buttons folder) but am not sure what the proper syntax for it is.

Also, is there a way to do message passing when things have the same name? Like, if my gui_script and my script are named the same thing, is there a way to let it know specifically to send it to the script and not to the gui_script?

The url used in msg.post is related to how your collections are organised, not your file structure. The socket corresponds to which “world” the target game object is in, the path is usually the id of the target game object and the fragment is the component of the game object (usually a script).

The documentation covers this in more detail here

You can use the following line somewhere in a target game object script to see what the url is

print(msg.url())

How you have structured things on disk has nothing to do with the URL that you use when you send a message. What matters is how you have arranged your collection(s) and named the game objects within those collections. Consider this collection:

In the above screenshot the absolute URL to the script on game object ‘b’ would be main:/b#script.

If the script attached to game object A wants to send a message to the script on game object B it could do like this:

msg.post("main:/b#script", "my_message")

But in the above example there really is no need to be that explicit. Since both game objects exist in the same collection it would be enough to use a relative URL like this:

msg.post("b#script", "my_message")

I could also send the message to B without specifying a component:

msg.post("b", "my_message") -- All components on b will get the message

If a script on a game object would like to send a message to another component on the same game object it’s enough to specify the component id like this:

msg.post("#sprite", "play_animation", { id = hash("foobar") })

It gets more advanced when you have multiple collections loaded via collection proxies or when collections are nested. You can read more about message passing in the manual.

5 Likes

Thank you SO much. That helped immensely. Got things working now.