HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Problem with Polled Loop

02-28-2010, 04:53 PM#1
sPyRaLz
To whom it may concern,

Edit #1: Apparently, after further testing, nothing after my call PolledWait() works.. the function just stops and exits.

Im trying to code a function that creates a forked lightning type effect.
Heres a snippet of the function. Minus all the bits that work.

Collapse JASS:
g = enumerated units
set l1 = GetUnitLoc(caster)

        loop
            set f = FirstOfGroup(g)
            exitwhen f == null
            set l2 = GetUnitLoc(f)
            

            set lex[index] = AddLightningEx("CLPB", true, GetLocationX(l1), GetLocationY(l1), GetLocationZ(l1), GetLocationX(l2), GetLocationY(l2), GetLocationZ(l2))
            set index = index + 1
            call GroupRemoveUnit(g, f)
        endloop


[b]//Remove Lightning    

        //call PolledWait(0.01)

        //call TriggerSleepAction(0.05)

call TimerStart(t, 0.05, false, null) 
loop
exitwhen TimerGetRemaining(t) <= 0
endloop   [/b]

set t = null    
        
call DisplayTimedTextToPlayer(Player(0),0,0,60,"index2: " + I2S(index2)) 
        loop
            call DisplayTimedTextToPlayer(Player(0),0,0,60,"removing")   
            call DestroyLightning(lex[index2]) 
            set lex[index2] = null
            set index2 = index2 + 1
            exitwhen index == index2
        endloop
call DisplayTimedTextToPlayer(Player(0),0,0,60,"index2: " + I2S(index2))     
endfunction

Unfortunately, When i include any kind of wait, polled, timer or triggersleepaction
the following loop does not run at all. I've tested this as it works fine when i remove the waits.


Collapse JASS:
        loop
            call DisplayTimedTextToPlayer(Player(0),0,0,60,"removing")   
            call DestroyLightning(lex[index2]) 
            set lex[index2] = null
            set index2 = index2 + 1
            exitwhen index == index2
        endloop

Has anyone ever come across a similar problem? Been stuck searching the forums for answers but i can't find the one i need..
02-28-2010, 05:25 PM#2
Ammorth
Collapse JASS:
call TimerStart(t, 0.05, false, null) 
loop
exitwhen TimerGetRemaining(t) <= 0
endloop

will not work. Jass code is executed in blocks during the frames. Therefore that loop is an infinite loop as the conditions can never be met. This will cause the thread to terminate after some time.

Aswell, using TriggerSleepAction() or PolledWait() within a function that was called with a timer, will cause the thread to terminate as well.

The usual approach is to create a struct with all the variables you wish to store and create a timer to call a callback function. You then attach the struct with all the data you want in the callback function, to the timer. In the callback function you then get the expired timer and retrieve the struct attached to the timer. You then have all the data you wanted to store.
03-01-2010, 03:32 AM#3
sPyRaLz
Hmm. I get the idea. Is there any restriction to where I declare the template for my struct? I came across a tutorial which says its global. Does that mean I can declare it anywhere I like?

http://www.thehelper.net/forums/showthread.php?t=79426


PS: Promise ill give you a rep+ once i get this working =) Thanks alot for the advice you've given me so far Ammorth.
03-01-2010, 05:25 AM#4
Ammorth
I'd declare it above where you are planning to use it. There are a few limitations to struct placement (involving public/private) but most of them you can get away with using keywords.

As for a means to attach stuff to timers, TimerUtils, in the resource section is a good method. It will also stop you from running into a possible corrupt handle stack by nulling timers.

As for rep, you can't actually give any rep if you have 0 rep, but I appreciate the gesture.