How to msg.post() property

I’m basically trying to post from one script to this controller script in a collection in a different folder, I’ve tried consulting the documentation with no avail.

go.property("speed", 20)


local enemy_spawn_heights = {115, 370, 625} 

function init(self)
--here is where the problem is
    msg.post("main:/resources/environment/platforms/controller#script", "set_speed", { speed = self.speed })
end

function update(self, dt) 

    -- Maybe spawn a platform at random height
	if math.random() < 0.2 then
		local h = enemy_spawn_heights[math.random(#enemy_spawn_heights)]

		factory.create("#enemy_factory", vmath.vector3(1700, h, 0), nil, {}, 1)
    end
end

file tree: https://puu.sh/uJQdB/cc1947f37a.png
collection tree: https://puu.sh/uJQeP/4913e3028d.png

The filesystem folder structure is not used at all with runtime message passing address paths. You need to load the collection either directly or by a proxy and then you can send messages to it.

msg.post(“/first/second/third/third_go”, “hello”)

1 Like

So what exactly would I put in for mine?

1 Like

If you need the url for a particular script, put the following line somewhere in the script (probably in the ‘init’ function) then run the code and check the output in your console window.

print(msg.url())

2 Likes

Assuming you load the collection into your main collection directly this should work.

msg.post("/platforms/controller#script", "set_speed", { speed = self.speed })

You do not need the main: socket in this case. The / makes the path absolute, not using it would be relative pathing.

Add a new GO to your main.collection and add the platforms.collection to it then test the above message.

1 Like

No avail :(, what I did was create a GO in the main.collection and then added a collectionproxy to the original platforms.collection. Is that incorrect?

Also is there no way to directly post to the original platforms.collection?

The id a game object gets depends on its position in the build time collection hierarchy.

Collection proxies are used to dynamically load a new world (collection) into the runtime. It does not sound like that is what you intend to do, right? What you probably want is to just add the collection to main in the editor.

I encourage you to read the message passing manual that describes this in detail. See http://www.defold.com/manuals/message-passing/

2 Likes

Ok, that makes a lot more sense, I read through that article already but I think maybe I need some sleep to absorb it in, I’ll report in tomorrow! Thanks everyone!

4 Likes