Solarus-Games English Forum

Solarus => Development => Topic started by: MetalZelda on January 24, 2016, 12:05:35 AM

Title: [solved] Is hero:on_taking_damage() broken ?
Post by: MetalZelda on January 24, 2016, 12:05:35 AM
Hi

So I was testing around the hero mode that I implemented in my project.
Now I need to define how much damage a enemy (or anything else)deal to the hero.
But, accessing hero:on_taking_damage(damage) through metatable to overwrite the default function seems not to work
I don't know what have I done wrong, there is no hero:on_taking_damage elsewhere, hero_mode is set to true in the savegame file, so that's not a savegame issue. The code is based on ROTH.

Code (lua) Select
-- On taking damage : Called when the hero takes damage. Increase this if we are in hero mode
  function hero_meta:on_taking_damage(damage) -- Not working
    local game = self:get_game()
local hero_mode = game:get_value("hero_mode")
if hero_mode then
  damage = damage * 2
else
  damage = damage
end 
game:remove_life(damage)
  end
Title: Re: Is hero:on_taking_damage() broken ?
Post by: froggy77 on January 24, 2016, 02:06:17 AM
There is no problem with your script.
I added this line "hero_mode = true" in save1.dat and I added your script in quest_manager.lua.
After "game:remove_life(damage)", I even added this line to test:
print ("hero_mode = ", game:get_value("hero_mode"), "so damage =", damage, "and get life = ", game:get_life())
In debug mode, I typed
game:set_value("hero_mode", false)
and damage returns to normal, so it works.
Title: Re: Is hero:on_taking_damage() broken ?
Post by: MetalZelda on January 24, 2016, 12:43:52 PM
Quote from: froggy77 on January 24, 2016, 02:06:17 AM
There is no problem with your script.
I added this line "hero_mode = true" in save1.dat and I added your script in quest_manager.lua.
After "game:remove_life(damage)", I even added this line to test:
print ("hero_mode = ", game:get_value("hero_mode"), "so damage =", damage, "and get life = ", game:get_life())
In debug mode, I typed
game:set_value("hero_mode", false)
and damage returns to normal, so it works.

Hmmm, does your print display something in the console ?
Mine don't work, weird thing is that there is a hero:on_state_changed in quest manager and this one works, but not on_taking_damage, tried to delete everything from initialize_hero but it did nothing

Weird thing is that it's the only thing that doesn't work here, sword damage works well, item distribution works, but not the damage multiplier

Edit : Oh, I found it, it was overwritten by another system script