| 09-13-2008, 05:56 AM | #1 |
Not the best of topic titles, but... Could I have comments on this system I made? Link. If it's okay, if I can improve anything. It basically handles everything timer-related in the map with a single timer, allows any period for functions to be executed, and supports data-passing. In that sense, it's kinda like TT, but with different features, methods, and usage. JASS:library WaitSystem initializer Init //*************************************************************************** //* Wait System v1.1, by Darius34 //* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ //* [url]http://www.thehelper.net/forums/showthread.php?p=847782[/url] //* //* Essentially a way to rectify having to create and maintain multiple //* timers, replacing and running everything with a single one. Supports //* data passing, and allows any period. //* //* Usage/Syntax //* ¯¯¯¯¯¯¯¯¯¯¯¯ //* Wait(duration, WS.actionfunc, data) //* //* - actionfunc, the user-defined function, has to be called with //* the "WS." prefix. //* - actionfunc also has to take an integer (the data struct) and return //* a boolean. Returning true continues the periodic execution of the //* function, while returning false halts it. //* //* Notes //* ¯¯¯¯¯ //* - The maxmimum accurate wait time/period you can have with this //* system is given by TIMER_PERIOD * ITERATION_LIMIT. It's 1000 //* seconds by default. //* - The period can be increased to the minimum period in the map. //* - The 'wait' used here isn't like normal wait; it doesn't pause //* execution. It runs like a timer would, in a separate thread. //* //* Credits //* ¯¯¯¯¯¯¯ //* - The people who made vJass possible. //* - Cohadar, for the concept behind TT, which I adapted some. //* //*************************************************************************** function interface WS takes integer DataStruct returns boolean globals // Configuration private constant real TIMER_PERIOD = 0.01 // Yeah, timer period. Can be increased to the minimum period in the map. private constant integer ITERATION_LIMIT = 100000 // Iteration limit. Just has to be large enough so that the timer doesn't loop // through and execute stuff a few cycles early. // -- System starts here. -- private timer Timer private integer IterationCount = 0 // Increments as the timer executes, resets when > ITERATION_LIMIT. private WS array ActionFunction // Wait data. private integer array RunCount private integer array DataStruct private real array Duration private integer InstanceIndex = 0 // Index to be used for the next instance of execution. private integer array Stack // Contains recycled indices. private integer StackSize = 0 endglobals function Wait takes real duration, WS actionfunc, integer data returns nothing local integer actioncount = R2I(duration/TIMER_PERIOD + IterationCount) local integer index // Determines count at which actions will be executed. if actioncount >= ITERATION_LIMIT then set actioncount = actioncount - ITERATION_LIMIT endif debug call BJDebugMsg(" Action Count: " + I2S(actioncount) + " | Iteration Count: " + I2S(IterationCount)) // Recycles a freed index, if any, or allocates a new index. if StackSize > 0 then set StackSize = StackSize - 1 set index = Stack[StackSize] else set index = InstanceIndex set InstanceIndex = InstanceIndex + 1 endif debug call BJDebugMsg("Instance Index: " I2S(index)) // Stores wait data for a subsequent, periodic wait, if applicable. set ActionFunction[index] = actionfunc set RunCount[index] = actioncount set DataStruct[index] = data set Duration[index] = duration endfunction private function HandleIterations takes nothing returns nothing local integer a = 0 // Increments running count, resets it if necessary. set IterationCount = IterationCount + 1 if IterationCount > ITERATION_LIMIT then set IterationCount = 0 endif // Loops through arrays and checks counts, to see if any actions should be executed. loop exitwhen a > InstanceIndex if RunCount[a] == IterationCount and ActionFunction[a] != null then if ActionFunction[a].evaluate(DataStruct[a]) then // Uses the return value of the user-defined function call Wait(Duration[a], ActionFunction[a], DataStruct[a]) // to determine if periodic execution continues. endif set Stack[StackSize] = a // Index recycling. set StackSize = StackSize + 1 endif set a = a + 1 endloop endfunction private function Init takes nothing returns nothing set Timer = CreateTimer() call TimerStart(Timer, TIMER_PERIOD, true, function HandleIterations) endfunction endlibrary Edit: Code. |
| 09-16-2008, 06:12 AM | #2 |
Bump. This was on the second page already. Another thing: is it true that desyncs can be caused by true/false (can't remember which) returns in timer callbacks? |
| 09-18-2008, 02:11 AM | #3 | |
hmnn, I dunno perhaps actually posting the code would help. Quote:
|
| 09-18-2008, 05:49 AM | #4 |
im not sure but i think you pass data by passing the calling function's struct itself? if i were to use this system, should ALL my struct's be the same/analogous? |
| 09-18-2008, 11:22 AM | #5 | ||||
Quote:
Quote:
Quote:
Quote:
|
| 09-18-2008, 12:02 PM | #6 | |
Quote:
It can be caused by them not being there where they should be - conditions, boolexprs, etc., since they seem to be evaluated differently in the mac version to the PC. |
| 09-19-2008, 04:42 PM | #7 | |
Quote:
Is this issue Mac-specific? |
| 09-20-2008, 02:27 AM | #8 | |
Quote:
JASS:function foo takes nothing returns boolean call BJDebugMsg("Blah") endfunction |
| 09-20-2008, 06:21 AM | #9 |
Okay, thanks for the information. Any comments on the system now? :P |
