はじめに
Fortnite の v39.00 (UE 5.8) のアップデートで、NPCの動作を制御するための新しいコンポーネントが追加されました。これにより、NPCの行動や認識をより細かく制御できるようになり、カスタムAIの実装が一層柔軟になります。
参考: v39.00 リリースノート
本記事では、NPCのEntityを取得し、追加されたコンポーネントを確認する方法を解説します。
環境: Fortnite v39.00 (UE 5.8)
準備
-
npc_behavior のサブクラス作成
Verse Explore で、 NPC Behavior Basic を選択して作成します。処理は変更しません。 -
NPCキャラクター定義作成
- Type: Guard
- Type: Custom
それぞれに NPCBehavior Script を設定します。
-
レベル上にデバイスを配置
NPCキャラクター定義2種類と、ボリュームデバイスを1つ配置します。
方法1: npc_behavior.GetEntity[]
GetNPCEntitiesWithNPCBehavior():[]entity=
var ReturnValue:[]entity = array{}
for(I -> AgentInVolume : Volume.GetAgentsInVolume()):
if(NPCBehavior := AgentInVolume.GetNPCBehavior[]):
if(NPCEntity := NPCBehavior.GetEntity[]):
set ReturnValue += array{NPCEntity}
ReturnValue
- volume_device.GetAgentsInVolume() で []agent を取得
- agent.GetNPCBehavior[] で npc_behavior を取得
-
npc_behavior.GetEntity[]を使って、NPCのEntityを取得
NPCキャラクター定義を作成していないときや、NPCBehavior Script を設定していないときは GetNPCBehavior[] が失敗します。
方法2: fort_character.GetEntity[]
GetNPCEntitiesWithFortCharacter():[]entity=
var ReturnValue:[]entity = array{}
for(I -> AgentInVolume : Volume.GetAgentsInVolume()):
if:
not player[AgentInVolume]
NPCFC := AgentInVolume.GetFortCharacter[]
then:
if(NPCEntity := NPCFC.GetEntity[]):
set ReturnValue += array{NPCEntity}
ReturnValue
- volume_device.GetAgentsInVolume() で []agent を取得
- プレイヤー以外のAgentを判定 ( not player[AgentInVolume] )
- agent.GetFortCharacter[] で fort_character を取得
-
fort_character.GetEntity[]を使って、NPCのEntityを取得
※ 野生動物やクリーチャーも該当するかは未検証。
NPCコンポーネントの取得
ジャンプイベントをトリガーに、NPCのコンポーネントをログ出力する例です。
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/Characters }
using { /Verse.org/SceneGraph }
using { /Fortnite.com/AI }
log_npc_comp_test := class(log_channel){}
npc_comp_test_verse := class(creative_device):
@editable
Volume:volume_device = volume_device{}
Logger : log = log{Channel := log_npc_comp_test}
OnBegin<override>()<suspends>:void=
for(Player : GetPlayspace().GetPlayers()):
if(FC := Player.GetFortCharacter[]):
FC.JumpedEvent().Subscribe(OnJumped)
# プレイヤーがジャンプしたときの処理
OnJumped(FC:fort_character):void=
NPCEntities := GetNPCEntitiesWithNPCBehavior()
Logger.Print("NPCEntities Count: {NPCEntities.Length}")
# NPC の Component をログ出力
for(I -> NPCEntity : NPCEntities):
Logger.Print("NPCEntities[{I}] ")
for(J -> NPCComp : NPCEntity.GetComponents()):
if(guard_actions_component[NPCComp]):
Logger.Print(" - [{J}] guard_actions_component")
else if(npc_actions_component[NPCComp]):
Logger.Print(" - [{J}] npc_actions_component")
else if(guard_awareness_component[NPCComp]):
Logger.Print(" - [{J}] guard_awareness_component")
else if(npc_awareness_component[NPCComp]):
Logger.Print(" - [{J}] npc_awareness_component")
# Volume 内の NPC の Entity を取得
GetNPCEntitiesWithNPCBehavior():[]entity=
var ReturnValue:[]entity = array{}
for(I -> AgentInVolume : Volume.GetAgentsInVolume()):
if(NPCBehavior := AgentInVolume.GetNPCBehavior[]):
if(NPCEntity := NPCBehavior.GetEntity[]):
set ReturnValue += array{NPCEntity}
ReturnValue
親子関係にあるので、条件分岐の順序に注意
guard_actions_component(子) → npc_actions_component(親)
guard_awareness_component(子) → npc_awareness_component(親)
出力例
NPCEntities Count: 2
NPCEntities[0]
- [1] npc_actions_component
- [3] npc_awareness_component
NPCEntities[1]
- [1] guard_actions_component
- [2] guard_awareness_component
まとめ
- v39.00でNPCに 4種類の新しいコンポーネント が追加された
- Entity取得方法は2通り
- npc_behavior.GetEntity[]
- fort_character.GetEntity[]
- NPCのTypeによって持つコンポーネントが異なる
- Custom Type → npc_actions_component, npc_awareness_component
- Guard Type → guard_actions_component, guard_awareness_component
これで、NPCの行動制御をより細かくログ出力・検証できるようになります。
なお、アドベントカレンダー期間中には 「コンポーネントを実際に使ってみた」応用記事 も執筆予定です。おたのしみにー!



