👏

【UEFN/SceneGraph】インベントリ(クイックバー)の空きスロットの取得

に公開
2025/12/24

前書き

Qiita Advent Calendar 2025 「UEFNから始めるひとりアドカレ」 Day12 の記事です

今回は、インベントリ(クイックバー)のスロットがいくつ空いているのかを調べる方法です。

手のアイコンの右隣りの5つのスロットです。

環境:
UEFN v39.11 (UE5.8)

1. 事前設定

プロジェクト設定

カスタムアイテムインベントリ、シーングラフ実験的機能 を有効にします。

Island Settings

カスタムのインベントリ構成 で、 ItemizationConfiguration_BRStype を選択します。

2. fort_inventory_weapon_hotbar_component の取得

コンポーネントの検索

該当のインベントリは、fort_inventory_weapon_hotbar_component の機能です。
公式ドキュメントによると、プレイヤーがそのコンポーネントを持つので、FindDescendantComponents() で検索します。

プレイヤー.FindDescendantComponents(fort_inventory_weapon_hotbar_component)

取得件数は1件となるかと思っていたのですが、実際は2件取得しました。
どちらのコンポーネントを使うのかこれではよくわかりませんでしたが、試しに、アイテムスポナーからプレイヤーが武器を入手すると一方のコンポーネントにのみ、データが入りました。

対象コンポーネントの特定

武器・アイテムを入手したときに使用する fort_inventory_weapon_hotbar_component を特定して、保持しておくことにします。
特定する方法は、インベントリにアイテムを追加するメソッド AddItem() を実行します。
一方のコンポーネントではエラーの結果となり、もう一方のコンポーネントではアイテム追加に成功しました。
成功したコンポーネントを変数(FortWeaponHotbarCompQ)に格納しておきます。
そして、インベントリをもとの空の状態に戻すため、インベントリからアイテムを取り除くメソッド RemoveItem() で追加したアイテムを削除します。

    # インベントリ(クイックバー)に該当するコンポーネントを特定して、 FortWeaponHotbarCompQ に保持する
    SetFortWeaponHotbarComp(Agent:agent)<suspends>:void=

        # 適当な武器・アイテムの entity配列
        WeaponEntities:[]entity = array:
            item_frozen_icecream_cone{}
            item_guzzling_icecream_cone{}
            item_icecream_cone{}
            item_lilwhips_special_serve{}
            item_spicy_icecream_cone{}

        for(FortWeaponHotbarComp : Agent.FindDescendantComponents(fort_inventory_weapon_hotbar_component)):
            var IsError:logic = false

            # エラーが発生すれば処理スキップ。次の FortWeaponHotbarComp へ
            for(Index := 0..4, WeaponEntity := WeaponEntities[Index], not IsError?):
                Result := FortWeaponHotbarComp.AddItem(WeaponEntity)
                # Result := FortWeaponHotbarComp.CanAddItem(WeaponEntity)  # どちらのEntityも成功したので不適
                if(ErrorType := Result.GetError[]):
                    set IsError = true

            Sleep(0.01)

            for(Index := 0..4, WeaponEntity := WeaponEntities[Index]):
                Result := FortWeaponHotbarComp.RemoveItem(WeaponEntity)
                if(ErrorType := Result.GetError[]):
                    set IsError = true

            # エラーが発生しなかった場合、変数に保持する
            if(not IsError?):
                set FortWeaponHotbarCompQ = option{ FortWeaponHotbarComp }

3. 各スロットのアイテムをチェック

GetItemInSlot() でスロットが空いているか

GetItemInSlot() の引数にスロットのインデックスを渡して実行すると、そのスロットの item_component を取得できます。
アイテムが入っていない場合に失敗するので、失敗したときに空きスロットであることがわかります。

    # fort_inventory_weapon_hotbar_component の空スロットをチェックする
    WeaponHotbarEmptyCount(Agent:agent):void=

        var EmptySlotIndexs:[]string = array{}
        if(FortWeaponHotbarComp := FortWeaponHotbarCompQ?):
            # デフォルトでスロット数は5
            for(SlotIndex := 0..4):
                if(ItemComp := FortWeaponHotbarComp.GetItemInSlot[SlotIndex]):
                    Print("  - Slot[{SlotIndex}] ItemComp: " + ToDiagnostic(ItemComp))
                    
                else:
                    Print("  - Slot[{SlotIndex}] is empty.")
                    set EmptySlotIndexs += array{"{SlotIndex}"}
           
            Print("空のスロットのインデックス: {Join(EmptySlotIndexs, ", ")}")
            Print("空のスロットの数: {EmptySlotIndexs.Length}" )
        
        else:
            Print("FortWeaponHotbarCompQ is None. Error")

