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

0 Members and 1 Guest are viewing this topic.

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) [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()
« Last Edit: June 06, 2017, 06:29:46 PM by zutokaza »

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
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) [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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
Re: How to get the hero in an item script?
« Reply #2 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

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!