ERROR:GAMEOBJECT: Instance '<unknown>' could not be found when dispatching message 'add_score' sent from main:/instance0#coin (SOLVED)

Hey guys! Once again I’m stuck and feel stupid trying to modify the runner tutorial sorry for bordering (once again) :sweat_smile:

I’m trying to get a simple score counting for the coins to work but am getting the ERROR message:

ERROR:GAMEOBJECT: Instance ‘<unknown>’ could not be found when dispatching message ‘add_score’ sent from main:/instance0#coin

The scripts working in this case are the coin.script and the gui.gui_script.

My coin.script currently looks like this:

local score = 1000	--punkte pro gesammelter Einheit
local speed = -240	--Geschwindigkeit des Sterns

function update(self, dt) -- funktion bestimmt x.pos der Instanz, 
	local p = go.get_position()
	p.x = p.x + speed * dt
	if p.x < -32 then		--wenn sie kleiner als -32 ist (vermutlich breite des Obj.)
		go.delete()			--wird Instanz gelöscht
	end
	go.set_position(p)		-- ansonsten wird pos.x gesetzt
end

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then 		-- wenn es eine collision gibt
		msg.post("blubb#main", "add_score", {amount = score}) -- wird an blubb.go#gui script gesendet, es soll function add_score ausführen (amount ist der oben definierte score)
		go.delete()											-- coin/instanz wird gelöscht
	end
end

My gui.gui_script like this

function init(self)
	self.score = 0
	self.score_node = gui.get_node("score")
end

--function add_score(self, score, message)

function on_message(self, message_id, message, sender)

	if message_id == hash("add_score") then

		self.score = self.score + message.amount
		gui.set_text(self.score_node, tostring(self.score))
	end
end

The main.gui is attached to the game object blubb.go and only has the gui.gui_script attached to it, a text node for the score (with the Id score) and a font.

Since the coins disappear once they collide with the hero I figure the collision response works fine. As for the instance I have no idea what it could be that’s missing. :expressionless:
Thanks a lot in advance! :blush:

From the error message it seems like the line

msg.post("blubb#main", "add_score", {amount = score})

is the offender. More specifically the url “blubb#main”.

From the looks of it you are trying to send the message to component “main” on the gameobject “blubb”, is that correct? Can you post a picture of you collection outline?

It should be… currently looks likte this

Your gui component has id “gui” and the gui resource is named “main.gui”. Looks like the correct URL should be “blubb#gui”. A component’s URL should be visible in the editor properties outline when selected.

50

1 Like

changing the code of the msg.post to

msg.post("blubb#gui", "add_score", {amount = score})
msg.post("#gui", "add_score", {amount = score})

both didn’t work… the ERROR message stays the same too

In my case the url for the gui looks like this :face_with_raised_eyebrow:

Is the collection in the outline screenshot your main collection?

The url you see in the editor is always relative to the currently open file.

! It actually wasn’t but… in the main Collection it looks just the same … now I’m confused

Since “blubb” is located in “level” it gets a runtime id of “/level/blubb”. This is the absolute path to the game object. Since spawned coins always end up in the root of the game world, you can address “blubb” as either absolute ("/level/blubb") or relative to the location (the world root) which is “level/blubb”.

1 Like

There is an issue on the url help in the editor. https://github.com/defold/editor2-issues/issues/747

Okay so if I get it now the msg.post should look like this:

msg.post("level:blubb#gui", "add_score", {amount = score})

It’s still not working but the ERROR message changed to

ERROR:SCRIPT: /level/coin/coin.script:15: Could not send message 'add_score' from 'main:/instance0#coin' to 'level:blubb#gui'.
stack traceback:
	[C]: in function 'post'
	/level/coin/coin.script:15: in function </level/coin/coin.script:13>

I can’t tell ift that’s better or worse :sweat_smile:

No.

msg.post("level:blubb#gui", "add_score", {amount = score})

Now you are trying to address the world “level”. This implies that you have loaded it by a collection proxy which you have not.

You should write the path to the game object as “level/blubb”. There should be no colon. So:

msg.post("level/blubb#gui", "add_score", {amount = score})
1 Like

The url format is like this:

"world:/path/to/gameobject#component"

Usually you want to address relative to where you are but you can be as specific as you want:

"#another_component" - address a component within the same game object.

"gameobject#component" - address a component in a game object in the same collection as the one doing the addressing (both are in main.collection, for instance).

"path/to/gameobject#component" - address a component in a game object in a subcollection relative to the one doing the addressing.

"/path/to/gameobject#component" - address a component in a game object in a subcollection starting from the root of the currently loaded game world (usually main.collection that is autoloaded at game start).

"world2:/path/to/gameobject#component" - address a component in a game object in a subcollection starting from the root of another loaded game world (loaded via proxy).

3 Likes

:sweat: Okay that makes sense… the / is used wenn following a path an the # to the final object. An I don’t Need the : unless I’m having another game world from wich I want to load something
I think I’m getting it now :frog:
And holly maccaroni it’s working :open_mouth: Thank you! :heart_eyes:

3 Likes

Yeah, it’s sort of like directory paths on disk but you can only go down. There is no way to address stuff above the current collection (like you can with “..” in directory paths). In that case you have to start from the root.

https://www.defold.com/manuals/addressing/ has all the details.

3 Likes

Thanks! that helps a lot :grinning:

3 Likes