Calling a function with waits in it

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Hi,
my problem is that I'm calling a function that uses waits in a loop, thus the loop waits until the function's waits are over. How can I get the loop to immediately continue?
That's the code, MSRL_Actions is the function with the loop, FireRocketLauncher with the waits.
JASS:

function FireRocketLauncher takes unit u, integer lvl, location spellloc returns nothing //which unit fires, level of rocket, target
    local player my_player = GetOwningPlayer( u)
    local location my_unitloc = GetUnitLoc( u)
    local unit my_dummy = CreateUnitAtLoc(my_player, 'h00K', my_unitloc , 270.00)
    
    call IssuePointOrderLoc( my_dummy, "attackground", spellloc )
    call SetUnitAbilityLevel(my_dummy, 'A00T', lvl)
    call PolledWait( ( DistanceBetweenPoints(my_unitloc, spellloc) / 1400.00 ) * 1.1 ) //That's the wait -.-
    call IssuePointOrderLoc( my_dummy, "blizzard", spellloc )
    call DestroyEffect(AddSpecialEffectLoc("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", spellloc))
    call UnitDamagePoint( u, 0, 140 + (lvl * 10), GetLocationX(spellloc), GetLocationY(spellloc), 230 + (20 * lvl), true, true, ATTACK_TYPE_MELEE, DAMAGE_TYPE_DEMOLITION, WEAPON_TYPE_WHOKNOWS)
    call AttachSoundToUnit(gg_snd_GrenadeExplosion, my_dummy)
    call SetSoundVolume(gg_snd_GrenadeExplosion, 200)
    call StartSound(gg_snd_GrenadeExplosion)
    call UnitApplyTimedLife(my_dummy, 'BTLF', 2.00)
    call RemoveUnit(my_dummy)
    set my_dummy = null
    set my_unitloc = null
    set my_player = null
endfunction

function MSRL_Actions takes nothing returns nothing
    local unit my_unit = GetTriggerUnit()
    local integer my_integer = 5
    local location my_targloc 
    local location my_location = GetSpellTargetLoc()

    loop
        exitwhen my_integer == 0
        set my_targloc = GetRandomPointInRange( GetLocationX(my_location), GetLocationY(my_location), 250)
        call FireRocketLauncher(my_unit, 3, my_targloc)
        set my_integer = my_integer - 1
    endloop
    set my_unit = null
    set my_targloc = null
endfunction

BTW that wait is needed because without it the explosion would occur when the rocket is still in the air´.
 

The_Kingpin

Member (Who are you and why should I care?)
Reaction score
41
You can't unless you start a new thread with ExecuteFunc() or you do something with timers.

P.S. You also didn't RemoveLocation() your locations and some are not set = null ed.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Yes The_Kingpin is right. Use struct to trasfer the arguements and use timers or ExecuteFunc.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Well, I'm pretty much new to structs and stuff like this. I've read DuckieKing's tutorial but it's still a bit too much for me. :eek:
Could you explain me in short what I have to put into the struct?
I don't want the complete trigger, just a little help :)
 

0zaru

Learning vJASS ;)
Reaction score
60
try TriggerSleepAction instead of polledwait, anyway it isn't very accurate do that.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
triggersleep has the same effect than polledwait has.
 

0zaru

Learning vJASS ;)
Reaction score
60
all though polledwait uses a timer instead and a loop to determine when ends:

JASS:
function PolledWait takes real duration returns nothing
    local timer t
    local real  timeRemaining

    if (duration > 0) then
        set t = CreateTimer()
        call TimerStart(t, duration, false, null)
        loop
            set timeRemaining = TimerGetRemaining(t)
            exitwhen timeRemaining <= 0

            // If we have a bit of time left, skip past 10% of the remaining
            // duration instead of checking every interval, to minimize the
            // polling on long waits.
            if (timeRemaining > bj_POLLED_WAIT_SKIP_THRESHOLD) then
                call TriggerSleepAction(0.1 * timeRemaining)
            else
                call TriggerSleepAction(bj_POLLED_WAIT_INTERVAL)
            endif
        endloop
        call DestroyTimer(t)
    endif
endfunction


try at least =P may work or not it's a suggestion. Try it at least =P
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
PolledWait is more accurate than TriggerSleepAction, but PolledWait leaks.

You'll have to put in the struct simply all the arguements you want to pass to the function, and then find a way to trasfer the struct to the trigger, perhaps by using globals, gamecache or handlevars.
 

SFilip

Gone but not forgotten
Reaction score
634
> PolledWait is more accurate than TriggerSleepAction
Not quite - the primary purpose of PolledWait is that it's not affected by lag while TriggerSleepAction is. In other words, if you run a one minute TriggerSleepAction for example and someone starts lagging, the game will not pause this wait, it will still keep going while PolledWait would stop and continue once the lag is over.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
... Thus PolledWait is more accurate becuase it will continue working even with problems such as lags. I didn't put it clearly enough it seems.
 

Arkan

Nobody rides for free
Reaction score
92
Use ExecuteFunc(), or if you have NewGen, you can use functionname.execute(argument1, argument2) to pass aguments, very handy.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Ok, it took my the whole evening to figure out how to work with structs :p
Well, now my ability works fine except of the nasty message "double free of location in FireRocketLauncher". That's my code so far.
JASS:

struct RocketBarrage //Used for Rocket Launcher and MSRL
    unit caster //casting unit
    location spellloc //targeted loc
    location unitloc //caster's loc
    integer level //level of rocket launcher ability
