Using chest to open a door

Started by jojo_f, March 06, 2018, 08:42:12 AM

Previous topic - Next topic
Here is the next question in my Solarus quest! Thanks for all of the help so far. Hopefully the more help one receives, the less one will need (maybe) but at the very least these questions will be logged for the needs of future users.

So today I am making a chest to open a door. This will be the very first room of the game, where possession of the tunic will be set to 0 and you receive the first tunic variant before you can exit the room. This will of course require some custom non-tunic Link sprites but that is not the purpose of this post. I am using the basics tutorial #32 ("doors") as a guide.

So if I have the chest in "clothes_chest" and the exit door is "exit_door" with the clothing savegame variable in "green_tunic", then use this script:

Code ( lua) Select
function clothes_chest:on_opened(tunic, 1, green_tunic)

map:open_doors("exit_door")
sol.audio.play_sound("door_open")
end


The tunic is given but the door does not open. However if I make a slight alteration by deleting the variant in the first line:

Code ( lua) Select
function clothes_chest:on_opened(tunic, green_tunic)

map:open_doors("exit_door")
sol.audio.play_sound("door_open")
end


The door will open, but the hero gets paused permanently at the chest, and the tunic is not given.

March 06, 2018, 11:36:05 PM #1 Last Edit: March 06, 2018, 11:49:46 PM by jojo_f
This thread is very helpful , not sure how I missed it :
http://forum.solarus-games.org/index.php/topic,1014.0.html

This works great. It can be so frustrating not knowing what is causing certain errors!

Code ( lua) Select

function 
clothes_chest:on_opened()

hero:start_treasure("tunic", 1)
map:open_doors("exit_door")
sol.audio.play_sound("door_open")

end


I still am unsure why chest_name:on_opened("treasure", variant-number) doesn't work?

Your usage of arguements in the function is incorrect.

The way defining functions work in lua (And in most if not all programming languages with some differences) is the section within the parentheses are variable only accessible from within the function.
So instead what you would want to do is something like this:

Code (lua) Select

function clothes_chest:on_opened(treasure_item, treasure_variant, treasure_savegame_variable)
  map:get_hero():start_treasure(treasure_item, treasure_variant, treasure_savegame_variable, function() -- The function here is to indicate an event happens after receiving an item
    map:open_doors("exit_door")
    sol.audio.play_sound("door_open")
  end)
end
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

Thank you! I see that what I was missing, is giving the treasure to the hero. Although somehow I took it for granted that it would occur. ::) Even receiving a response with advice makes me happy because, I feel very out of my depth sometimes using Solarus, but intend to keep learning.