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

GetUnitsSelectedAll not working as intended?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
This line is fired from a condition after an ability has been casted. But I never reach beyond 2 and printing player name. GetUnitsSelectedAll is terminating the execution, and I'm unsure as to why.

JASS:
        method openMenu takes SpellMenu openedMenu, code callback returns nothing
            call BJDebugMsg("player openMenu")
            call ShowUnit(.u, true)
            // If another menu is open we close it and notify changes
            if IsUnitSelected(.u, .p) then
                call BJDebugMsg("1")
                call .notifyCloseMenu(.currentOpenMenu)
                call BJDebugMsg("1.1")
            else
                call BJDebugMsg("2")
                // No previous menu selected, save selection group
                call BJDebugMsg(GetPlayerName(.p))
              [B][U]  set .selectedUnits = GetUnitsSelectedAll(.p)[/U][/B]
                call BJDebugMsg("2.1")
            endif
            call BJDebugMsg("select")
            if GetLocalPlayer() == .p then
                call ClearSelection()
                call SelectUnit(.u, true)
            endif
            call BJDebugMsg("reselected")
            set .currentOpenMenu = openedMenu
            set .prevSelected = true
            call TimerStart(.timerRefresh, REFRESH_RATE, true, callback)
            set .currentWindow = 0
            call .notifyOpenMenu(openedMenu)
        endmethod


EDIT:

The reason was that GetUnitsSelectedAll() wasn't working as intended from a condition scope, if i registered it from an action it worked...


Another strange behaviour im having is that if you shift-click an item to pick up multiple times it will execute multiple pickup events and if i drop and hide the item, the unit will proceed towards the item, pick it up and reveal it...

JASS:
scope ItemMenu initializer Init 
   
    globals 
        private BagMenu bagMenu
        private trigger trgPickup    = CreateTrigger()
       
        private unit bagOwner
        private constant integer BAG_ABILITY = 'A00Q'
        private constant integer BAG_LIMIT    = 18
       
        private integer counter = 0
       
    endglobals
   
    private function OnMoveToInventory takes nothing returns nothing 
        local unit owner = GetTriggerBagOwner()
        local item it = GetTriggerBagItem()
        call DisableTrigger(trgPickup)
        call BJDebugMsg("Item (" + GetItemName(it) + ") was moved to inventory by " + GetUnitName(owner) + ".")
        call UnitAddItem(owner, it)
        call EnableTrigger(trgPickup)
    endfunction 
   
    private function OnItemDrop takes nothing returns nothing 
        call BJDebugMsg("Item (" + GetItemName(GetTriggerBagItem()) + ") was dropped by " + GetUnitName(GetTriggerBagOwner()) + ".")
    endfunction 
   
    private function Open takes nothing returns nothing 
        call bagMenu.open()
    endfunction 
   
    private function OnSpell takes nothing returns boolean 
        local timer t 
        if GetSpellAbilityId() == BAG_ABILITY then 
            return true
        endif
        return false 
    endfunction 
   
    private function OnPickup takes nothing returns nothing 
        local item it = GetManipulatedItem()
        if GetTriggerUnit() != bagOwner then 
            return
        endif
 
        call IssueImmediateOrderBJ(bagOwner, "stop" )    // Attempt to prevent ghost adding items
   
        call SetItemPosition(it, GetUnitX(bagOwner), GetUnitY(bagOwner))
        if not bagMenu.addItem(it) then 
            // Failed to add item - likely due to size limit reached
        else 
            // Item added successfully 
            set counter = counter + 1
            call BJDebugMsg(I2S(counter) + " == " + I2S(bagMenu.count()))
        endif   
        set it = null 
    endfunction
   
    private function Init takes nothing returns nothing 
        local trigger trgSpell = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(trgPickup, EVENT_PLAYER_UNIT_PICKUP_ITEM)
        call TriggerRegisterAnyUnitEventBJ(trgSpell, EVENT_PLAYER_UNIT_SPELL_CAST )
        call TriggerAddCondition(trgSpell, Condition(function OnSpell))
        call TriggerAddAction(trgSpell, function Open)
        set bagOwner  = gg_unit_Hblm_0001
        call UnitAddAbility(bagOwner, BAG_ABILITY)
        set bagMenu = BagMenu.create(bagOwner, BAG_LIMIT)
        call TriggerAddAction(trgPickup, function OnPickup)
       
        call Command.register("-bag", function Open)
       
       
        // Register Bag Item Events 
        call OnBagDrop(function OnItemDrop)
        call OnBagInventory(function OnMoveToInventory)
    endfunction 
   