コード全体

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/SceneGraph }
using { /Fortnite.com/Characters }

using { /UnrealEngine.com/Itemization }
using { /Fortnite.com/Itemization }
using { /Fortnite.com/Itemization/VibinItems }

using { SceneGraphHelper }

weapon_hotbar_check_empty_slot_device := class(creative_device):

    var FortWeaponHotbarCompQ:?fort_inventory_weapon_hotbar_component = false

    OnBegin<override>()<suspends>:void=

        if(FirstPlayer := GetPlayspace().GetPlayers()[0]):
            SetFortWeaponHotbarComp(FirstPlayer)

            if(FC := FirstPlayer.GetFortCharacter[]):
                FC.CrouchedEvent().Subscribe(OnCrouched)

    OnCrouched(CrouchedResult:tuple(fort_character, logic)):void=
        if:
            CrouchedResult(1)?
            CrounchedAgent := CrouchedResult(0).GetAgent[]
            
        then:
            WeaponHotbarEmptyCount(CrounchedAgent)


    # インベントリ(クイックバー)に該当するコンポーネントを特定して、 FortWeaponHotbarCompQ に保持する
    SetFortWeaponHotbarComp(Agent:agent)<suspends>:void=

        WeaponEntities:[]entity = array:
            item_frozen_icecream_cone{}
            item_guzzling_icecream_cone{}
            item_icecream_cone{}
            item_lilwhips_special_serve{}
            item_spicy_icecream_cone{}

        for(FortWeaponHotbarComp : Agent.FindDescendantComponents(fort_inventory_weapon_hotbar_component)):
            var IsError:logic = false

            # エラーが発生すれば処理スキップ。次の FortWeaponHotbarComp へ
            for(Index := 0..4, WeaponEntity := WeaponEntities[Index], not IsError?):
                Result := FortWeaponHotbarComp.AddItem(WeaponEntity)
                # Result := FortWeaponHotbarComp.CanAddItem(WeaponEntity)  # どちらのEntityも成功したので不適
                if(ErrorType := Result.GetError[]):
                    set IsError = true

            Sleep(0.01)

            for(Index := 0..4, WeaponEntity := WeaponEntities[Index]):
                Result := FortWeaponHotbarComp.RemoveItem(WeaponEntity)
                if(ErrorType := Result.GetError[]):
                    set IsError = true

            if(not IsError?):
                set FortWeaponHotbarCompQ = option{ FortWeaponHotbarComp }

    # fort_inventory_weapon_hotbar_component の空スロットをチェックする
    WeaponHotbarEmptyCount(Agent:agent):void=

        var EmptySlotIndexs:[]string = array{}
        if(FortWeaponHotbarComp := FortWeaponHotbarCompQ?):
            # デフォルトでスロット数は5
            for(SlotIndex := 0..4):
                if(ItemComp := FortWeaponHotbarComp.GetItemInSlot[SlotIndex]):
                    Print("  - Slot[{SlotIndex}] ItemComp: " + ToDiagnostic(ItemComp))
                    
                else:
                    Print("  - Slot[{SlotIndex}] is empty.")
                    set EmptySlotIndexs += array{"{SlotIndex}"}
           
            Print("空のスロットのインデックス: {Join(EmptySlotIndexs, ", ")}")
            Print("空のスロットの数: {EmptySlotIndexs.Length}" )
        
        else:
            Print("FortWeaponHotbarCompQ is None. Error")

動作確認

https://youtu.be/CdvhjE93w4U

参考情報

https://dev.epicgames.com/documentation/ja-jp/fortnite/fort-inventory-component-in-fortnite

Discussion