[Custom Inventory]: a custom add_item_error subclass passed to AddError arrives as a plain add_item_error

Summary

I made my own error type, inventory_full_error (a subclass of add_item_error), and used it to refuse an item in the inventory’s OnReceive.

The code that called AddItem does get one error back, but it is no longer an inventory_full_error: casting it to inventory_full_error fails. So the caller can’t find out why the item was refused.

Please select what you are reporting on:

Verse

What Type of Bug are you experiencing?

Verse

Steps to Reproduce

  1. Create a blank island and turn on Custom Items and Inventory in Project Settings.
  2. Add the Verse file below and place error_cast_test_device in the level.
  3. Launch a session. Everything runs in OnBegin; no player action is needed.
  4. Read the Verse log.
using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Itemization }
using { /Verse.org/SceneGraph }
using { /Verse.org/Simulation }

# Our own error type, so the caller can tell why the add failed.
inventory_full_error := class(add_item_error) {}

# An inventory that refuses every item with inventory_full_error.
refusing_inventory := class(inventory_component):
    OnReceive<override>(SceneEvent:scene_event):logic =
        if (Query := add_item_query_event[SceneEvent], Query.Inventory = Self):
            Query.AddError(inventory_full_error{})
            # Check 1: the error we just added, read back inside the query.
            if (Error := Query.Errors.Last[], inventory_full_error[Error]):
                Print("[in the query] expected: inventory_full_error, got: inventory_full_error")
            else:
                Print("[in the query] expected: inventory_full_error, got: add_item_error (not an inventory_full_error)")
        (super:)OnReceive(SceneEvent)

error_cast_test_device := class(creative_device):
    OnBegin<override>()<suspends>:void =
        Sleep(1.0)
        if (Sim := GetSimulationEntity[]):
            # An entity with our inventory, and a plain item, both in the scene.
            InventoryEntity := entity{}
            Inventory := refusing_inventory{Entity := InventoryEntity}
            InventoryEntity.AddComponents(array{Inventory})
            Item := entity{}
            Item.AddComponents(array{item_component{Entity := Item}})
            Sim.AddEntities(array{InventoryEntity, Item})

            # Check 2: the same error, as the caller of AddItem receives it.
            if (Errors := Inventory.AddItem(Item).GetError[], Error := Errors[0]):
                if (inventory_full_error[Error]):
                    Print("[caller] expected: inventory_full_error, got: inventory_full_error ({Errors.Length} error)")
                else:
                    Print("[caller] expected: inventory_full_error, got: add_item_error (not an inventory_full_error) ({Errors.Length} error)")
            else:
                Print("[caller] expected: AddItem to fail, got: success")

Expected Result

The caller receives the error that was passed to AddError, so both checks find an inventory_full_error:

[in the query] expected: inventory_full_error, got: inventory_full_error
[caller] expected: inventory_full_error, got: inventory_full_error (1 error)

Observed Result

The error is an inventory_full_error inside the query, but the caller’s only error is not:

[in the query] expected: inventory_full_error, got: inventory_full_error
[caller] expected: inventory_full_error, got: add_item_error (not an inventory_full_error) (1 error)

The same happens with CanAddItem, with remove_item_error subclasses on CanRemoveItem and RemoveItem, and with <castable> on the subclass.

Workaround

None through the errors. A caller that needs the reason has to ask the inventory directly, through a separate Verse method that repeats the inventory’s checks.

Platform(s)

Base: 6.0.0-60803671+++Fortnite+Release-43.11
Version: 6.0.0-60809112+++Fortnite+Release-43.12
Platform: Windows 10 (22H2) [10.0.19045.6466] (x86_64)

Additional Notes

Use case:
An inventory refuses items for different reasons (for example “locked” vs “no room”), and the code that tried the add needs the reason to show the right message. add_item_error and remove_item_error have no fields, so a subclass is the only way to carry a reason. The docs recommend exactly this: Inventory Component in Fortnite says “You can write a unique error class to be used in these instances” and defines no_slots_add_item_error := class(add_item_error).

If errors are meant to be opaque, please say so in the docs and offer another way to pass a refusal reason to the caller (a field on the base error classes would do).

FORT-1202845 has been created and its status is ‘Unconfirmed’. This is now in a queue to be reproduced and confirmed.