Author Topic: [Solved]How to get the hero in an item script?  (Read 315 times)

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
[Solved]How to get the hero in an item script?
« 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
  1. --slow the hero down
  2. function item:on_map_changed(map)
  3.   hero:set_walking_speed(30)
  4. end

I tried:
Code: Lua
  1. local item = ...
  2. local game = item:get_game()
  3. local hero = game:get_hero()

and

Code: Lua
  1. local item = ...
  2. local game = item:get_game()
  3. local map = item:get_map()
  4. local hero = map:get_entity("hero")

and

Code: Lua
  1. local item = ...
  2. local game = item:get_game()
  3. local map = item:get_map()
  4. local hero = map:get_hero()
« Last Edit: June 06, 2017, 06:29:46 pm by zutokaza »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: How to get the hero in an item script?
« Reply #1 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
  1. function item:on_obtaining()
  2.   self:get_map():get_entity("hero"):set_walking_speed(120)
  3. end
  4.  
  5. if game:get_value("sola_house_f1_heart_gem_finish") then
  6.   function item:on_map_changed(map)
  7.     self:get_map():get_entity("hero"):set_walking_speed(120)
  8.   end
  9. end

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1041
    • View Profile
Re: How to get the hero in an item script?
« Reply #2 on: June 06, 2017, 10:39:13 am »
Code: Lua
  1. --slow the hero down
  2. function item:on_map_changed(map)
  3.   local hero = map:get_hero()
  4.   hero:set_walking_speed(30)
  5. end
  6.  

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: [Solved]How to get the hero in an item script?
« Reply #3 on: June 06, 2017, 06:32:08 pm »
@Christopho @Zefk
Both solutions work! Thank you!