Change a game object's sprite in script

  1. Can a game object’s sprite be changed in the script?
  2. If the game object’s sprite is a spritesheet, is it possible to choose just any 1 of the items to display.
1 Like

If your looking for swapping basic animations you can use the flipbook api: Flipbook animations in Defold manual

1 Like

If sprite is using an atlas, you can use

 sprite.play_flipbook("#sprite", hash("atlas_animation"))

If it’s using a spritesheet, then on the spritesheet, you need to create a new animation, set the start and end sprite to the specific sprite you want to use and set the playback to none. This way, you can use a specific sprite from a spritesheet

1 Like

thanks

I can not get it to work. I am using a spritesheet with 3 animations. I am getting the following error:

ERROR:GAMEOBJECT: Component ‘/CoinFactory#sprite’ could not be found when dispatching message ‘play_animation’ sent from main:/CoinFactory#CoinFactory

my code follows:

factory.create(“#factory”, position, nil, {}, 1.5)
sprite.play_flipbook(“#sprite”, hash(“anim2”))

Is the sprite on the game object where you have the factory component is? Or are you trying to play animation on the created game object?

The sprite is attached to the game object of the factory’s prototype. The sprite’s image is the tilesource and the it’s animation is one of the 3 animations.

Then, you will need to do:

local go_id = factory.create("#factory", position, nil, {}, 1.5)
local sprite_url = msg.url(go_id, "sprite")
sprite.play_flipbook(sprite_url, hash("anim2"))

For GO created by factories, you need to use (save) their url to control them

2 Likes

You have to provide nil for the socket if I’m not mistaken:

local sprite_url = msg.url(nil, go_id, "sprite")

2 Likes

That works!!! Thanks so much!

1 Like

I want to randomize which animation that displays. I manually created a table with several of the animations and using randomize, it worked fine. Is there anyway to automatically load the list of all the animations for the sprite, then load the table from it?

You could probably create a table on a Lua module and access the table from the script controlling factory.

Thanks for the advise, I will try to figure it out.

1 Like

You could create a module:

local M = {}

local anim_table= {"anim1", "anim2"...}

function M.get_anim_table()
    return anim_table
end

return M

And, on the script that controls factory:

local global_module = require("global_module")
local anim_table = global_module.get_anim_table()
-- Here, you could pick a random anim from table and apply on the GO created by the factory

You can get information from an atlas, but there will be all animations in it, not only the ones you want for a particular sprite.
If you have a good naming convention you could use a prefix to filter the desired animations.
But the simplest is to put the list/mapping in a lua table. You’ll do it once then forget.
Static is better then dynamic.

1 Like

Thank you both, very much! I think it is now working.

1 Like