Check if widget is a unit

Grundy

Ultra Cool Member
Reaction score
35
A widget can be a doodad, item, unit, or widget. Is there a way to tell if a certain widget is a unit or a doodad or an item? I want to do something different if the widget in question is a unit or an item.
 

Grundy

Ultra Cool Member
Reaction score
35
I just thought of something, similiar to how people check if a doodad is a tree:

JASS:

globals
    constant integer DUMMY_ID = 'h000' //dummy unit's code
    constant integer UNIT_TARGET_SPELL = 'A000' //spell code for an ability that can only target units
    constant string ORDER_STRING = "channel" //UNIT_TARGET_SPELL's order string
endglobals

function IsWidgetUnit takes widget whichWidget returns boolean
    local unit dummy = CreateUnit(Player(12), DUMMY_ID, GetWidgetX(whichWidget), GetWidgetY(whichWidget), 0)
    local boolean result
    call UnitAddAbility(dummy, UNIT_TARGET_SPELL)
    set result = IssueTargetOrder(dummy, ORDER_STRING, whichWidget)
    call KillUnit(dummy)
    set dummy = null
    return result
endfunction


But I guess this now belongs in JASS help, huh?

I'll try what you said, if that doesn't work maybe this will - I'm at work, can't test now.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
I've tested an it works, but you need to convert the widget like this :

JASS:
library WidgetType

globals
    constant integer WIDGET_NULL = 0
    constant integer WIDGET_UNIT = 1
    constant integer WIDGET_DESTRUCTABLE = 2
    constant integer WIDGET_ITEM = 3
endglobals
    
public function W2U takes widget w returns unit
    return w
    return null
endfunction

public function W2D takes widget w returns destructable
    return w
    return null
endfunction

public function W2I takes widget w returns item
    return w
    return null
endfunction

function GetWidgetType takes widget w returns integer
    if w == null then
        return WIDGET_NULL
    endif
    if GetUnitTypeId(W2U(w)) != 0 then
        return WIDGET_UNIT
    endif
    if GetDestructableTypeId(W2D(w)) != 0 then
        return WIDGET_DESTRUCTABLE
    endif
    if GetItemTypeId(W2I(w)) != 0 then
        return WIDGET_ITEM
    endif
    return WIDGET_NULL
endfunction

endlibrary


Hope you understand vJass, if not i can explain how to use.
 

Strilanc

Veteran Scripter
Reaction score
42
The method provided by AceHart will work, with the added bonus that removed-units-you-still-have-a-reference-to will be classified as not-unit. You will need to cast the widget to a unit first, using the return bug.

You can do the same thing for a lot of types. Almost all the native methods return 0 or null when run on the incorrect type.
 
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