F13.BeginPlay|启动事件
大约 2 分钟
新建一个测试actor
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyTest.generated.h"
UCLASS()
class EXORCIST_API AMyTest : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyTest();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
void AMyTest::BeginPlay()
{
Super::BeginPlay();
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("C++ BeginPlay"));
}
void AActor::BeginPlay()
{
TRACE_OBJECT_LIFETIME_BEGIN(this);
ensureMsgf(ActorHasBegunPlay == EActorBeginPlayState::BeginningPlay, TEXT("BeginPlay was called on actor %s which was in state %d"), *GetPathName(), (int32)ActorHasBegunPlay);
SetLifeSpan( InitialLifeSpan );
RegisterAllActorTickFunctions(true, false); // Components are done below.
TInlineComponentArray<UActorComponent*> Components;
GetComponents(Components);
for (UActorComponent* Component : Components)
{
// bHasBegunPlay will be true for the component if the component was renamed and moved to a new outer during initialization
if (Component->IsRegistered() && !Component->HasBegunPlay())
{
Component->RegisterAllComponentTickFunctions(true);
Component->BeginPlay();
ensureMsgf(Component->HasBegunPlay(), TEXT("Failed to route BeginPlay (%s)"), *Component->GetFullName());
}
else
{
// When an Actor begins play we expect only the not bAutoRegister false components to not be registered
//check(!Component->bAutoRegister);
}
}
if (GetAutoDestroyWhenFinished())
{
if (UWorld* MyWorld = GetWorld())
{
if (UAutoDestroySubsystem* AutoDestroySys = MyWorld->GetSubsystem<UAutoDestroySubsystem>())
{
AutoDestroySys->RegisterActor(this);
}
}
}
ReceiveBeginPlay();
ActorHasBegunPlay = EActorBeginPlayState::HasBegunPlay;
}
注
meta=(DisplayName = "BeginPlay")
用于修改显示节点名。
因为我们加了Super::BeginPlay();所以执行顺序如下:
- 重写后的Actor
BeginPlay
- 执行
BeginPlay
中的ReceiveBeginPlay()
;——》BP BeginPlay
- 执行打印——》C++
BeginPlay