HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Loop Problem

04-11-2006, 02:18 PM#1
Thunder_Eye
Collapse JASS:
function DoTMJ takes unit caster, unit target, real interval, integer intervals, real damage returns nothing
    local integer curinterval
    loop
        call TriggerSleepAction(interval)
        call UnitDamageTarget(caster, target, damage, true, true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_FIRE, WEAPON_TYPE_WHOKNOWS)
        set curinterval = curinterval + 1
        exitwhen curinterval == intervals
    endloop
    set caster = null
    set target = null
    set interval = 0.00
    set intervals = 0
    set damage = 0.00
    set curinterval = 0
endfunction

why doesnt this function work?
It damages the target one time, even though I set the "intervals" to 5.
04-11-2006, 02:29 PM#2
ProFeT
try to initialize the variable with a zero value

=> local integer curinterval = 0

;)

Edit: i don t think you have to set to zero the integer/real variables. Even for unit variable, just set to null when you destroy an object.
like :
Collapse JASS:
local effect eff
call DestroyEffect(eff)
set eff = null
04-11-2006, 02:39 PM#3
Thunder_Eye
Yeah I know, just did it anyway, think ive heard somewhere that it would help(could be wrong though).

I was thinking of setting cur to 0, but thought it would just take up space, will test it now.

EDIT: hah, it worked :S, cant see why but anyway it works. thx for the help.
04-11-2006, 05:08 PM#4
blu_da_noob
If you don't give an initial value to a variable, how does WC3 know what it should be? In some languages it uses the value currently stored in the location that that variable was allocated (which could be basically any number). In WC3, it just ends the thread when you try to use an undefined variable (ie. skips the rest of the actions).
04-11-2006, 05:12 PM#5
Thunder_Eye
Well If I remember right I havent had to initialize some times. I thought it were set to 0 when it gets declared.
04-11-2006, 05:18 PM#6
ixmike88
You wouldn't need to set it if it's a pointer or it's defined by another variable, otherwise (this applies for anything), set it to 0 or null.
04-11-2006, 05:20 PM#7
The)TideHunter(
You dont need to set any local varible to anything if you dont want to,
Collapse JASS:
local integer curinterval
Its a perfectly fine part of code, it is set to 0 by default.

Try this code and see how it goes
Collapse JASS:
function IsRealNegative takes real r returns boolean
    if(r < 0.00) then
        return true
    endif
    return false
endfunction

function DoTMJ takes unit caster, unit target, real interval, integer intervals, real damage returns nothing
    local integer curinterval
    local integer intervalTime = interval
    local integer intervalsD = intervals
    if(IsRealNegative(intervalTime) == true) then
        set intervalTime = 1
    endif
    if(IsRealNegative(intervalsD) == true) then
        set intervalsD = 5
    endif
    loop
        exitwhen curinterval == intervals
        call UnitDamageTarget(caster, target, damage, true, true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_FIRE, WEAPON_TYPE_WHOKNOWS)
        call TriggerSleepAction(interval)
        set curinterval = curinterval + 1
    endloop
    set intervalTime = 0
    set intervalsD = 0
    set curinterval = 0
endfunction

If this dosent work, tell me, il show u code i made with the same purpose, buty slightly different
04-11-2006, 05:23 PM#8
Thunder_Eye
The thing is that the TriggerSleep need to be before the UnitDamage.
04-11-2006, 05:32 PM#9
The)TideHunter(
It shouldent have to be, when it is first cast, it damages the unit, then waits, then again etc.
If you want to wait before the first damage, put it outside the loop like this

Collapse JASS:
function IsRealNegative takes real r returns boolean
    if(r < 0.00) then
        return true
    endif
    return false
endfunction

function DoTMJ takes unit caster, unit target, real interval, integer intervals, real damage returns nothing
    local integer curinterval
    local integer intervalTime = interval
    local integer intervalsD = intervals
    if(IsRealNegative(intervalTime) == true) then
        set intervalTime = 1
    endif
    if(IsRealNegative(intervalsD) == true) then
        set intervalsD = 5
    endif
    call TriggerSleepAction(interval)
    loop
        exitwhen curinterval == intervals
        call UnitDamageTarget(caster, target, damage, true, true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_FIRE, WEAPON_TYPE_WHOKNOWS)
        call TriggerSleepAction(interval)
        set curinterval = curinterval + 1
    endloop
    set intervalTime = 0
    set intervalsD = 0
    set curinterval = 0
endfunction
04-11-2006, 05:35 PM#10
Thunder_Eye
Hmm, I rather use the way Profet used and just initialize the variable as that worked.
04-11-2006, 07:19 PM#11
blu_da_noob
Quote:
Originally Posted by The)TideHunter(
You dont need to set any local varible to anything if you dont want to,
Collapse JASS:
local integer curinterval
Its a perfectly fine part of code, it is set to 0 by default.

That is outright wrong. Try not initializing a variable and then trying using it. Your thread will crash.