• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[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