Message URL Errors

Hi, I’m new to Defold and I’m getting errors whenever I attempt to send messages to a receiver.

I wrote this code to send the position of an object to a script located inside a game object named opholder located in a collection named holder.

function update(self, dt)
	 local opholder = msg.url("opholder#script")
	 msg.post(opholder, "position", {position = go.get_position()})
end

however, whenever I run the code, I get the following error message.

ERROR:GAMEOBJECT: Instance '/opholder' could not be found when dispatching message 'position' sent from default:/instance2#script

I tried different variations, but I keep getting the same type of error everytime.

function update(self, dt)
	 local opholder = msg.url("holder/opholder#script")
	 msg.post(opholder, "position", {position = go.get_position()})
end

ERROR:GAMEOBJECT: Instance '/holder/opholder' could not be found when dispatching message 'position' sent from default:/instance4#script
function update(self, dt)
	 local opholder = msg.url("holder:/opholder#script")
	 msg.post(opholder, "position", {position = go.get_position()})
end

ERROR:SCRIPT: card/operation.script:45: The socket 'holder' could not be found.
stack traceback:
	[C]: in function 'url'
	card/operation.script:45: in function <card/operation.script:44>

Can someone help me understand why these errors are occurring and how I can fix this issue? I looked at the defold API and it stated, “The format of the string must be “[socket:][path][#fragment]”, which is similar to a http URL. When addressing instances, socket is the name of the collection. path is the id of the instance, which can either be relative the instance of the calling script or global. fragment would be the id of the desired component.”, but I don’t know what I’m doing wrong.

Thanks

From the error message:

ERROR:SCRIPT: card/operation.script:45: The socket ‘holder’ could not be found.

It seems like you didn’t set the collection name to holder. To change a collection’s name, select the root “Collection” node in the Outline window and set the name to “holder” in the properties there. Then, your third code option should work I think:

local opholder = msg.url(“holder:/opholder#script”)
msg.post(opholder, “position”, {position = go.get_position()})

1 Like

It seems like you’re sending messages from a spawned go with a script. Is that right?

I encourage you to read the Message passing manual. It describes how addressing works: http://www.defold.com/manuals/message-passing/

1 Like

The “socket” part of the url is not the collection part. It addresses the world, which comprises the main collection loaded at startup, or any collection loaded through a collection proxy. See the Message Passing manual on how these things work: http://www.defold.com/manuals/message-passing/

3 Likes

Thanks for the help :slight_smile:

It’s always helpful to print(msg.url()) from the init() function to understand which URL each game object has.

3 Likes