Solarus-Games English Forum

Solarus => Development => Topic started by: dpro_games on July 11, 2017, 12:16:00 PM

Title: [SOLVED] How to use: chest:on_opened() ?
Post by: dpro_games 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"'

function sword_chest:on_opened("sword", 1, "sword_chest_save")
  hero:unfreeze()
  chest_open = chest_open + 1
  test()
end
Title: Re: How to use: chest:on_opened() ?
Post by: MetalZelda 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
Title: Re: How to use: chest:on_opened() ?
Post by: dpro_games on July 11, 2017, 04:55:15 PM
Thank for the answere
With this code
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...
Title: Re: How to use: chest:on_opened() ?
Post by: Christopho 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?
Title: Re: How to use: chest:on_opened() ?
Post by: dpro_games on July 11, 2017, 05:04:32 PM
I just want to detect when the chest is open to open a door.
Title: Re: How to use: chest:on_opened() ?
Post by: MetalZelda on July 11, 2017, 06:40:12 PM
Quote from: dpro_games on July 11, 2017, 04:55:15 PM
Thank for the answere
With this code
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
Title: Re: How to use: chest:on_opened() ?
Post by: dpro_games 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.
Title: Re: How to use: chest:on_opened() ?
Post by: dpro_games on July 12, 2017, 06:54:13 PM
I found by myself, that's work !  :D
I use this line:
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