[Solved]How to get the hero in an item script?

Started by zutokaza, June 06, 2017, 04:58:05 AM

Previous topic - Next topic
June 06, 2017, 04:58:05 AM Last Edit: June 06, 2017, 06:29:46 PM by zutokaza
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()
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616

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

Code (lua) Select

--slow the hero down
function item:on_map_changed(map)
  local hero = map:get_hero()
  hero:set_walking_speed(30)
end

@Christopho @Zefk
Both solutions work! Thank you!
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616