• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Which indexing method is better?

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi guys, please see the following example:
JASS:
//Are there any practical difference between these two allocation and deallocation methods? 
//Which one is better?

//First:
globals
    integer array a
    integer index = 0
endglobals

static method Loop takes nothing returns nothing
    local thistype this
    local integer i = 0          //<--------------
    loop
        exitwhen i > index
        set this = a[i]
        ......
        if RemainingTime <= 0 then
            set index = index - 1      //<--------------
            set a[i] = a[index]
            set i = i - 1
            ....
        else
            ....
        endif
        ......
        set i = i + 1
    endloop
endmethod

static method Run takes nothing returns nothing
    local thistype this = this.allocate
    ....
    set a[index] = this
    set index = index + 1       //<--------------
    if index == 1 then
        call TimerStart(...., function thistype.Loop)
    endif
    ....
endmethod


//Second:

globals
    integer array a
    integer index = 0
endglobals

static method Loop takes nothing returns nothing
    local thistype this
    local integer i = 1  // <-------------
    loop
        exitwhen i > index
        set this = a[i]
        ......
        if RemainingTime <= 0 then
            set a[i] = a[index]
            set index = index - 1   //<--------------
            set i = i - 1
            ....
        else
            ....
        endif
        ......
        set i = i + 1
    endloop
endmethod

static method Run takes nothing returns nothing
    local thistype this = this.allocate
    ....
    set index = index + 1    //<--------------
    set a[index] = this
    if index == 1 then
        call TimerStart(...., function thistype.Loop)
    endif
    ....
endmethod

Which one is better? Thanks.
 
Last edited:
Level 21
Joined
Mar 27, 2012
Messages
3,232
I'm having trouble reading this because of the unnecessary struct syntax, but the second way should work better judging by the initialization.
Also, you should never have to decrement the loop counter in JASS, because you can just choose to increase it at the appropriate times.
 
Status
Not open for further replies.
Top