Help: "enemy:on_dying": Get a specific sprite instead of "enemy_killed"

Started by froggy77, October 05, 2014, 06:42:57 PM

Previous topic - Next topic
Hi,

I wonder if it is possible to call a sprite, during the "enemy:on_dying()", other than this sprite "enemies/enemy_killed" , for example, for a specific type of enemy.
"enemies/enemy_killed" would be the default sprite when nothing specified.

If so, what is the way to get it?

Thank you in advance.

See the doc of enemy:on_dying(): http://www.solarus-games.org/doc/latest/lua_api_enemy.html#lua_api_enemy_on_dying
Right now, dying animations are not really customizable. There is only this "hurt style" property that can choose between 3 built-in behaviors. It will be replaced one day by a more customizable API.

In the meantime, there should be a way: instead of changing the sprite, you can change the animation. Keep the sprite "entities/enemy_killed", but add several animations to it.
And in enemy:on_dying(), pick the animation of your choice.

If you really need different sprites, another way might be to make the sprite "entities/enemy_killed" show nothing (like a transparent pixel), and In enemy:on_dying(), create a new sprite of your choice.
Untested but could work :)

Thank you for answering. For now, it does not work. :(
I kept the sprite "sprites/enemies/enemy_killed.png", but added several animations to it (Just a change of colors to test).
I simply copied / pasted the lines from enemy_killed.dat and put y = 32.
So I get two animations in enemy_killed.dat ("killed" and "test")

In   function enemy:on_dying(),   I added
sprite:set_animation("test")
This tries to work with the enemy sprite and not "enemy_killed.png"
So I put this instead.
local sprite2 = enemy:get_sprite()
sprite2:set_animation("test")

This tries to work too with the enemy sprite.and not "enemy_killed.png"

OK, so I tried another thing, I copied my new lines from enemy_killed.dat to paste them into .dat of the enemy.
It's not OK, because the enemy sprite is replaced too soon and then there is the usual animation called "killed".
Moreover, the animation is either too fast (I played with the "frame_delay") or incomplete  because the sprite is killed.

I even deleted the pixels to make transparent the "killed" animation.
I tried with  a create_sprite, but Solarus crashes and I have nothing in error log.
local sprite2 = enemy:create_sprite("enemies/enemy_killed")

What I know is that the problem exists between keyboard and chair. ;D

Quote from: froggy77 on October 06, 2014, 07:46:33 PM
Thank you for answering. For now, it does not work. :(
I kept the sprite "sprites/enemies/enemy_killed.png", but added several animations to it (Just a change of colors to test).
I simply copied / pasted the lines from enemy_killed.dat and put y = 32.
So I get two animations in enemy_killed.dat ("killed" and "test")

In   function enemy:on_dying(),   I added
sprite:set_animation("test")
This tries to work with the enemy sprite and not "enemy_killed.png"
So I put this instead.
local sprite2 = enemy:get_sprite()
sprite2:set_animation("test")

This tries to work too with the enemy sprite.and not "enemy_killed.png"

OK, so I tried another thing, I copied my new lines from enemy_killed.dat to paste them into .dat of the enemy.
It's not OK, because the enemy sprite is replaced too soon and then there is the usual animation called "killed".
Moreover, the animation is either too fast (I played with the "frame_delay") or incomplete  because the sprite is killed.
You are right... So, there is no way in the current API to access the "enemies/enemy_killed" sprite that the engine automatically created. (Unless by doing ugly funny things with sprite metatables... :) )

Quote from: froggy77 on October 06, 2014, 07:46:33 PM
I even deleted the pixels to make transparent the "killed" animation.
I tried with  a create_sprite, but Solarus crashes and I have nothing in error log.
local sprite2 = enemy:create_sprite("enemies/enemy_killed")

What I know is that the problem exists between keyboard and chair. ;D
This should be the preferred workaround with the current API. The engine should not crash. I reported the bug here: https://github.com/christopho/solarus/issues/635 and I will fix it for the next bugfix release.

Thanks for your patience :)

I can't reproduce the crash with the current git version. It may already be fixed, or I did not exactly reproduced what you do. Can you send me your data?

It is not a crash of the engine. The problem with enemy:on_dying() is just that, too much frames in an animation don't have time to finish ; We do not see then all the frames.
I even added a timer. It is replaced too soon by sprite "enemies/enemy_killed" and there is only one possible animation for "killed" (for a single monster).