Author Topic: [Solved]Timer problem  (Read 209 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
  1. function map:on_started()
  2.  
  3.   sol.timer.start(1000, function()
  4.     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
  5.       sol.timer.start(5000, function()
  6.         solid_1:set_activated(false)
  7.         solid_2:set_activated(false)
  8.         solid_3:set_activated(false)
  9.         solid_4:set_activated(false)
  10.       end)
  11.     end
  12.     return true
  13.   end)
  14.  
  15. 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
  1. function map:on_started()
  2.  
  3. function solid_1:on_activated()
  4.     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
  5.       timer_sound = sol.timer.start(5000, function()
  6.         solid_1:set_activated(false)
  7.         solid_2:set_activated(false)
  8.         solid_3:set_activated(false)
  9.         solid_4:set_activated(false)
  10.       end)
  11.       timer_sound:set_with_sound(true)
  12.     end
  13. end
  14.  
  15. function solid_2:on_activated()
  16.     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
  17.       timer_sound = sol.timer.start(5000, function()
  18.         solid_1:set_activated(false)
  19.         solid_2:set_activated(false)
  20.         solid_3:set_activated(false)
  21.         solid_4:set_activated(false)
  22.       end)
  23.     timer_sound:set_with_sound(true)
  24.     end
  25. end
  26.  
  27. function solid_3:on_activated()
  28.     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
  29.       timer_sound = sol.timer.start(5000, function()
  30.         solid_1:set_activated(false)
  31.         solid_2:set_activated(false)
  32.         solid_3:set_activated(false)
  33.         solid_4:set_activated(false)
  34.       end)
  35.     timer_sound:set_with_sound(true)
  36.     end
  37. end
  38.  
  39. function solid_4:on_activated()
  40.     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
  41.       timer_sound = sol.timer.start(5000, function()
  42.         solid_1:set_activated(false)
  43.         solid_2:set_activated(false)
  44.         solid_3:set_activated(false)
  45.         solid_4:set_activated(false)
  46.       end)
  47.     timer_sound:set_with_sound(true)
  48.     end
  49. end

Max

  • Full Member
  • ***
  • Posts: 167
    • 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
  1. function map:on_started()
  2.  
  3. local switches_on= 0
  4.  
  5. for switch in map:get_entities("solid") do
  6.   function switch:on_activated()
  7.     switches_on = switches_on + 1
  8.     if switches_on >= 4 then
  9.       switches_on = 0
  10.       sol.timer.start(5000, function() deactivate_switches() end)
  11.       timer_sound:set_with_sound(true)
  12.     end
  13.   end
  14. end
  15.  
  16. function deactivate_switches()
  17.    for switch in map:get_entities("solid") do
  18.      switch:set_activated(false)
  19.    end
  20. end
  21.  
  22. end

Max

  • Full Member
  • ***
  • Posts: 167
    • 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.