この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
wiki:ue4:tips:501 [2015/03/28 10:00] step |
— (現在) | ||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== C++/コンポーネントの追加(4.7.3) ====== | ||
- | |||
- | - 「詳細パネル」の「新規コンポーネントの追加」から「新規C++コンポーネントの追加…」を選択する。 | ||
- | - 「ActorComponent」を選択して次へ進む。 | ||
- | - 「MyActorComponent」など名前を付けて「クラスを作成」する。 | ||
- | |||
- | |||
- | <code csharp> | ||
- | // MyActorComponent.h | ||
- | class STDPROJECT_API UMyActorComponent : public UActorComponent | ||
- | { | ||
- | GENERATED_BODY() | ||
- | |||
- | private: | ||
- | // 追加 | ||
- | float m_fRunningTime; | ||
- | } | ||
- | </code> | ||
- | |||
- | <code csharp> | ||
- | |||
- | // 開始時に呼ばれる | ||
- | void UMyActorComponent::InitializeComponent() | ||
- | { | ||
- | Super::InitializeComponent(); | ||
- | } | ||
- | |||
- | |||
- | // 毎フレーム呼ばれる | ||
- | void UMyActorComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) | ||
- | { | ||
- | Super::TickComponent( DeltaTime, TickType, ThisTickFunction ); | ||
- | |||
- | // 追加 | ||
- | AActor* pOwner = GetOwner(); | ||
- | FVector NewLocation = pOwner->GetActorLocation(); | ||
- | float DeltaHeight = (FMath::Cos(m_fRunningTime + DeltaTime) - FMath::Cos(m_fRunningTime)); | ||
- | NewLocation.X += DeltaHeight * 20.0f; | ||
- | m_fRunningTime += DeltaTime; | ||
- | pOwner->SetActorLocation(NewLocation); | ||
- | } | ||
- | </code> | ||
- | |||
- | 動作はCos移動するだけの物を実装してみました。プロジェクトをビルドしてエラーが出ないことを確認します。 | ||
- | |||
- | - UE4エディターに戻って「MyActorComponent」を任意のアクターに割り当てます。 | ||
- | {{:wiki:ue4:tips:ue4_add_component.png|}} | ||
- | |||
- | これで再生するとアクターが動くはずですが、もし動かない場合は「アクターの可動性」が「スタティック」になっているので、「ムーバブル」に変更します。 | ||
- | |||
- | |||
- | {{:wiki:ue4:tips:ue4_actor_movable.png|}} | ||
- | |||