Solarus-Games English Forum

Solarus => Development => Topic started by: zutokaza on June 06, 2017, 04:58:05 AM

Title: [Solved]How to get the hero in an item script?
Post by: zutokaza on June 06, 2017, 04:58:05 AM
I am wondering how to get the hero in an item script.

I want to do this:

Code ( lua) Select
--slow the hero down
function item:on_map_changed(map)
  hero:set_walking_speed(30)
end


I tried:
Code ( lua) Select
local item = ...
local game = item:get_game()
local hero = game:get_hero()


and

Code ( lua) Select
local item = ...
local game = item:get_game()
local map = item:get_map()
local hero = map:get_entity("hero")


and

Code ( lua) Select
local item = ...
local game = item:get_game()
local map = item:get_map()
local hero = map:get_hero()
Title: Re: How to get the hero in an item script?
Post by: Zefk on June 06, 2017, 06:09:15 AM
One solution is using "self". I did this when making boots that speed up walking.

Code ( lua) Select
function item:on_obtaining()
  self:get_map():get_entity("hero"):set_walking_speed(120)
end

if game:get_value("sola_house_f1_heart_gem_finish") then
  function item:on_map_changed(map)
    self:get_map():get_entity("hero"):set_walking_speed(120)
  end
end
Title: Re: How to get the hero in an item script?
Post by: Christopho on June 06, 2017, 10:39:13 AM
Code (lua) Select

--slow the hero down
function item:on_map_changed(map)
  local hero = map:get_hero()
  hero:set_walking_speed(30)
end
Title: Re: [Solved]How to get the hero in an item script?
Post by: zutokaza on June 06, 2017, 06:32:08 PM
@Christopho @Zefk
Both solutions work! Thank you!