Parent of go created by factory

#1

Hello, obviously I have some misunderstanding how factory works, can someone explain please?

so, here is my project structure

main.collection
—game.collection
------board.go
---------BoardController.script
---------BaseTile.factory

inside of BoardController script

print(msg.url());--DEBUG:SCRIPT: url: [main:/game/board#BoardController]
print(board_id); -- DEBUG:SCRIPT: hash: [/game/board]
print(tile_type);--  DEBUG:SCRIPT: BaseTile
local factory_url = msg.url(nil, _id,  tile_type);
print(factory_url) ;-- DEBUG:SCRIPT: url: [main:/game/board#BaseTile]
local tile_id = factory.create(factory_url, scene_pos, nil, {tile_color = color});
pprint(msg.url(tile_id));--DEBUG:SCRIPT: url: [main:/instance0]
go.set_parent(tile_id, board_id);
pprint(msg.url(tile_id));--DEBUG:SCRIPT: url: [main:/instance0]

my question is, why created “instance0” is not child of “board.go” when it just created?
And why “go.set_parent(tile_id, board_id)” doesn’t do nothing here?

0 Likes

#2

It is by design. For some games it might make sense to have the spawned object a child of the factory component but for others it doesn’t make sense at all (like bullets being fired from a weapon).

This is actually an asynchronous operation. The game object will not become a child until the next frame. (and yes we do not explain this in the documentation, I will make sure this gets updated)

1 Like

#3

Оh "asynchronous ", now it is clear :slight_smile:

by the way it could be nice feature to tell factory where to spawn objects.

Thanks for fast (and furious) response!

cheers!

1 Like

#4

Did you try the position parameter? Or do you mean something else?

0 Likes

#5

No, by “where to spawn” I meant making them children of some specific gameObject.
e.g in my case I would like say to factory to spawn “tiles.go” in to “board.go” by default.
But this is only suggestion.

0 Likes

#6

I agree, a parent option in factory.create() and for collectionfactory would be very useful.

2 Likes