Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Inexorably--
- --Library
- local Loop={}
- --//
- --Library functions
- Loop.Methods=function()
- return {
- Wait=function(self)
- repeat
- wait()
- until not self.Stop
- end,
- Yield=function(self)
- repeat
- wait()
- until self.Done
- end,
- RunOnDone=function(self,_function)
- coroutine.resume(coroutine.create(function()
- repeat
- wait()
- until self.Done
- _function()
- end))
- end,
- Resume=function(self)
- self.Stop=false
- return self
- end,
- Pause=function(self)
- self.Stop=true
- return self
- end,
- Status=function(self)
- if self.Stop then
- return "Paused"
- elseif not self.Stop then
- return "Running"
- elseif self.Done then
- return "Done"
- end
- end
- }
- end
- Loop.New=function(_type,_function,info)
- local newLoop=Loop.Methods()
- newLoop:Pause()
- if _type=="Numeric" then
- coroutine.resume(coroutine.create(function()
- for i=info.Start,info.End,(info.Increment or 1) do
- if newLoop.Stop then newLoop:Wait() end
- _function(i)
- end
- newLoop.Done=true
- end))
- return newLoop
- elseif _type=="Generic" then
- coroutine.resume(coroutine.create(function()
- for i,v in next, info.Table do
- if newLoop.Stop then newLoop:Wait() end
- _function(i,v)
- end
- newLoop.Done=true
- end))
- return newLoop
- end
- end
- --//
- --Numeric example
- print("Numeric")
- local Print=function(i) print(i) wait(1) end
- local loop=Loop.New("Numeric", --type of loop goes here
- Print, --function to call each iteration goes here (numeric passes 'i' (iteration) as first arg)
- {Start=10,End=1,Increment=-1} --'for i=start,end,increment do' (increment defaults to 1)
- )
- loop:Resume() --like a coroutine, you have to 'Resume' it to start it
- wait(3)
- loop:Pause() --it will pause when it starts the next iteration and hits the if statement
- print(loop:Status()) -->Paused
- wait(3)
- loop:Resume()
- loop:Yield() --yields script until loop finishes
- print("Loop done!")
- --//
- --Generic example
- print("Generic")
- local Print=function(i,v) print(i,v) wait(1) end --passes iteration,value
- local DoneFunction=function() print("Loop done (:") end
- local loop=Loop.New("Generic", --type of loop
- Print, --function to call each iteration (this time it passes 'i,v')
- {Table=workspace:GetChildren()} --takes the table to iterate through
- ):Resume() --we can just resume it here (and could put it all on one line) because Resume returns the loop
- loop:RunOnDone(DoneFunction) --fires the given function after the loop finishes (does not yield script)
- loop:Yield() --this is only here to separate this test from the one below
- --//
- --Numeric & generic simultaneously
- print("Numeric & Generic")
- local Print=function(i,v) print(i,v) wait(1) end
- local DoneFunction=function() print("Done! (:") end
- Loop.New("Numeric",Print,{Start=1,End=10}):Resume():RunOnDone(DoneFunction)
- Loop.New("Generic",Print,{Table=workspace:GetChildren()}):Resume():RunOnDone(DoneFunction)
- --//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement