How to set atlas property from factory?

Hi again.

If I have a script with go.property("my_atlas", resource.atlas("/atlas.atlas")) defined, how do I set this when the instance is created from a factory?

local props = { health = 50, target = msg.url("player") }
local id = factory.create("#can_factory", nil, nil, props)

-- Accessing to factory created script properties
local url = msg.url(nil, id, "can")
local can_health = go.get(url, "health")

resource.atlas(...) can only be used inside of go.property() as stated by the API documentation.

If I have a hundred atlases (I will likely end up with more and currently generate them in my external editor…) and want to dynamically select which one to use, how do I accomplish this?

Interesting problem! If you have hundreds of them you don’t want to use go.property("my_atlas", resource.atlas("/atlas.atlas")) since all of them will be in memory at once.

What I would do is to per atlas create one dummy game object file with a sprite which references the atlas. Each dummy game object is referenced from a factory with the Load Dynamically checkbox set.

When you want to use an atlas you load the resources used by the correct factory using factory.load(). This will load the atlas into memory after which you can set it on your game objects.

1 Like

Thanks for the reply! That’s actually exactly what I’m doing at the moment, but thought might be an easier way.

I am very glad the game object and atlas files are raw text and easy to generate in external tools - I wouldn’t want to make all the atlases and add all the factory components by hand!

We’ve talked about adding an option to not automatically load resources referenced in a script property. Once we have that it will be a lot easier to set up something like this.

2 Likes