UE5/UE4 C++でアクター(Actor)が持つコンポーネント(Component)をクラス(Class)を指定して取得する(GetComponentByClass、GetComponentsByClass、UComponent::StaticClass)
こちらのサイトを参考にさせていただきました。
・UE4 コンポーネント取得 子供オブジェクト取得
https://qiita.com/yukimm123/items/9623a3ace7f7f32a2c53
コンポーネントの取得は「GetComponentByClass()」関数を使います。
こんな感じで「Actor」が持つ「Component」を一つだけ取得することができます。// アクターのコンポーネントを一つ取得
TObjectPtr< AActor> aActor;
TObjectPtr< UActorCompoent> aComponent = aActor->GetComponentByClass(UNewComponent::StaticClass());
取得したいクラスを「UComponent::StaticClass()」で引数に指定します。
全て取得したい場合は「GetComponentsByClass()」関数を使います。
こんな感じで「Actor」が持つ「Component」をすべて取得することができます。// アクターのコンポーネントをすべて取得
TObjectPtr< AActor> aActor;
TArray< TObjectPtr< UActorCompoent>> aComponents = aActor->GetComponentsByClass(UNewComponent::StaticClass());
取得したコンポーネントは「UActorCompoent」になっているため、使う場合はキャストを行います、
// 取得したクラスにキャストする
TObjectPtr< UNewComponent> aNewComponent = Cast< UNewComponent>(aComponent);
結構使う頻度は多いと思うので基礎的なことですが覚えておきたいところです。
| UE5 | 10:00 | comments:0 | trackbacks:0 | TOP↑