Author Topic: Using chest to open a door  (Read 348 times)

jojo_f

  • Newbie
  • *
  • Posts: 20
    • View Profile
Using chest to open a door
« on: March 06, 2018, 08:42:12 am »
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
  1. function clothes_chest:on_opened(tunic, 1, green_tunic)
  2.  
  3. map:open_doors("exit_door")
  4. sol.audio.play_sound("door_open")
  5. end
  6.  

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
  1. function clothes_chest:on_opened(tunic, green_tunic)
  2.  
  3. map:open_doors("exit_door")
  4. sol.audio.play_sound("door_open")
  5. end
  6.  

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

jojo_f

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Using chest to open a door
« Reply #1 on: March 06, 2018, 11:36:05 pm »
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
  1. function  
  2. clothes_chest:on_opened()
  3.  
  4. hero:start_treasure("tunic", 1)
  5. map:open_doors("exit_door")
  6. sol.audio.play_sound("door_open")
  7.  
  8. end
  9.  

I still am unsure why chest_name:on_opened("treasure", variant-number) doesn't work?
« Last Edit: March 06, 2018, 11:49:46 pm by jojo_f »

YoshiMario2000

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Using chest to open a door
« Reply #2 on: March 06, 2018, 11:36:49 pm »
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
  1. function clothes_chest:on_opened(treasure_item, treasure_variant, treasure_savegame_variable)
  2.   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
  3.     map:open_doors("exit_door")
  4.     sol.audio.play_sound("door_open")
  5.   end)
  6. end
  7.  
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?

jojo_f

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Using chest to open a door
« Reply #3 on: March 06, 2018, 11:55:01 pm »
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.