Author Topic: How to mantain the chest activated? (Solved!)  (Read 3619 times)

0 Members and 1 Guest are viewing this topic.

leon
  • Newbie
  • *
  • Posts: 8
    • View Profile
How to mantain the chest activated? (Solved!)
« on: February 21, 2022, 08:16:20 PM »
Hi,

I want the chest to stay activated when I hit the switch, even if it hasn't opened. So when I leave the map and return, the chest remains visible and the switch has already been activated.

I tried this, but without success.

The reference is the lesson 17 on YT.

here´s the code,  first_map.lua

Code: [Select]
function map:on_started() 
  if bau_tunica:is_open() then  --"BAU_TUNICA" IS THE CHEST
    bau_tunica:set_enabled(true)
    interruptor_a:set_activated(true)  --"INTERRUPTOR_A" IS THE SWITCH
  elseif interruptor_a:is_activated() then  --this is my attempt
    bau_tunica:set_enabled(true)
  end
end

function fogo:on_interaction()
  game:start_dialog("salvar_jogo-perguntar", function(resposta)
    if resposta==1 then
      game:save()
      game:start_dialog("jogo_salvo")
    end
  end)
end

function interruptor_a:on_activated()
  sol.audio.play_sound("cursor")
  bau_tunica:set_enabled(true)
end

« Last Edit: April 07, 2022, 01:50:03 PM by leon »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1192
    • View Profile
Re: How to mantain the chest activated?
« Reply #1 on: February 21, 2022, 10:29:06 PM »
The state of the switch is not saved automatically: you have to save it explicitly. In other words, this condition is always false in map:on_started() (when the map starts):
Code: [Select]
elseif interruptor_a:is_activated() then
But you can save the state of the switch yourself when the switch gets activated, with
Code: [Select]
game:set_value("mymap_interruptor_a", true)and test that in map:on_started().

leon
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How to mantain the chest activated?
« Reply #2 on: February 22, 2022, 08:18:35 PM »

I´m sorry, I can´t understand (I´m noob).

What I must to learn to understand this line of code that you showed?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1192
    • View Profile
Re: How to mantain the chest activated?
« Reply #3 on: February 23, 2022, 09:45:23 AM »
game:set_value() is documented here: https://www.solarus-games.org/doc/1.6/lua_api_game.html#lua_api_game_set_value
It sets a variable that can be saved in the savegame file, which is what you want if I understand correctly. So that the information (the switch state) is preserved when the player leaves and reenters the map, or even if they save and quit. You just need to choose a name for your saved variable, in my code snippet I put "mymap_interruptor_a" as an example but it is up to you to choose a relevant name. And the value is a boolean: true to indicate that the switch is activated, false (or unset) otherwise.

Then in map:on_started(), call game:get_value() to retrieve the state of the switch, and if it is saved as activated, then activate it with interruptor_a:set_activated(true).

If you are lacking Lua basics in general, I would recommend the Programming in Lua free guide: https://www.lua.org/pil/contents.html

PS: I just check tutorial 17. In that tutorial, I use the state of the treasure chest (which is automatically saved, as set by a checkbox in the quest editor) to guess the state of the switch, so actually I don't have to do all that.

leon
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How to mantain the chest activated?
« Reply #4 on: March 01, 2022, 07:47:56 PM »
Right, I will try again! Thank you!

leon
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How to mantain the chest activated?
« Reply #5 on: March 19, 2022, 08:30:07 PM »
Hello!

I made it!

Thank you for your help!