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

dpro_games

  • Newbie
  • *
  • Posts: 33
    • 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: 511
    • 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: 33
    • 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: 1053
    • 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: 33
    • 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: 511
    • 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
  1. function sword_chest:on_opened(item, variant, savegame)
  2.  
  3.   if item:get_name() == "sword" then
  4.  
  5.   end
  6. end
« Last Edit: July 11, 2017, 06:43:43 pm by MetalZelda »

dpro_games

  • Newbie
  • *
  • Posts: 33
    • 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: 33
    • 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