Sending messages to GO created by factories

It is a bit confusing to me how referencing to game objects created by factories works. I want to send a message to such an object (to change its state somehow). In order to do that I need to know its URL, and it seems like the only way to obtain this URL is to send the URL from GO of interest to its parent GO in message and store it in table there. Am I right or there is a simpler way?

factory.create returns url of created object. You can use it like this:

function spawn_ghost(self)
	local ghost = {
		id = factory.create(self.ghosts_factory),
		track = self.character_trace,
		events = self.events,
	}
	msg.post(ghost.id, 'set_track', ghost.track)
	self.ghosts[#self.ghosts + 1] = ghost
end
3 Likes

Thanks, I was confused because in documentation it says that factory.create() returns a "global id’, and I was not aware that URL is basically the same thing.

Well, an id is equal to the “path” segment of an url. As long as you’re sending messages within the same world (main collection) and don’t load collections through proxies you probably don’t have to worry too much about the differences.

There are some situations though when you might need to construct url:s and then you need to know how it all works. In that case check out http://www.defold.com/doc/message-passing#_addressing_and_urls

3 Likes