• 🏆 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!

[JASS] "Destroying" Indexers

Status
Not open for further replies.
Level 11
Joined
Apr 6, 2008
Messages
760
I want to destroy a struct which is saved in a indexer, but i have 2 places it can get destroyed. 1. Inside the loop and 2. from a method.


JASS:
globals
     private timer Timer = CreateTimer()
     private ASDF array A
     private integer Index = 0
endglobals

struct ASDF

integer INDEX

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()

        //struff
        set this.INDEX = Index        

        if Index == 0 then
             call TimerStart(//blaH)
        endif
        

        set A[Index] = this
        set Index = Index + 1
    endmethod

    method Destroy takes nothing returns nothing
          //Here is the question, will this break the indexer?
          //Is there are better way, then how?
          call this.destroy()
 
          set Index = Index - 1
          set A[this.INDEX] = A[Index]

          if Index == 0 then
               call PauseTimer(Timer)
          endif
    endmethod

endstruct
 
Level 8
Joined
Aug 4, 2006
Messages
357
The best way to do this is using the onDestroy instance method, which executes right before the instance is destroyed. Simply call .destroy() and the indexer will do its stuff as well as recycle the struct index. Here you go:
JASS:
globals
     private timer Timer = CreateTimer()
     private ASDF array A
     private integer Index = 0
endglobals

struct ASDF

    integer INDEX

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()

        //struff
        set this.INDEX = Index

        if Index == 0 then
             call TimerStart(//blaH)
        endif


        set A[Index] = this
        set Index = Index + 1
        return this
    endmethod

    method onDestroy takes nothing returns nothing
          set Index = Index - 1
          set A[this.INDEX] = A[Index]
          set A[this.INDEX].INDEX = this.INDEX

          if Index == 0 then
               call PauseTimer(Timer)
          endif
    endmethod

endstruct
 
Status
Not open for further replies.
Top