Changing sprite texture/atlas

Hey, I’d like to change the atlas that my sprite is using. And I’ve been following this example: https://www.defold.com/ref/sprite/#texture0 this is my code:

local function changeSprite (self, index)
	local oldAddress = go.get("#sprite", "texture0")
	pprint("Old address"..oldAddress)
	
	local buffer = resource.load(oldAddress)
	local newPath = atlasAddresses[progressionTables.skinReference[index].atlas]
	
	resource.set(newPath, buffer)
	go.animate("#sprite", "play_animation", {id = hash(progressionTables.skinReference[index].texture)})
end

I seem to get an error at line ‘local buffer = resource.load(oldAddress)’, the error is “bad argument #1 to ‘load’ (string expected, got userdata)”. Am I doing something wrong? Did texture0 value type changed, somewhere along the way? What would be the way to change the atlas now?

The oldAddress is a hash (try “print(oldAddress)”). And as the documentation mentions, the `resource.load()´ function takes a string.

Nothing has changed with that function since it was added though. Could it be that your code has changed?

Oh by ‘something has changed’ I meant that the example/tutorial in the link provided (https://www.defold.com/ref/sprite/#texture0) suggested this course of action. That code snippet is freshly written, it didn’t work before either. Also the documentation you linked seems to mention string|hash.

ping?

The documentation is if I’m not mistaken incorrect, ping @sicher . resource.load() loads a resource from disk. It can’t be easily used to change texture.

You want to change the whole texture and not just the animation? I wonder if it wouldn’t be better to have multiple game objects, one for each atlas, and spawn them using factory.create(). Make sure to have the Load Dynamically checkbox checked for the factories so you get ref-counting and load the one you need using factory.load().

2 Likes

Another a little tricky way:

I reported about this issue using “Have a suggestion?” button few months ago, but this example still in docs. (https://www.defold.com/ref/sprite/#texture0)
Anyway would be great to have a possibility to change the texture of the sprite. (then example can stay in the docs =)) )

3 Likes

We have “Resource properties” in the works, and then you’ll be able to do such a thing.

8 Likes

A teardrop down my cheek :smiley:

1 Like