Passings messages to factory-made objects (SOLVED)

I’ve got a factory that spawns enemy ships and the ships contain a factory to spawn bombs.
I need to pass a message from the bomb object back to the parent enemy ship object.
Is there a way to get the url of the parent ship from within the bomb script?

Something like this maybe:

function spawn_bomb(self)
local bomb = factory.create("#bomb")
msg.post(bomb, “set_ship”)
end

And then listen for that message in on_message and save the sender to a variable.

You could also have a property on the bomb:

go.property(“parent”, msg.url())

And set that property when you call factory.create

3 Likes

Great, thanks Johan - that seems to work! :grin:

I would go for this solution as well. Nice and clean.

1 Like

This should definitely be in the list of pro tips or in the documentation. It’s a very useful feature when you spawn objects to have a two way communication.

4 Likes

Good idea! I will add a section about this in the factory manual.

3 Likes

@sicher could you kindly add the solution with go.property to the Factory documentation since I don’t fully grasp it in the example in this thread and probably there are others like me.

1 Like

You can read an example here: Factory component manual

1 Like