How to mantain the chest activated? (Solved!)

Started by leon, February 21, 2022, 08:16:20 PM

Previous topic - Next topic
February 21, 2022, 08:16:20 PM Last Edit: April 07, 2022, 01:50:03 PM by leon
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


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



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):
elseif interruptor_a:is_activated() then

But you can save the state of the switch yourself when the switch gets activated, with
game:set_value("mymap_interruptor_a", true)
and test that in map:on_started().


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

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

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.


Hello!

I made it!

Thank you for your help!