package TimedLoop // TimedLoop // originally created by Vexorian // augmented by some ideas from "Constant Timer Loop 32" by Nestharus timer timedLoopTimer = CreateTimer() trigger timedLoopTrig = CreateTrigger() int conditionCount = 0 function evaluateTimedLoopTrig() TriggerEvaluate(timedLoopTrig) if conditionCount == 0 timedLoopTimer.pause() enum Mode RUNNING PAUSED STOPPED STOPPED_FOR_GOOD public module TimedLoop static private thistype array instances // The array static private integer instanceCount = 0 // The count static private triggercondition triggerCond = null // the triggerCondition for this static private conditionfunc cond = Condition(function onExpire) // TODO this should be private but does not work currently private Mode mode = Mode.PAUSED abstract function onTimedLoop() private static function onExpire() returns boolean int i = instanceCount - 1 while i >= 0 thistype t = instances[i] t.onTimedLoop() if t.mode != Mode.RUNNING instanceCount-- instances[i] = instances[instanceCount] if t.mode == Mode.STOPPED_FOR_GOOD t.mode = Mode.PAUSED destroy t else t.mode = Mode.PAUSED i-- if instanceCount == 0 TriggerRemoveCondition(timedLoopTrig, triggerCond) triggerCond = null conditionCount-- return false function stopTimedLoop() mode = Mode.STOPPED function stopTimedLoopAndDestroy() mode = Mode.STOPPED_FOR_GOOD function startTimedLoop() instances[instanceCount] = this instanceCount++ if instanceCount == 1 if triggerCond == null triggerCond = TriggerAddCondition(timedLoopTrig, cond) conditionCount++ if conditionCount == 1 timedLoopTimer.startPeriodic(ANIMATION_PERIOD, function evaluateTimedLoopTrig) mode = Mode.RUNNING ondestroy if mode != Mode.PAUSED stopTimedLoop() printWarning("Destroyed Instance using TimedLoop before stopping the Loop")