JASS:
//Dual Timer System v2.0
//
//Functions:
//
//TimedFunc(real time, integer struct, code func)
//
//*Executes code in a periodic manner, will not stop until the function returns true
//*If you want the code to stop in one-shot, simply put 'return true' at the bottom of the function <------------HOW TO USE
//
//Data()
//
//*Used in your functions that going to be called (func parameter), gets the struct you set when you called TimedFunc()
//
//Notes:
//
//Slower than using the single timer system
//
//======================================================================================================================
//
//Single Timer System v1.0
//
//~Documentation for the second half of the system which has been added~
//
//Functions:
//
//PeriodFunc(integer struct, code func)
//
//*A simple function that takes just a function and a struct, and it will execute it
//every .01 seconds, it will only stop executing when the function returns true <---------------HOW TO USE
//
//FastData()
//
//*Used in your functions, gets the struct you set when you called PeriodFunc()
//
//Notes:
//
//A high-speed method to call periodic functions, but at the cost
//of a changeable interval to call functions
//
//You can change the interval from .01 if you desire, the constant function 'Interval' in the SingleTimerSystem
//library is the number you can change
//
//This system can be very useful for other systems, such as knockback and physics, whereas the other timer system really
//shouldn't be used for very small repeating timeouts, although you can if you really want to
//
//*Faster than DTS
//
//======================================================================================================================
//
//Preloading:
//
//Both of these systems use preloading in order to speed up the systems in-game, which means it will automatically
//load a certain number of resources (currently set at 32) so that # of timed functions can be running at the same time
//without causing any problems. If you happen to exceed that limit with your map, this will not cause any serious problems,
//just a slow-down of the code due to in-game loading of necessary resources
//
//*The STS (SingleTimerSystem)'s Preload amount is only set at 16, because there really shouldn't be a hell of alot
//of functions running 100 times a second
//
//The Preload amounts can be set with the 'STSPreloadAmount' constant function, and the 'DTSPreloadAmount' constant function
//
//If your like many mappers(and gamers) out there, your going to want as little in-game preloading causing lag in your game
//I have provided some debug code to allow you to find the highest number of timedfunctions used during your game at any
//point (at the same time)
//
//In order to do so, simply enable the debug mode using JNGP, and test your map out online
//Obviously, your map will need to be finished
//
//In the global blocks at the beginning of each system, there is an integer 'N' which is used for debugging purposes
//If you have finished finding the appropriate Preload amount, you should remove that 'N'
//
//Please Note: Not having a high enough number of preloaded resources will not be enough to cause significant lag
//in-game. If your too lazy to set your Preload number accordingly to your map, this will not 'kill' your map, it will
//just cause a small amount of temporary lag that could've been avoided
library SingleTimerSystem initializer STSPreLoad
globals
private timer T = CreateTimer()
prviate integer array Struct
private integer Data
private trigger array Function
private trigger Swap
//
private integer N = 0 //This should be removed after you find the appropriate Preload amount
endglobals
private constant function STSPreloadAmount takes nothing returns integer
return 16
endfunction
private constant function Interval takes nothing returns real
return .01
endfunction
private function Working takes nothing returns nothing
local integer i = 0
loop
exitwhen i == Max
set Data = Struct[i]
if TriggerEvaluate(Function[i]) then
set Max = Max - 1
set Swap = Function[i]
set Function[i] = Function[Max]
set Function[Max] = Swap
set Struct[i] = Struct[Max]
set i = i - 1
if Max == 0 then
call PauseTimer(T)
return
endif
endif
set i = i + 1
endloop
endfunction
function PeriodFunc takes integer struct, code func returns nothing
if Function[Max] == null then
set Function[Max] = CreateTrigger()
debug set N = N + 1
debug call BJDebugMsg(" STSPRELOAD LIMIT REACHED, CURRENT RESOURCES: " + I2S(N+STSPreloadAmount()))
endif
call TriggerClearConditions(Function[Max])
call TriggerAddCondition(Function[Max], Condition(func))
set Struct[Max] = struct
set Max = Max + 1
if Max == 1 then
call TimerStart(T, Interval(), true, function Working)
endif
endfunction
constant function FastData takes nothing returns integer
return Data
endfunction
function STSPreLoad takes nothing returns nothing
local integer i = 0
loop
exitwhen i > STSPreloadAmount()
set Function[i] = CreateTrigger()
set i = i + 1
endloop
endfunction
endlibrary
library DualTimerSystem initializer DTSPreLoad
globals
private timer T = CreateTimer()
private real array Working
private real array TimeOut
private real TimerTimeOut = 0.0
private integer array Struct
private integer Data
private trigger array Function
private trigger Swap
//
private integer N = 0 //This should be removed after you find the appropriate Preload amount
endglobals
private constant function DTSPreloadAmount takes nothing returns integer
return 32
endfunction
private constant function MaxTimeOut takes nothing returns real
return 20000.0
endfunction
//This is a constant function for the largest number of seconds you can put into one
//timed function
//This number isn't too important, its just how the system is set-up
//If you feel you need more than 5 hours in a single timer, you can change this number as you please
//Although, for very long timeouts, I do suggest using QUICK_TIMER()
private function Working takes nothing returns nothing
local integer i = 0
local real newtimeout = MaxTimeOut()
loop
exitwhen i == Max
set Working[i] = Working[i] - TimerTimeOut
if Working[i] == 0 then
set Working[i] = TimeOut[i]
set Data = Struct[i]
if TriggerEvaluate(Function[i]) then
set Max = Max - 1
set Working[i] = Working[Max]
set TimeOut[i] = TimeOut[Max]
set Struct[i] = Struct[Max]
set Swap = Function[i]
set Function[i] = Function[Max]
set Function[Max] = Swap
set i = i - 1
endif
else
if Working[i] < newtimeout then
set newtimeout = Working[i]
endif
endif
set i = i + 1
endloop
set TimerTimeOut = newtimeout
call TimerStart(T, newtimeout, false, function Working)
if Max == 0 then
call PauseTimer(T)
endif
endfunction
function TimedFunc takes real time, integer struct, code func returns nothing
local integer i = 0
local real TimeElapsed = TimerGetElapsed(T)
call PauseTimer(T)
set TimerTimeOut = time
loop
exitwhen i == Max
set Working[i] = Working[i] - TimeElapsed
if Working[i] < TimerTimeOut then
set TimerTimeOut = Working[i]
endif
set i = i + 1
endloop
if Function[Max] == null then
set Function[Max] = CreateTrigger()
debug set N = N + 1
debug call BJDebugMsg("DTSPRELOAD LIMIT REACHED, CURRENT RESOURCES: " + I2S(N+STSPreloadAmount()))
endif
call TriggerClearConditions(Function[Max])
call TriggerAddCondition(Function[Max], Condition(func))
set Struct[Max] = struct
set Working[Max] = time
set TimeOut[Max] = time
call TimerStart(T, TimerTimeOut, false, function Working)
set Max = Max + 1
endfunction
constant function Data takes nothing returns integer
return Data
endfunction
function DTSPreLoad takes nothing returns nothing
local integer i = 0
loop
exitwhen i > DTSPreloadAmount()
set Function[i] = CreateTrigger()
set i = i + 1
endloop
endfunction
endlibrary
Last edited: