Author Topic: [SOLVED] How to use: chest:on_opened() ?  (Read 4750 times)

0 Members and 1 Guest are viewing this topic.

dpro_games

  • Newbie
  • *
  • Posts: 47
    • View Profile
[SOLVED] How to use: chest:on_opened() ?
« on: July 11, 2017, 12:16:00 PM »
Hello, I create this code to increment a value when this chest is open but I have this error :
Error: Failed to load script 'maps/dungeons/tuto/place_holder': [string "maps/dungeons/tuto/place_holder.lua"]:5: <name> or '...' expected near '"sword"'

Code: [Select]
function sword_chest:on_opened("sword", 1, "sword_chest_save")
  hero:unfreeze()
  chest_open = chest_open + 1
  test()
end
« Last Edit: July 14, 2017, 07:07:57 PM by dpro_games »

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #1 on: July 11, 2017, 02:23:55 PM »
http://www.solarus-games.org/doc/latest/lua_api_chest.html#lua_api_chest_on_opened

The first argument need to be an item, what you've done is that the first argument is a string

dpro_games

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #2 on: July 11, 2017, 04:55:15 PM »
Thank for the answere
With this code
Code: [Select]
function sword_chest:on_opened(sword, 1, "sword_chest_save")
  hero:unfreeze()
  chest_open = chest_open + 1
  test()
end

I have another error
Error: Failed to load script 'maps/dungeons/tuto/place_holder': [string "maps/dungeons/tuto/place_holder.lua"]:5: <name> or '...' expected near '1'

I don't understand why...

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #3 on: July 11, 2017, 05:01:21 PM »
Your first line is not valid Lua syntax. Are you trying to define a function or to call a function?

dpro_games

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #4 on: July 11, 2017, 05:04:32 PM »
I just want to detect when the chest is open to open a door.

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #5 on: July 11, 2017, 06:40:12 PM »
Thank for the answere
With this code
Code: [Select]
function sword_chest:on_opened(sword, 1, "sword_chest_save")
  hero:unfreeze()
  chest_open = chest_open + 1
  test()
end

I have another error
Error: Failed to load script 'maps/dungeons/tuto/place_holder': [string "maps/dungeons/tuto/place_holder.lua"]:5: <name> or '...' expected near '1'

I don't understand why...

Do like this
Don't know if this will work because I never use chest:on_opened() but that's the way to go


Code: (lua) [Select]
function sword_chest:on_opened(item, variant, savegame)
 
  if item:get_name() == "sword" then

  end
end
« Last Edit: July 11, 2017, 06:43:43 PM by MetalZelda »

dpro_games

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #6 on: July 12, 2017, 06:33:15 PM »
It work but there is still a problem... When I open the chest, the value is increase but the hero don't get the sword in the chest. May I need to give the sword by the code but I don't know the line which give the item like if the player open a chest...
Thank to help me.

dpro_games

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: How to use: chest:on_opened() ?
« Reply #7 on: July 12, 2017, 06:54:13 PM »
I found by myself, that's work !  :D
I use this line:
Code: [Select]
hero:start_treasure(treasure_item, [treasure_variant, [treasure_savegame_variable, [callback]]])To give the treasure to the hero when he open the chest.

Thanks you for helped me !  :D :D