endstruct

function FireRocketLauncher takes nothing returns nothing
    local RocketBarrage Data=1
    local player my_player = GetOwningPlayer( Data.caster)
    local location my_unitloc = GetUnitLoc( Data.caster)
    local location my_spellloc = Data.spellloc
    local integer my_lvl = Data.level
    local unit my_dummy = CreateUnitAtLoc(my_player, 'h00K', my_unitloc , 270.00)
    
    call IssuePointOrderLoc( my_dummy, "attackground", my_spellloc )
    call SetUnitAbilityLevel(my_dummy, 'A00T', my_lvl)
    call PolledWait( ( DistanceBetweenPoints(my_unitloc, my_spellloc) / 1400.00 ) * 1.1 )
    call IssuePointOrderLoc( my_dummy, "blizzard", my_spellloc )
    call DestroyEffect(AddSpecialEffectLoc("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", my_spellloc))
    call UnitDamagePoint( Data.caster, 0, 140 + (my_lvl * 10), GetLocationX(my_spellloc), GetLocationY(my_spellloc), 230 + (20 * my_lvl), true, true, ATTACK_TYPE_MELEE, DAMAGE_TYPE_DEMOLITION, WEAPON_TYPE_WHOKNOWS)
    call AttachSoundToUnit(gg_snd_GrenadeExplosion, my_dummy)
    call SetSoundVolume(gg_snd_GrenadeExplosion, 200)
    call StartSound(gg_snd_GrenadeExplosion)
    call UnitApplyTimedLife(my_dummy, 'BTLF', 1.00)
    
    set my_dummy = null
    call RemoveLocation(my_unitloc)
    call RemoveLocation(my_spellloc)
    set my_unitloc = null
    set my_spellloc = null
    set my_player = null
endfunction

function MSRL_Actions takes nothing returns nothing
    local unit my_unit = GetTriggerUnit()
    local integer my_integer = 5
    local location my_targloc 
    local location my_spellloc = GetSpellTargetLoc()
    local RocketBarrage Data = RocketBarrage.create()
    set Data.unitloc = GetUnitLoc(my_unit)
    set Data.level = GetUnitAbilityLevel( my_unit, 'S001')
    set Data.caster = my_unit

    loop
        exitwhen my_integer == 0
        set my_targloc = GetRandomPointInRange( GetLocationX(my_spellloc), GetLocationY(my_spellloc), 250)
        set Data.spellloc = my_targloc

        call SetUnitUserData(my_unit, 1)
        call ExecuteFunc("FireRocketLauncher")
        call PolledWait(0.1) //tried with and without this wait
        set my_integer = my_integer - 1
    endloop
    set my_unit = null
    call RemoveLocation(my_targloc)
    call RemoveLocation(my_spellloc)
    set my_targloc = null
    set my_spellloc = null
    call Data.destroy()
endfunction
 

Vexorian

Why no custom sig?
Reaction score
187
Ok, it took my the whole evening to figure out how to work with structs
You kind of have wasted your time...

In your case, you didn't need structs you only needed this:

JASS:
function FireRocketLauncher takes unit u, integer lvl, location spellloc returns nothing //which unit fires, level of rocket, target
    local player my_player = GetOwningPlayer( u)
    local location my_unitloc = GetUnitLoc( u)
    local unit my_dummy = CreateUnitAtLoc(my_player, 'h00K', my_unitloc , 270.00)
    
    call IssuePointOrderLoc( my_dummy, "attackground", spellloc )
    call SetUnitAbilityLevel(my_dummy, 'A00T', lvl)
    call PolledWait( ( DistanceBetweenPoints(my_unitloc, spellloc) / 1400.00 ) * 1.1 ) //That's the wait -.-
    call IssuePointOrderLoc( my_dummy, "blizzard", spellloc )
    call DestroyEffect(AddSpecialEffectLoc("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", spellloc))
    call UnitDamagePoint( u, 0, 140 + (lvl * 10), GetLocationX(spellloc), GetLocationY(spellloc), 230 + (20 * lvl), true, true, ATTACK_TYPE_MELEE, DAMAGE_TYPE_DEMOLITION, WEAPON_TYPE_WHOKNOWS)
    call AttachSoundToUnit(gg_snd_GrenadeExplosion, my_dummy)
    call SetSoundVolume(gg_snd_GrenadeExplosion, 200)
    call StartSound(gg_snd_GrenadeExplosion)
    call UnitApplyTimedLife(my_dummy, 'BTLF', 2.00)
    call RemoveUnit(my_dummy)
    set my_dummy = null
    set my_unitloc = null
    set my_player = null
endfunction

function MSRL_Actions takes nothing returns nothing
    local unit my_unit = GetTriggerUnit()
    local integer my_integer = 5
    local location my_targloc 
    local location my_location = GetSpellTargetLoc()

    loop
        exitwhen my_integer == 0
        set my_targloc = GetRandomPointInRange( GetLocationX(my_location), GetLocationY(my_location), 250)
        call FireRocketLauncher.execute(my_unit, 3, my_targloc)
        set my_integer = my_integer - 1
    endloop
    set my_unit = null
    set my_targloc = null
endfunction


Your code with structs is just TERRIBLY wrong. Try learning structs another day, one of the times you actually need structs to solve your problem.
 

The_Kingpin

Member (Who are you and why should I care?)
Reaction score
41
The way you are using your structs will work, but if two units cast it while the effect is already running... well... then you've got problems...
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top