Dynamically change atlas of an object

Hello! New Defold user here. I’m wondering if it’s possible to change/swap an atlas of a sprite at runtime?

List item

I tried using:

go.set("#sprite", "image", hash("/game/atlas/skin_002.atlas"))

The path seems to be correct, but the game returns “go.set failed with error code -10”

Then I tried:
go.set("#sprite", "image", "/game/atlas/skin_002.atlas")
which obviously returns “the property ‘image’ of ‘#sprite’ must be a hash”

I’ve seen posts on the forum where users define the atlas as a resource like this
go.property("skin_xmas", resource.atlas())
and then use it :
go.set("/go#sprite", "image", self.skin_xmas)

Second approach seems to be working for me, but I’m not sure if I want to use it for something more complex as it feels a bit hardcoded. Is that approach totally fine, or am I missing something here?

1 Like

It is. If the resources is not referenced it is not included in the bundle

1 Like

Exactly this. Defold will not include resources without a reference in the scene graph. Using go.property("foo", resource.atlas("/my.atlas")) will make sure the my.atlas is referenced and included in the build.

Here’s a similar example (although using a tilesource instead of an atlas, but the principle is the same):