Hello, I’m still new to Defold but have run into an apparent snag in the way I intended to generate a project using collection factories and factories.
I have created a Collection (game_screen) with a set of factories within, which I intended to call whole via a Collection Factory within another Collection(main_screen).
If I’m not missing my mark however the instances generated by the Factory are then created within the main Collection (main_screen) rather than the Collection the Factories are part of (game_screen).
I would have expected /instance5 to be within the original collection, where /frame is and hence the addressing still works.
I hope this all makes sense.
Anyway the question is:
a) am I wrong?
b) if I am wrong why is the addressing breaking?
c) if I’m correct, is there any way of resolving the addressing… or should I restructure my project?
It says in the error that the spawned object is at main_screen:/instance5, so you’re right about it being in the main collection. Maybe there isn’t actually a component at /frame#test?
I’m not sure if this description matches your earlier description of your collections.
If I understand you correctly (a picture of Outline of your game_screen collection would be useful):
When you use a Collection Factory to instantiate A collection (somewhere “in” B hierarchy), it’s actually that all the Game Objects from the A are created (instantiated) among the B Game Objects following the hierarchy as defined in A, but for the addressing purpose, they use a prefix “collection[N]” (because you can create N “instances” of A hierarchy in your program using Collection Factory).
The key to understand this is that collection is not a runtime thing. It exists only in Editor - it’s only a hierarchy of Game Objects. Game Objects do exist in runtime.
This addressing nuance is explained in Collection Factory manual:
A prefix /collection[N]/, where [N] is a counter, is added to the id to uniquely identify each instance:
As a result, when addressing you must take into account, that you are referencing new instances of Game Objects that are in runtime within your newly created “collection[N]” collection.
Note, that collectionfactory.create() returns a whole table of all instantiated objects with their URLs Use it or utilize powerful relative addressing:
Nevertheless, the error you have:
tells us there is rather a Factory being used (instance[N]), so please, do explain your Outlines of collections precisely and what you are doing in code to instantiate them
I would be also happy, if you could tell me if my video:
explains everything you need to know about addressing or is there something I can do better in it
I don’t think that I explained the issue as well as I should have done, see outlines below:
As a side note however I see that I get exactly the same issue when simply manually putting the intended generated Collection (game_screen) into the main Collection (main_screen)… so this presumably be an issue/feature with the use of Factories within nested collections, rather than anything to do with the collection_factory.
The main collection (main_screen) below contains a collections factory which prototypes the main game element collection (game_screen)…
The main game element then contains a number of factories which then generate a few different elements which refer back to two different script elements (/frame#test and /scorer#score)
However when these elements are created by the factory they appear to address as main_screen:/instance? rather than game_screen:/instance? or even main_screen:/game_screen/instance? as I would expect.
I realise now that I don’t fully know what I expect for addressing when there are nested collections, and I don’t see in the relevant page (but I could just being going snow blind at this point).
The game_screen collection is part of main_screen above only as I was testing the behaviour, it is not intended to be there.
I appreciate that when created via the collection factory that the collection would be “collection0:/” or similar rather than “gamescreen:/” however since I am using relative addressing this shouldn’t matter.