Trouble with IF statement

Zalinian

New Member
Reaction score
0
For some reason jass doesn't like multiple IF statements next to each other if they involve similar comparison types. I can't seem to get this working properly. Can anyone fix this or does anyone know of a better way to split a number's digits(ex: 1234 would become 1, 2 ,3 ,4 with each set to its own variable. This way I can measure ones, tens, hundreds, thousands, etc)

This my coding for a bonus system btw. Once the abilities are set up, simply call the function with the target unit, the number, and the bonus type (damage, armor, etc) and bam. Currently only damage is added in. It's also set to cap at 9999.

JASS:
//          Bonus System
function bonus_add takes unit target, integer bonus_num, string bonus_type returns nothing
    local integer num
    local integer thousand
    local integer hundred
    local integer ten
    local integer one
    local integer array skill
    if bonus_type == "damage" then
        set skill[1] = 'A00U'
        set skill[2] = 'A00V'
        set skill[3] = 'A00P'
        set skill[4] = 'A00I'
    endif
    if bonus_num < 10000 then
        set num = bonus_num
        if num >= 1000 then
            set thousand = num / 1000
            set num = num - (thousand * 1000)
        endif
        if num >= 100 then
            set hundred = num / 100
            set num = num - (hundred * 100)
        endif
        if num >= 10 then
            set ten = num / 10
            set num = num - (ten * 10)
        endif
        if num >= 1 then
            set one = num / 1
            set num = num - (one * 1)
        endif
    else
        set thousand = 9
        set hundred = 9
        set ten = 9
        set one = 9
    endif
    call BJDebugMsg(I2S(thousand))
    call BJDebugMsg(I2S(hundred))
    call BJDebugMsg(I2S(ten))
    call BJDebugMsg(I2S(one))
    if thousand > 0 then
        call UnitAddAbility(target, skill[4])
        call SetUnitAbilityLevel(target, skill[4], thousand)
        call UnitMakeAbilityPermanent(target, true, skill[4])
    endif
    if hundred > 0 then
        call UnitAddAbility(target, skill[3])
        call SetUnitAbilityLevel(target, skill[3], hundred)
        call UnitMakeAbilityPermanent(target, true, skill[3])
    endif
    if ten > 0 then
        call UnitAddAbility(target, skill[2])
        call SetUnitAbilityLevel(target, skill[2], ten)
        call UnitMakeAbilityPermanent(target, true, skill[2])
    endif
    if one > 0 then
        call UnitAddAbility(target, skill[1])
        call SetUnitAbilityLevel(target, skill[1], one)
        call UnitMakeAbilityPermanent(target, true, skill[1])
    endif
endfunction
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Looks fine to me... I don't see where it would error (but then again, it's late and I'm tired, and I may have missed something...)
 

ZugZugZealot

New Member
Reaction score
33
There's a shortcut you can use for that via substrings.

Something like this
JASS:
function bonus_add takes unit target, integer bonus_num, string bonus_type returns nothing
    local string skillParse = " " + I2S(bonus_num)
    local string tempChar
    local integer tempInteger
    local integer array skill
    local integer i
    
    if bonus_type == "damage" then
        set skill[1] = 'A00U'
        set skill[2] = 'A00V'
        set skill[3] = 'A00P'
        set skill[4] = 'A00I'
    endif
    
    if StringLength(skillParse) < 6 then
        set i = 4
        loop
            exitwhen i < 1
            set tempChar = SubString( skillParse, i, i + 1 )
            if tempChar != null then
                set tempInteger = S2I(tempChar)
                if tempInteger > 0 then
                    call UnitAddAbility(target, skill[5 - i])
                    call SetUnitAbilityLevel(target, skill[5 - i], tempInteger)
                    call UnitMakeAbilityPermanent(target, true, skill[5 - i])
                endif
            endif
            set i = i - 1
        endloop
    else
        set i = 1
        loop
            exitwhen i > 4
            call UnitAddAbility(target, skill<i>)
            call SetUnitAbilityLevel(target, skill<i>, 9)
            call UnitMakeAbilityPermanent(target, true, skill<i>)
            set i = i + 1
        endloop
    endif
    set tempChar = null
endfunction</i></i></i>
 

Zalinian

New Member
Reaction score
0
It saves fine, it just doesn't work correctly. It won't register a number unless its over 1000.
 

ZakkWylde-

New Member
Reaction score
14
I have no actual basis for this recommendation other than my logic/math intuition.

change the if statements so that one number does not (potentially) fall under all 4 if commands...I know it sounds stupid...but hey =D

i.e.

JASS:
if 10000 &gt; num &gt;= 1000 then
            set thousand = num / 1000
            set num = num - (thousand * 1000)
        endif
        if 1000 &gt; num &gt;= 100 then
            set hundred = num / 100
            set num = num - (hundred * 100)
        endif
        if 100 &gt; num &gt;= 10 then
            set ten = num / 10
            set num = num - (ten * 10)
        endif
        if 10 &gt; num &gt;= 1 then
            set one = num / 1
            set num = num - (one * 1)
        endif


