Do factories spawn Game Objects in to the the same collection that the factory is in?
I am experiencing a situation where game objects spawned by factories are receiving collision messages where the other_id is a hash of the absolute URL of the other collision object even though that other collision object should be in the same collection. In every other non-factory-spawned Game Object, the other_id is a hash of the relative path
This poses a problem for me as my game is organised into different level collections (‘level_1’, ‘level_2’ . . .). The game objects communicate with each other inside those collections using relative paths.
Since I can’t get the parent collection of a game object from within a game object (so I can pass it as a parameter to a factory.create), I’m unsure how to send messages to game objects in a specific level collection from objects spawned from a factory.
Are there any best practices for dealing with this situation?
Yes, factory.create() returns the id of the spawned game object. Store that and use it to communicate:
local my_object_id = factory.create("#factory", nil, nil, {my_value = 1})
-- communicate with the object
msg.post(my_object_id, "hello")
-- store it for future reference
table.insert(self.spawned_objects, my_object_id)
My problem was that I could not address game objects in a level collection from a factory spawned object.
But as I type this, I realise I can probably just send a message to the spawned object and store the sender on the spawned object and then use that to send messages back
But you’re right, you can absolutely send a message to the spawned object and have it store the sender. You can even pass that as a parameter when spawning: