Author Topic: Help: "enemy:on_dying": Get a specific sprite instead of "enemy_killed"  (Read 2277 times)

froggy77

  • Full Member
  • ***
  • Posts: 137
    • View Profile
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.
« Last Edit: October 05, 2014, 07:56:29 pm by froggy77 »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Help: "enemy:on_dying": Get a specific sprite instead of "enemy_killed"
« Reply #1 on: October 05, 2014, 08:33:10 pm »
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 :)

froggy77

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Re: Help: "enemy:on_dying": Get a specific sprite instead of "enemy_killed"
« Reply #2 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
Code: [Select]
sprite:set_animation("test")This tries to work with the enemy sprite and not "enemy_killed.png"
So I put this instead.
Code: [Select]
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.
Code: [Select]
local sprite2 = enemy:create_sprite("enemies/enemy_killed")
What I know is that the problem exists between keyboard and chair. ;D
« Last Edit: October 06, 2014, 07:48:20 pm by froggy77 »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Help: "enemy:on_dying": Get a specific sprite instead of "enemy_killed"
« Reply #3 on: October 06, 2014, 09:07:53 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
Code: [Select]
sprite:set_animation("test")This tries to work with the enemy sprite and not "enemy_killed.png"
So I put this instead.
Code: [Select]
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… :) )

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.
Code: [Select]
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 :)
« Last Edit: October 06, 2014, 09:09:28 pm by Christopho »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
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?

froggy77

  • Full Member
  • ***
  • Posts: 137
    • View Profile
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).