Let me know if it works...I'd be interested to see...
(also, I'm not sure about writing inequalities in JASS...if you can do it the way I did...worst comes to worst, have [ljass]1000> num and num >=100[/ljass])

re-EDIT: Hrmm--ignore everything I wrote above...lemme try and find a way to do this for you (if you'd prefer a raw integer comparison of sorts to ZUGZUG's trigger...). I love a problem like this (YAY MATH!)
EDIT#2: PROBLEM SOLVED =D Below...
 

ZakkWylde-

New Member
Reaction score
14
JASS:
function Trig_Math_Actions takes nothing returns nothing
    local integer thousand
    local integer hundred
    local integer ten
    local integer one
    local integer random 
    set random = GetRandomInt(1, 12000)
    call BJDebugMsg(I2S(random))
    if random &lt; 10000 then
        set thousand = (random / 1000)
        set hundred = ((random - (thousand*1000)) / 100)
        set ten = (((random - (thousand*1000)) - (hundred*100)) / 10)
        set one = (((random - (thousand*1000)) - (hundred*100)) - (ten*10)) //if you want to be classy, 
        //you can divide all of it by 1 (to be consistent xD)
        call BJDebugMsg(I2S(thousand))
        call BJDebugMsg(I2S(hundred))
        call BJDebugMsg(I2S(ten))
        call BJDebugMsg(I2S(one))
    endif
endfunction

//===========================================================================
function InitTrig_Math takes nothing returns nothing
    set gg_trg_Math = CreateTrigger(  )
    call TriggerRegisterTimerEvent(gg_trg_Math, 3, true)
    call TriggerAddAction( gg_trg_Math, function Trig_Math_Actions )
endfunction


There you go...if you run the trigger...every 3 seconds you'll see (i'll give examples)

1450
1
4
5
0
----
9009
9
0
0
9
----
3076
3
0
7
6
----
10
0
0
1
0

Hopefully this is exactly what you wanted =D. If the parentheses really bother you, I'm sure there's a way you could remove a few...but I didn't bother--I went overboard with the parentheses to know exactly what was going first (even if order of operations tells me anyways).
 

Zalinian

New Member
Reaction score
0
Yea I was just thinking of the same setup. I was about to test it without any IF statements.

EDIT: Yea it works perfectly. thanks for the input.

Final Code:
JASS:
//          Bonus System
function bonus_add takes unit target, integer bonus_num, string bonus_type returns nothing
    local integer num
    local integer thousand
    local integer hundred
    local integer ten
    local integer one
    local integer array skill
    if bonus_type == &quot;damage&quot; then
        set skill[1] = &#039;A00U&#039;
        set skill[2] = &#039;A00V&#039;
        set skill[3] = &#039;A00P&#039;
        set skill[4] = &#039;A00I&#039;
    endif
    if bonus_num &lt; 10000 then
        set num = bonus_num
        set thousand = num / 1000
        set num = num - (thousand * 1000)
        set hundred = num / 100
        set num = num - (hundred * 100)
        set ten = num / 10
        set num = num - (ten * 10)
        set one = num / 1
        set num = num - (one * 1)
    else
        set thousand = 9
        set hundred = 9
        set ten = 9
        set one = 9
    endif
    //call BJDebugMsg(I2S(thousand))
    //call BJDebugMsg(I2S(hundred))
    //call BJDebugMsg(I2S(ten))
    //call BJDebugMsg(I2S(one))
    if thousand &gt; 0 then
        call UnitAddAbility(target, skill[4])
        call SetUnitAbilityLevel(target, skill[4], thousand)
        call UnitMakeAbilityPermanent(target, true, skill[4])
    endif
    if hundred &gt; 0 then
        call UnitAddAbility(target, skill[3])
        call SetUnitAbilityLevel(target, skill[3], hundred)
        call UnitMakeAbilityPermanent(target, true, skill[3])
    endif
    if ten &gt; 0 then
        call UnitAddAbility(target, skill[2])
        call SetUnitAbilityLevel(target, skill[2], ten)
        call UnitMakeAbilityPermanent(target, true, skill[2])
    endif
    if one &gt; 0 then
        call UnitAddAbility(target, skill[1])
        call SetUnitAbilityLevel(target, skill[1], one)
        call UnitMakeAbilityPermanent(target, true, skill[1])
    endif
endfunction

And the remove one:
JASS:
function bonus_remove takes unit target, string bonus_type returns nothing
    local integer array skill
    local integer start
    local integer end
    if bonus_type == &quot;damage&quot; then
        set skill[1] = &#039;A00U&#039;
        set skill[2] = &#039;A00V&#039;
        set skill[3] = &#039;A00P&#039;
        set skill[4] = &#039;A00I&#039;
    endif
    set start = 1
    loop
        exitwhen start &gt; end
        call UnitRemoveAbility(target, skill[start])
        set start = start + 1
    endloop
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top