search all structs of the same type?

KeresPup

Member
Reaction score
5
Is there a way to search between all structs of the same type?

For example

JASS:
    struct myStruct
        integer id
        integer a
        string b
    endstruct
 
    myStruct array z
    myStruct y
    myStruct x


I would want to search all existing myStruct objects and return the myStruct object that holds a specific integer id.

Is this possible?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
You should just use a linked list.

When you define the create method for myStruct, add the node to a global list. Then remove it when a struct is destroyed.

JASS:
struct myStruct
    thistype next
    thistype prev 
    integer x // example
 
    method destroy takes nothing returns nothing
        set this.next.prev = this.prev
        set this.prev.next = this.next 
        call this.deallocate()
    endmethod
 
    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        set thistype(0).next.prev = this
        set this.next = thistype(0).next 
        set thistype(0).next = this
        set this.prev = thistype(0)
        return this
    endmethod
endstruct


Now if you ever want to check if there is a specific struct with x equal to 5, you can just do:
JASS:
local myStruct node = myStruct(0).next
loop
    exitwhen node.x == 5
    // or you can do if node.x == 5 or w/e you want
    set node = node.next
endloop
 
General chit-chat
Help Users

      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