Core Addition (1.5.0)
Updated version from 1.4.1 to 1.5.0 Added: multiObj:SetTime(n) multiObj:ResetTime(n) multiObj:ResolveTimer(...) multiObj:OnTimedOut([function(self)]) multiObj:OnTimerResolved([function(self)],...) Added: multi:getVersion() Example will be posted in the readme
This commit is contained in:
parent
b65711cf1c
commit
65a4dfa9c9
59
README.md
59
README.md
@ -1,4 +1,11 @@
|
|||||||
# multi Version: 1.4.1
|
# multi Version: 1.5.0
|
||||||
|
Updated from 1.4.1
|
||||||
|
Added:
|
||||||
|
- An easy way to manage timeouts
|
||||||
|
- Small bug fixes
|
||||||
|
|
||||||
|
Example at end of the readme
|
||||||
|
|
||||||
My multitasking library for lua</br>
|
My multitasking library for lua</br>
|
||||||
To install copy the multi folder into your enviroment and you are good to go</br>
|
To install copy the multi folder into your enviroment and you are good to go</br>
|
||||||
|
|
||||||
@ -697,6 +704,54 @@ multi:mainloop()
|
|||||||
...</br>
|
...</br>
|
||||||
.inf-1 inf</br>
|
.inf-1 inf</br>
|
||||||
|
|
||||||
|
Timeout management
|
||||||
|
```lua
|
||||||
|
-- Note: I used a tloop so I could control the output of the program a bit.
|
||||||
|
require("multi.tloop")
|
||||||
|
a=0
|
||||||
|
inc=1 -- change to 0 to see it not met at all, 1 if you want to see the first condition not met but the second and 2 if you want to see it meet the condition on the first go.
|
||||||
|
loop=multi:newTLoop(function(self)
|
||||||
|
print("Looping...")
|
||||||
|
a=a+inc
|
||||||
|
if a==14 then
|
||||||
|
self:ResolveTimer("1","2","3") -- ... any number of arguments can be passed to the resolve handler
|
||||||
|
-- this will also automatically pause the object that it is binded to
|
||||||
|
end
|
||||||
|
end,.1)
|
||||||
|
loop:SetTime(1)
|
||||||
|
loop:OnTimerResolved(function(self,a,b,c) -- the handler will return the self and the passed arguments
|
||||||
|
print("We did it!",a,b,c)
|
||||||
|
end)
|
||||||
|
loop:OnTimedOut(function(self)
|
||||||
|
if not TheSecondTry then
|
||||||
|
print("Loop timed out!",self.Type,"Trying again...")
|
||||||
|
self:ResetTime(2)
|
||||||
|
self:Resume()
|
||||||
|
TheSecondTry=true
|
||||||
|
else
|
||||||
|
print("We just couldn't do it!") -- print if we don't get anything working
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
multi:mainloop()
|
||||||
|
```
|
||||||
|
# Output (Change the value inc as indicated in the comment to see the outcomes!)
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Loop timed out! tloop Trying again...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
Looping...</br>
|
||||||
|
We did it! 1 2 3</br>
|
||||||
|
|
||||||
# TODO (In order of importance)
|
# TODO (In order of importance)
|
||||||
- Write the wiki stuff</br>
|
- Write the wiki stuff. (There are a lot of undocumented, but useful features that make automation of spawning a lot of different multiObject types eaiser, etc... To do really soon!)</br>
|
||||||
- Test for unknown bugs</br>
|
- Test for unknown bugs</br>
|
||||||
|
|||||||
@ -22,7 +22,7 @@ function print(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
multi = {}
|
multi = {}
|
||||||
multi.Version={'A',4,1}
|
multi.Version={1,5,0}
|
||||||
multi.stage='stable'
|
multi.stage='stable'
|
||||||
multi.__index = multi
|
multi.__index = multi
|
||||||
multi.Mainloop={}
|
multi.Mainloop={}
|
||||||
@ -208,6 +208,9 @@ end
|
|||||||
function multi:getChildren()
|
function multi:getChildren()
|
||||||
return self.Mainloop
|
return self.Mainloop
|
||||||
end
|
end
|
||||||
|
function multi:getVersion()
|
||||||
|
return multi.Version[1].."."..multi.Version[2].."."multi.Version[3]
|
||||||
|
end
|
||||||
--Processor
|
--Processor
|
||||||
function multi:getError()
|
function multi:getError()
|
||||||
if self.error then
|
if self.error then
|
||||||
@ -459,6 +462,42 @@ end
|
|||||||
function multi:Sleep(n)
|
function multi:Sleep(n)
|
||||||
self:hold(n)
|
self:hold(n)
|
||||||
end
|
end
|
||||||
|
-- Advance Timer stuff
|
||||||
|
function multi:SetTime(n)
|
||||||
|
if not n then n=3 end
|
||||||
|
local c=multi:newBase()
|
||||||
|
c.Type='timemaster'
|
||||||
|
c.timer=multi:newTimer()
|
||||||
|
c.timer:Start()
|
||||||
|
c.set=n
|
||||||
|
c.link=self
|
||||||
|
self._timer=c.timer
|
||||||
|
function c:Act()
|
||||||
|
if self.timer:Get()>=self.set then
|
||||||
|
self.link:Pause()
|
||||||
|
for i=1,#self.link.funcTM do
|
||||||
|
self.link.funcTM[i](self.link)
|
||||||
|
end
|
||||||
|
self:Destroy()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
multi.ResetTime=multi.SetTime
|
||||||
|
function multi:ResolveTimer(...)
|
||||||
|
self._timer:Pause()
|
||||||
|
for i=1,#self.funcTMR do
|
||||||
|
self.funcTMR[i](self,...)
|
||||||
|
end
|
||||||
|
self:Pause()
|
||||||
|
end
|
||||||
|
function multi:OnTimedOut(func)
|
||||||
|
self.funcTM[#self.funcTM+1]=func
|
||||||
|
end
|
||||||
|
function multi:OnTimerResolved(func)
|
||||||
|
self.funcTMR[#self.funcTMR+1]=func
|
||||||
|
end
|
||||||
|
-- Timer stuff done
|
||||||
function multi:Pause()
|
function multi:Pause()
|
||||||
if self.Type=='mainprocess' then
|
if self.Type=='mainprocess' then
|
||||||
print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()")
|
print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()")
|
||||||
@ -583,6 +622,8 @@ function multi:newBase(ins)
|
|||||||
end
|
end
|
||||||
c.Active=true
|
c.Active=true
|
||||||
c.func={}
|
c.func={}
|
||||||
|
c.funcTM={}
|
||||||
|
c.funcTMR={}
|
||||||
c.ender={}
|
c.ender={}
|
||||||
c.Id=0
|
c.Id=0
|
||||||
c.PId=0
|
c.PId=0
|
||||||
|
|||||||
27
test.lua
Normal file
27
test.lua
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package.path="?/init.lua;"..package.path
|
||||||
|
require("multi.tloop")
|
||||||
|
a=0
|
||||||
|
inc=1 -- change to 0 to see it not met at all, 1 if you want to see the first condition not met but the second and 2 if you want to see it meet the condition on the first go.
|
||||||
|
loop=multi:newTLoop(function(self)
|
||||||
|
print("Looping...")
|
||||||
|
a=a+inc
|
||||||
|
if a==14 then
|
||||||
|
self:ResolveTimer("1","2","3") -- ... any number of arguments can be passed to the resolve handler
|
||||||
|
-- this will also automatically pause the object that it is binded to
|
||||||
|
end
|
||||||
|
end,.1)
|
||||||
|
loop:SetTime(1)
|
||||||
|
loop:OnTimerResolved(function(self,a,b,c) -- the handler will return the self and the passed arguments
|
||||||
|
print("We did it!",a,b,c)
|
||||||
|
end)
|
||||||
|
loop:OnTimedOut(function(self)
|
||||||
|
if not TheSecondTry then
|
||||||
|
print("Loop timed out!",self.Type,"Trying again...")
|
||||||
|
self:ResetTime(2)
|
||||||
|
self:Resume()
|
||||||
|
TheSecondTry=true
|
||||||
|
else
|
||||||
|
print("We just couldn't do it!") -- print if we don't get anything working
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
multi:mainloop()
|
||||||
Loading…
x
Reference in New Issue
Block a user