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

Struct extends Array - Help

Status
Not open for further replies.
Level 3
Joined
Dec 16, 2010
Messages
31
Can someone explain how I can get unique id's for structs that follow the struct extends array type without any units or allocate() involved?

for instance I would like to have:

JASS:
struct TEST extends array
    static method createNormal takes nothing returns thistype
         local thistype this = thistype[someID]
         return this
    endmethod
endstruct

I would like to know techniques that people have used to generate "someID" as in the example above. And also I would like to know how they managed these structs and their indecies. Like how did you recycle them?

Edit: you know, Bribe seems to have done what I was trying to do. I suppose the unique index stuff from units was clouding my thought process.
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
Here you are

Full commented look into the background workings of structs

JASS:
//a struct that extends an array is code with the syntactic sugar of a struct
//this means that no extra code is generated

//structs normally generated create, destroy, and onDestroy
struct Hello extends array
    //instantiation can be a tricky thing. When a struct is created, you have to ensure
    //that the instance returned is unique. Furthermore, when a struct is destroyed,
    //you need to reuse that struct's instance to save on memory

    //instanceCount is a counter that continues to increase as new instances are created
    private static integer instanceCount = 0 //how many Hello's that are created

    //recycleCount is a counter that continues to increase as structs are destroyed
    //this is to ensure that destroyed instances are used the next time the struct
    //is created
    private static integer recycleCount = 0 //how many Hello ids that were recycled

    //recycle is an array that stored the destroyed instances
    private static integer array recycle //an array storing recycled Hello's

    //this is the create method, a method that creates unique instances of the struct
    //thistype is a keyword that translates into the struct's name
    //in this case, thistype would be Hello
    //if the struct was named BooYa, thistype would be BooYa
    public static method create takes nothing returns thistype
        //this is a keyword that refers to the current instance
        //static methods are like regular functions, so they don't get a this, which is
        //why I was able to declare one
        //static methods are called via struct.method()
        local thistype this

        //first check for any destroyed structs
        if (recycleCount > 0) then //if there are Hello's waiting to be reused
            //decrease the counter so that you are on the right slot in the array
            set recycleCount = recycleCount - 1 //dec counter

            //set the current instance to the destroyed struct
            set this = recycle[recycleCount] //use it
        else
            //if there are no destroyed structs, make a new one by increasing
            //the instance counter
            set instanceCount = instanceCount + 1 //0 is null, so first inc by 1

            //set the current instance to the instance counter
            set this = instanceCount
        endif

        //perform creation code here :O

        //return the created struct
        return this
    endmethod

    //destroy is called when the struct is to be destroyed
    //this is not a static method
    //a non static method translates to this (using destroy as an example)
    //public static method destroy takes thistype this returns nothing
    //the instance of the struct is automatically passed in
    //they are called via structInstance.method()

    public method destroy takes nothing returns nothing
        //remember this is passed in, so it exists!
        //store this into the recycle variable
        //the first struct destroyed would be recycle[0] = this
        set recycle[recycleCount] = this
        //increase recycle counter
        set recycleCount = recycleCount + 1

        //if create was called, recycleCount would be decreased back down to 0
        //and recycle[0] would be used

        //put destruction code here
    endmethod
endstruct

Above allows this
JASS:
local Hello h = Hello.create()
call h.destroy()


If you have any sort of list, then you can use this model instead
JASS:
struct Hello extends array
    private static thistype recycle = 0
    private static integer instanceCount = 0

    private thistype next
    //private thistype previous??

    static method create takes nothing returns thistype
        local thistype this

        if (recycle == 0) then
            set instanceCount = instanceCount + 1
            set this = instanceCount
        else
            set this = recycle
            set recycle = recycle.next
        endif

        return this
    endmethod

    method destroy takes nothing returns nothing
        /*remove from list??
            set next.previous = previous.next
            set previous.next = next.previous
       */

        set next = recycle
        set recycle = this
    endmethod



Really, the second model is faster in all scenarios, but most people use the first ; O.
 
Status
Not open for further replies.
Top