Snippet GetWidgetType

Reaction score
341
GetWidgetType
TriggerHappy187

Script Info


JASS:
function GetWidgetType takes widget w returns integer



This function can detect what type of widget a widget is.

Widgets can be either an unit,item or destructable.

You also may ask why it returns an integer, well this is the easiest way I could think of to detect what type the widget is, by representing a different integer for each different widget type.

  • JASS:
    constant integer WIDGET_TYPE_ITEM
  • JASS:
    constant integer WIDGET_TYPE_UNIT
  • JASS:
    constant integer WIDGET_TYPE_DESTRUCTABLE

If you still don't understand on how you would use those constants, here is an example.
JASS:
scope example initializer InitTrig

    private function Actions takes nothing returns nothing
        local widget u = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
        local widget i = CreateItem('ratf', 0, 0)
        local widget d = CreateDestructable('ATtr', 0, 0, 0, 0, 1)
        
        if GetWidgetType(u) == WIDGET_TYPE_UNIT then
            call BJDebugMsg("u is a unit")
        endif
        
        if GetWidgetType(i) == WIDGET_TYPE_ITEM then
            call BJDebugMsg("i is an item")
        endif
        
        if GetWidgetType(d) == WIDGET_TYPE_DESTRUCTABLE then
            call BJDebugMsg("d is an Destructable")
        endif
    endfunction

    //===========================================================================
    private function InitTrig takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterTimerEvent( t, 0.00, false)
        call TriggerAddAction( t, function Actions )
    endfunction

endscope

The Script

JASS:
library GetWidgetType

    globals
        constant integer WIDGET_TYPE_ITEM            = 0
        constant integer WIDGET_TYPE_UNIT            = 1
        constant integer WIDGET_TYPE_DESTRUCTABLE    = 2
    endglobals
    
    //! textmacro GetWidgetType takes TYPE,TYPECAST
    private function $TYPECAST$ takes widget w returns $TYPE$
        return w
        return null
    endfunction
    //! endtextmacro 
    
    //! runtextmacro GetWidgetType("unit","W2U")
    //! runtextmacro GetWidgetType("destructable","W2D")
    //! runtextmacro GetWidgetType("item","W2I")
    
    function GetWidgetType takes widget w returns integer
        if GetUnitTypeId(W2U(w)) != 0 then
            return WIDGET_TYPE_UNIT
        elseif GetDestructableTypeId(W2D(w)) != 0 then
            return WIDGET_TYPE_DESTRUCTABLE
        elseif GetItemTypeId(W2I(w)) != 0 then
            return WIDGET_TYPE_ITEM
        endif
        return -1
    endfunction
    
endlibrary
 
Reaction score
341
I was wondering is this was possible (but I was thinking get handle type, for a DestroyHandle function, and concluded it was not).

Well done. (+rep)

Pity it's absolutely useless. :(
(Am I mistaken?)

I wouldn't call it useless, someone can find a use for it.
 

Tom Jones

N/A
Reaction score
437
I like this.

As we can't create widgets, widgets must point to an existing unit, item or destructable handle. Thus:

JASS:
library GetWidgetType

    globals
        constant integer WIDGET_TYPE_ITEM            = 0
        constant integer WIDGET_TYPE_UNIT            = 1
        constant integer WIDGET_TYPE_DESTRUCTABLE    = 2
    endglobals
    
    //! textmacro GetWidgetType takes TYPE,TYPECAST
    private function $TYPECAST$ takes widget w returns $TYPE$
        return w
        return null
    endfunction
    //! endtextmacro 
    
    //! runtextmacro GetWidgetType("unit","W2U")
    //! runtextmacro GetWidgetType("destructable","W2D")
    //! runtextmacro GetWidgetType("item","W2I")
    
    function GetWidgetType takes widget w returns integer
        if W2U(w) == w then
            return WIDGET_TYPE_UNIT
        elseif W2D(w) == w then
            return WIDGET_TYPE_DESTRUCTABLE
        elseif W2I(w) == w then
            return WIDGET_TYPE_ITEM
        endif
        return -1
    endfunction
endlibrary

And even:
JASS:
function DestroyWidget takes widget w returns nothing
    if W2U(w) == w then
        call RemoveUnit(W2U(w))
    elseif W2D(w) == w then
        call RemoveDestructable(W2D(w))
    elseif W2I(w) == w then
        call RemoveItem(W2I(w))
    endif
endfunction
 

Romek

Super Moderator
Reaction score
964
> As we can't create widgets
local widget w = CreateUnit/Item/Destructable()?

Works fine, Though you can't use the widget for any functions that take units, items or destructables without typecasting. (SetWidgetLife though (for example), would work fine)
 

Romek

Super Moderator
Reaction score
964
> Give only one example where it could be useful.
There are none, hence why this is in the graveyard. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top