Solarus-Games English Forum

Solarus => Development => Topic started by: Starlock on December 29, 2016, 06:07:52 PM

Title: How to specify custom attack sprite
Post by: Starlock on December 29, 2016, 06:07:52 PM
I'm trying to create a boss that can only be hurt when attacked by a thrown item with a specific sprite but I'm not sure how to call the thrown items sprite in the enemy code.

Code ( lua) Select
function enemy:on_custom_attack_received(attack, sprite)
  if attack == "thrown_item" and attack:get_sprite() == "entities/boss3block" then
  enemy:hurt(1)
  enemy:remove_life(1)
  end
end


This was my attempt but it gives me an error that the get_sprite() is calling a nil value
Title: Re: How to specify custom attack sprite
Post by: YoshiMario2000 on December 29, 2016, 06:37:11 PM
Try using the sprite value that is given in the function.
Quote from: http://www.solarus-games.org/doc/1.5/lua_api_enemy.html#lua_api_enemy_on_custom_attack_receivedenemy:on_custom_attack_received(attack, sprite)

Called when this enemy receives an attack with a custom effect.

This function is called if you have set consequence of the attack to "custom". You have to define what happens, for example hurting the enemy, making a special reaction, etc.

attack (string): The attack that was received: "sword", "thrown_item", "explosion", "arrow", "hookshot", "boomerang" or "fire".
sprite (sprite): The sprite of this enemy that receives the attack, or nil if the attack does not come from a pixel-precise collision.

So to run the function I would do,
Code (lua) Select
function enemy:on_custom_attack_received(attack, sprite)
  if attack == "thrown_item"
    and sprite:get_animation_set() == "entities/boss3block" then
    enemy:hurt(1)
    enemy:remove_life(1)
  end
end
Title: Re: How to specify custom attack sprite
Post by: MetalZelda on December 29, 2016, 10:05:45 PM
As the documentation says, sprite refers to a sprite of the enemy, not the thrown item, you have to get the thrown item by yourself, in my project, while battling king dodongo for example, the room is only filled with bombs, but he can swallow jars, bushes ...

And you just have to call enemy:hurt(1), enemy:remove_life is handled by hurt()

Yet, I admit that a attack:get_sprite() for this type of function would be very helpful, I don't know how this is doable