How to Create Collection Factory Directly from Assets?

Hi everyone, nice to meet you! My name is Hanz and I’m new to Defold. I want to spawn a group of game objects using a collection factory from my assets. I tried calling this in my game manager script attached to a game object in the main collection:

Blockquote
function init(self)
local factory_url = msg.url(“/factory/stage_1.collectionfactory#stage_1”)
print(“Factory URL:”, factory_url)
collectionfactory.create(factory_url, vmath.vector3(0, 0, 0))
end

But I get the error “Instance (null) not found.” However, if I add the collection factory component inside a game object that is a child of the game manager in the main collection, it works fine.

So my question is:
Can a collection factory be referenced directly from the asset file and created at runtime, or must it always be part of a game object inside the main collection to be instantiated? thank you

You can access factories from different game objects but need to use the correct url for them. If you share your collection hierarchy, I can try helping you with it

Hi Asatte, thanks for your response. I’ve attached my project for reference.

I want to spawn the stage_1 collection factory at runtime without having to reference it as a game object in the main collection. The reason is that I plan to have multiple levels or stages, and I want to manage their data via a Lua module. Then, at runtime, I can create the stage dynamically based on this data.

My main collection will remain loaded at all times. When the player completes a stage, I want to destroy the previous stage’s collection instance and spawn the next stage using the collection factory URL referenced from the Lua module.

In short, the collection factory will act as the stage container that I create and destroy dynamically during gameplay.

I think you need this:

Hi Alex, thanks for your reference, I will check them out. Really appreciated it. Cheers

1 Like

My approach to your goal would be:

  • Creating a game object on main (SceneManager)
  • Adding collection factories (one for each level you would have in your game) and setting the collections
  • Loading the initial collection (possibly main menu)
  • Handling the scenes by unloading current collection (loaded from factories) and loading the new one

I have a tutorial on changing scenes here: https://www.youtube.com/watch?v=1pxi8wDoagU
And one for reloading scenes: https://www.youtube.com/watch?v=fB0S15AOtb4
I would recommend just using the reload tutorial as it also covers the first tutorial’s change scene implementation but improved to be more dynamic.

While I would suggest the way on the tutorial, I am no expert either so you can wait till more experienced Defold users to answer your question as I am not sure how to achieve your current goal

Hi Asatte, thanks for sharing your tutorial reference—I’ll definitely watch it tonight. I’ve only been using Defold for about a month, coming from Unity, so I’m still getting used to how things work here. Thanks for your insight i will try it. Cheers

1 Like