[Solved]Timer problem

Started by Zefk, July 16, 2018, 12:00:52 AM

Previous topic - Next topic
July 16, 2018, 12:00:52 AM Last Edit: July 16, 2018, 05:30:45 AM by Zefk
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



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

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.

July 16, 2018, 05:14:41 AM #3 Last Edit: July 16, 2018, 05:32:25 AM by Zefk
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

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.