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

[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 :)
 
Status
Not open for further replies.
Top