• 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.

[Solved] Operator to find a member variable's struct instance?

Status
Not open for further replies.
Level 14
Joined
Jul 1, 2008
Messages
1,314
Good Day Ladies and Gentlemen,

I would like to know, if there is a way to use a very simple way in vJass, to find the corresponding struct instance of a member variable. Like an operator method?

I got a treasure chest struct and want to find out the corresponding struct instance, given the treasure chest's unit!

Example:

JASS:
sruct chest
       unit u
       boolean open
endstruct

private function IsChestClosed takes unit ChestUnit returns boolean
        local chest c
        if GetUnitTypeId(ChestUnit) == CHEST_OBJECT_ID then
             // CAN I DO THIS?
             set c = GetInstanceOf( ChestUnit)
             // CAN I DO THIS?
             if c.open then
                  return true
             endif
        endif
        return false
 endfunction


JASS:
struct chest
      static group allChests = CreateGroup()
 
     static method register takes unit U returns thistype
            local thistype this = 0

           // CAN I DO THIS?
           // How can I access this static group?
      
            if not IsUnitInGroup( U, thistype.allChests) then
                set this = thistype.allocate()
                call GroupAddUnit( thistype.allChests, U)
                set this.u = U
            endif
      
            return this
        endmethod
 
endstruct

Is there a direct way?

thanks for any help! :)
 
Last edited:
You can use a hashtable or an unit indexer.

JASS:
local chest c = chest.create()

// with unit indexer
set UnitChestData[GetUnitId(u)] = c

// or with hashtable
call SaveInteger(Hashtable, 0, GetHandleId(u), c)
Then to retrieve it:

JASS:
// with unit indexer
local chest c = UnitChestData[GetUnitId(u)]

// or with hashtable
set c = chest(LoadInteger(Hashtable, 0, GetHandleId(u)))
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
Thanks for that fast and helpful reply, and for clarifying it by adding code! :) (though there is a type chest(LoadInteger()) should be simply LoadInteger(), isnt it?)

As I cannot use an unit indexer in this case I choose that hashtable approac - which works quite well.

By this, I conclude that there is no "inbuilt" way to structs to retrieve the instances of a member, right?

I guess this is solvedd :)
 
structs is integer, yes you can save/load it like it.

basicly yes that's good ways shown by TriggerHappy, you can just write your own handy function that will do it for you if you want to have in built in.

JASS:
static method getInstance takes unit u returns thistype
    return LoadInteger(Hashtable, 0, GetHandleId(u))
endmethod
 
Status
Not open for further replies.
Top