UnitInventoryGroup can contain null unit reference

In my Diablo Mortal Shroud map, I noticed a bug that was added in the last few years, possibly in 5.0.

I use inventory items and a dialog to display them.

After removing an item from the inventory via the Move Unit action, I loop over the unit group of the carried items. Unfortunately, the unit group contains null references which causes the loop to end too early.

In trigger GUI’s raw display:

    ForEachUnitInGroup(item,(UnitInventoryGroup(hero[p1])))
        actions
            drawItem2(p1,dialog,item,false,sellVal,repairCosts,rechargeCosts,unsocketCosts,craftButton,singleButtons,toStash)

In the generated script:

auto86F8FDC8_g = UnitInventoryGroup(gv_hero[lp_p1]);
auto86F8FDC8_u = UnitGroupCount(auto86F8FDC8_g, c_unitCountAll);
for (;; auto86F8FDC8_u -= 1) {
    lv_item = UnitGroupUnitFromEnd(auto86F8FDC8_g, auto86F8FDC8_u);
    if (lv_item == null) { break; }
    gf_drawItem2(lp_p1, lv_dialog, lv_item, false, lv_sellVal, lv_repairCosts, lv_rechargeCosts, lv_unsocketCosts, lv_craftButton, lv_singleButtons, lv_toStash);
}

As you can see, the loop ends when lv_item is null. Unfortunately, the unit group loop returns null references for items that just left the inventory. Thus, the loop is not iterating over all items.

Version 5.0.1.81009

I just figured that I noticed this issue in February 2018 already, so it was added around version 4.1.4 or some patch after that.

Anyway, it is still an issue as loops will fail to iterate over all inventory items.

Workaround:

  • use the deprecated loop and add a non-null-check for the loop variable