Advertisement
Eternalfireeater

Loop Library

Jun 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. --Inexorably--
  2.  
  3.  
  4.  
  5. --Library
  6.     local Loop={}
  7. --//
  8.  
  9.  
  10. --Library functions
  11.     Loop.Methods=function()
  12.         return {
  13.             Wait=function(self)
  14.                 repeat
  15.                     wait()
  16.                 until not self.Stop
  17.             end,
  18.        
  19.        
  20.             Yield=function(self)
  21.                 repeat
  22.                     wait()
  23.                 until self.Done
  24.             end,
  25.            
  26.            
  27.             RunOnDone=function(self,_function)
  28.                 coroutine.resume(coroutine.create(function()
  29.                     repeat
  30.                         wait()
  31.                     until self.Done
  32.                    
  33.                     _function()
  34.                 end))
  35.             end,
  36.            
  37.            
  38.             Resume=function(self)
  39.                 self.Stop=false
  40.                
  41.                 return self
  42.             end,
  43.            
  44.            
  45.             Pause=function(self)
  46.                 self.Stop=true
  47.                
  48.                 return self
  49.             end,
  50.            
  51.            
  52.             Status=function(self)
  53.                 if self.Stop then
  54.                     return "Paused"
  55.                    
  56.                 elseif not self.Stop then
  57.                     return "Running"
  58.                    
  59.                 elseif self.Done then
  60.                     return "Done"
  61.                 end
  62.             end
  63.         }
  64.     end
  65.    
  66.    
  67.     Loop.New=function(_type,_function,info)
  68.         local newLoop=Loop.Methods()
  69.               newLoop:Pause()
  70.        
  71.         if _type=="Numeric" then
  72.             coroutine.resume(coroutine.create(function()
  73.                 for i=info.Start,info.End,(info.Increment or 1) do
  74.                     if newLoop.Stop then newLoop:Wait() end
  75.                        
  76.                     _function(i)
  77.                 end
  78.                
  79.                 newLoop.Done=true
  80.             end))
  81.            
  82.             return newLoop
  83.            
  84.         elseif _type=="Generic" then
  85.             coroutine.resume(coroutine.create(function()
  86.                 for i,v in next, info.Table do
  87.                     if newLoop.Stop then newLoop:Wait() end
  88.                        
  89.                     _function(i,v)
  90.                 end
  91.                
  92.                 newLoop.Done=true
  93.             end))
  94.            
  95.             return newLoop
  96.         end
  97.     end
  98. --//
  99.  
  100.  
  101. --Numeric example
  102.     print("Numeric")
  103.     local Print=function(i) print(i) wait(1) end
  104.    
  105.     local loop=Loop.New("Numeric", --type of loop goes here
  106.           Print, --function to call each iteration goes here (numeric passes 'i' (iteration) as first arg)
  107.           {Start=10,End=1,Increment=-1} --'for i=start,end,increment do' (increment defaults to 1)
  108.     )
  109.     loop:Resume() --like a coroutine, you have to 'Resume' it to start it
  110.     wait(3)
  111.     loop:Pause() --it will pause when it starts the next iteration and hits the if statement
  112.     print(loop:Status()) -->Paused
  113.     wait(3)
  114.     loop:Resume()
  115.     loop:Yield() --yields script until loop finishes
  116.     print("Loop done!")
  117. --//
  118.  
  119.  
  120. --Generic example
  121.     print("Generic")
  122.     local Print=function(i,v) print(i,v) wait(1) end --passes iteration,value
  123.     local DoneFunction=function() print("Loop done (:") end
  124.    
  125.     local loop=Loop.New("Generic", --type of loop
  126.           Print, --function to call each iteration (this time it passes 'i,v')
  127.           {Table=workspace:GetChildren()} --takes the table to iterate through
  128.     ):Resume() --we can just resume it here (and could put it all on one line) because Resume returns the loop
  129.     loop:RunOnDone(DoneFunction) --fires the given function after the loop finishes (does not yield script)
  130.     loop:Yield() --this is only here to separate this test from the one below
  131. --//
  132.  
  133.  
  134. --Numeric & generic simultaneously
  135.     print("Numeric & Generic")
  136.     local Print=function(i,v) print(i,v) wait(1) end
  137.     local DoneFunction=function() print("Done! (:") end
  138.    
  139.     Loop.New("Numeric",Print,{Start=1,End=10}):Resume():RunOnDone(DoneFunction)
  140.     Loop.New("Generic",Print,{Table=workspace:GetChildren()}):Resume():RunOnDone(DoneFunction)
  141. --//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement