Factory Spawned Objects in to specific collection (SOLVED)

Hi

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

No. You can think about collections as a means to name game object and keep static naming when reusing game objects. In runtime they do not exist.

So, all spawned game objects will have names like “/instance0”, “/instance1” etc, no matter what name the spawning component has.

It sounds like this might pose problems for you. If so, in what way?

1 Like

Hi @sicher , thanks for clarifying.

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

Thanks!

1 Like

Aha, sorry. Yes, I misread your comment.

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:

local my_id = go.get_id()
factory.create("#factory", nil, nil, { target = my_id })

and in the spawned object:

go.property("target", msg.url())

...
4 Likes

Solved!

Thanks

2 Likes