endscope
 
Last edited:
Level 18
Joined
Nov 21, 2012
Messages
835
Maybe check if item is pickable IsItemPickupable, also please show what happend when bagMenu.addItem is called.
JASS:
    /*****************************************************************************
    *   by Bannar
    *    ExtensionMethods
    *
    *    General purpose functions that extend native jass interface.
    *
    ******************************************************************************
    *
    *    Item handle extension methods:
    *
    *       function GetUnitItemCount takes unit whichUnit returns integer
    *          Returns the number of items equipped.
    *
    *       function IsUnitInventoryFull takes unit whichUnit returns boolean
    *          Checks if unit inventory is full.
    *
    *       function GetUnitItemSlot takes unit whichUnit, item whichItem returns integer
    *          Retrieves slot number of specified item equiped by unit whichUnit or -1 if not found.
    *
    *       function IsItemAlive takes item whichItem returns boolean
    *          Returns value indicating whether specified item is alive.
    *
    *       function IsItemPickupable takes item whichItem returns boolean
    *          Returns value indicating whether specified item can be picked up.
    *
    *****************************************************************************/

    function GetUnitItemCount takes unit whichUnit returns integer
        local integer size = UnitInventorySize(whichUnit)
        local integer slot = 0
        local integer result = 0
        loop
            exitwhen slot >= size
            if UnitItemInSlot(whichUnit, slot) != null then
                set result = result + 1
            endif
            set slot = slot + 1
        endloop

        return result
    endfunction

    function IsUnitInventoryFull takes unit whichUnit returns boolean
        return GetUnitItemCount(whichUnit) == UnitInventorySize(whichUnit)
    endfunction

    function GetUnitItemSlot takes unit whichUnit, item whichItem returns integer
        local integer slot = 0
        local integer size

        if UnitHasItem(whichUnit, whichItem) then
            set size = UnitInventorySize(whichUnit)
            loop
                if UnitItemInSlot(whichUnit, slot) == whichItem then
                    return slot
                endif
                set slot = slot + 1
                exitwhen slot >= size
            endloop
        endif

        return -1 // NOT_FOUND
    endfunction

    function IsItemAlive takes item whichItem returns boolean
        return GetItemTypeId(whichItem) != 0 and GetWidgetLife(whichItem) > 0.405
    endfunction

    function IsItemPickupable takes item whichItem returns boolean
        return IsItemAlive(whichItem) and not IsItemOwned(whichItem) and IsItemVisible(whichItem)
    endfunction
    /*
    *   END ExtensionMethods by Bannar
    */
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
It is hidden and moved to unit position. I tried IsItemPickable and I still get the Warning some times. Though the current solution of just hiding them again will work I think.

JASS:
/*
            Adds a given item to the bag. If the bag is full it will return false, else
            the item will be hidden from the map.
        */
        method addItem takes item whichItem returns nothing
            local integer index = slots.size()
            local string btn
            local Slot slot
          
            if not IsItemPickupable (whichItem) then
                call BJDebugMsg("CANNOT PICKUP ITEM: " + GetItemName(whichItem))
            endif
          
            if .contains(whichItem) then
                call SetItemVisible(whichItem, false)    // we hide it again to prevent bugs
                debug call BJDebugMsg("[BagPlugin_BagMenu_additem] Warning: " + GetItemName(whichItem) + " is already inside the bag.")
                return
            endif
            if index >= limit then
                debug call BJDebugMsg("[BagPlugin_BagMenu_additem] Warning: Bag is full.")
                return
            endif
            call SetItemVisible(whichItem, false)
            set slot = Slot.create(whichItem)
            call .slots.add(index, slot)
            set btn = BlzGetItemIconPath(whichItem)
            set slot.option = .menu.createOption(index, thistype.getItemName(whichItem), BlzGetItemExtendedTooltip(whichItem), btn, btn, function thistype.onItemSelected)
            set thistype.slotOptionMap.integer[slot.option] = slot    // SpellOption --> Slot
        endmethod
 
Last edited:
Status
Not open for further replies.
Top