Posting a message to game object script from factory

Hi,

Been stuck with this for a while, and I’m fully aware that’s it’s just my addressing however I’ve completely stumped myself.

My goal: I want to send a message to the script of a game object being created by a factory. The aim of this is to set bounds on my map for different objects using a hidden layer on a my level tilemap.

This script (level.script) finds the position of specific trigger tiles (46 and 47) and creates instances of enemy objects at the coordinates. What I want to do is for another set of unique tiles, have a message sent to stop instances moving any further.

function init(self)
sx, sy, w, h = tilemap.get_bounds("#tilemap")
print(sx, sy, w, h)

for y = sy, h+sy-1 do
	for x = sx, w+sx-1 do
		local t = tilemap.get_tile("#tilemap", "enemy", x, y)
		if t == 47 then
			local id = factory.create("#mushroom_factory", module.tile_vector(vmath.vector3(x, y, 0)))				
		elseif t == 46 then
			factory.create("#skeleton_factory", module.tile_vector(vmath.vector3(x, y, 0)))
		end

		local p = tilemap.get_tile("#tilemap", "patrol", x, y)
		if p == 5 then
			msg.post(msg.url(nil, id, "mushroom_factory"), "patrol_limit", { limit = module.tile_vector(vmath.vector3(x, y, 0)) } )
		end
	end
end

end

My issue is with the line:

msg.post(msg.url(nil, id, "mushroom_factory"), "patrol_limit", { limit = module.tile_vector(vmath.vector3(x, y, 0)) } )

As this only seems to let me send a message to components within my /level object.
My collection structure is as follows:
image

The script I am wanting to send my message to is mushroom.script in the prototype object mushroom:
image

I apologise for what’s probably just me misreading something

Thanks

I think this should be:

msg.url(nil, id, "mushroom")

Hi, thanks for the response.
I’ve tried this and get the same error I was getting with a lot of other formats I tried:

ERROR:GAMEOBJECT: Component ‘/level#mushroom’ could not be found when dispatching message ‘patrol_limit’ sent from level1:/level#level

There is also chance that id is nil looking at the code, url should have 3 parts.

You’re probably right. ID is returning ‘/instance1’ etc but when trying to pass into a function it’s apparently nil. I’ll need to look at the factory documentation again properly I guess haha.

The functionality you require is documented here:

If you are unable to get it to work I suggest that you start with an empty project and create a simple example that you can experiment with.

I actually think your only issue is that you’re declaring id in the wrong scope, hence why it’s always nil when trying to construct the url with it. Try something like this:

-- declare at the scope of your loop, not the conditional
local id
-- where you spawn a mushroom
if t == 47 then
   id = factory.create(etc...)
end
-- where you're passing the message
if id and p == 5 then
    msg.post(msg.url(nil, id, "mushroom"), etc...)
end

Because you’re declaring the local variable “id” in the scope of the “if t == 47” condition, that variable would only be known within that or nested scopes. Then you’re trying to reference it in the parallel scope of “if p == 5”, where it has no meaning.

To get around this you just need to declare the variable in the lowest common scope in which you’ll need access to it, even if you’re only assigning it in a deeper scope. Then when you’re using the variable for something, check that it’s actually been assigned a value first. Hope this helps!