Author Topic: [Solved]Timer problem  (Read 575 times)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
[Solved]Timer problem
« on: July 16, 2018, 12:00:52 am »
I have a problem with my script. The switches are suppose to reset after 5 seconds when they are all activated. They do reset, but the one second timer is already running through again and I cannot activate any switches for another 5 seconds.

Code: ( lua) [Select]
function map:on_started()

  sol.timer.start(1000, function()
    if solid_1:is_activated() == true and solid_2:is_activated() == true and solid_3:is_activated() == true and solid_4:is_activated() == true then
      sol.timer.start(5000, function()
        solid_1:set_activated(false)
        solid_2:set_activated(false)
        solid_3:set_activated(false)
        solid_4:set_activated(false)
      end)
    end
    return true
  end)

end


Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Timer problem
« Reply #1 on: July 16, 2018, 02:09:27 am »
The following script is one solution:

Code: ( lua) [Select]
function map:on_started()

function solid_1:on_activated()
    if solid_1:is_activated() == true and solid_2:is_activated() == true and solid_3:is_activated() == true and solid_4:is_activated() == true then
      timer_sound = sol.timer.start(5000, function()
        solid_1:set_activated(false)
        solid_2:set_activated(false)
        solid_3:set_activated(false)
        solid_4:set_activated(false)
      end)
      timer_sound:set_with_sound(true)
    end
end

function solid_2:on_activated()
    if solid_1:is_activated() == true and solid_2:is_activated() == true and solid_3:is_activated() == true and solid_4:is_activated() == true then
      timer_sound = sol.timer.start(5000, function()
        solid_1:set_activated(false)
        solid_2:set_activated(false)
        solid_3:set_activated(false)
        solid_4:set_activated(false)
      end)
    timer_sound:set_with_sound(true)
    end
end

function solid_3:on_activated()
    if solid_1:is_activated() == true and solid_2:is_activated() == true and solid_3:is_activated() == true and solid_4:is_activated() == true then
      timer_sound = sol.timer.start(5000, function()
        solid_1:set_activated(false)
        solid_2:set_activated(false)
        solid_3:set_activated(false)
        solid_4:set_activated(false)
      end)
    timer_sound:set_with_sound(true)
    end
end

function solid_4:on_activated()
    if solid_1:is_activated() == true and solid_2:is_activated() == true and solid_3:is_activated() == true and solid_4:is_activated() == true then
      timer_sound = sol.timer.start(5000, function()
        solid_1:set_activated(false)
        solid_2:set_activated(false)
        solid_3:set_activated(false)
        solid_4:set_activated(false)
      end)
    timer_sound:set_with_sound(true)
    end
end

Max

  • Full Member
  • ***
  • Posts: 212
    • View Profile
Re: Timer problem
« Reply #2 on: July 16, 2018, 04:09:43 am »
You could try this syntax:

Local switches_on= 0

For switch in map:get_entities("solid") do
switch:on_activated()
Switches_on = switches on + 1
If switches_on >= 4 then
 Switches_on = 0
 Sol.timer.start(5000, function() deactivate_switches() end)
End
End
End

Local Function deactivate_switches()
 For switch in map:get_entities("solid") do
 Switch:set_activated(false)
End
End


I typed this on mobile so the capitalization is off and there's typos probably, but I think that'd work.

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: [Solved]Timer problem
« Reply #3 on: July 16, 2018, 05:14:41 am »
That does indeed Work. Thanks @Max!  ;D

Code: ( lua) [Select]
function map:on_started()

local switches_on= 0

for switch in map:get_entities("solid") do
  function switch:on_activated()
    switches_on = switches_on + 1
    if switches_on >= 4 then
      switches_on = 0
      sol.timer.start(5000, function() deactivate_switches() end)
      timer_sound:set_with_sound(true)
    end
  end
end

function deactivate_switches()
   for switch in map:get_entities("solid") do
     switch:set_activated(false)
   end
end

end

Max

  • Full Member
  • ***
  • Posts: 212
    • View Profile
Re: [Solved]Timer problem
« Reply #4 on: July 16, 2018, 06:19:19 am »
No problem! The syntax

For x in map:get_entities("prefix") do
X:function()
End

Is super useful for applying the same code to an arbitrary number of entities. Once I found out a out it, my life was made much easier, lol.