[UE] Orden de ejecución de Blueprint y C++ Beginplay

Beginplay se ejecuta cuando se carga el nivel y solo se llama en tiempo de ejecución. Entonces, ¿cuál es el orden de ejecución de la función Beginplay de C++ y blueprint?

demasiado tiempo no mires la versión

Permítanme hablar sobre la conclusión primero: en realidad depende del momento en que C++ llame al método Beginplay de la clase principal en el cuerpo de la función Beginplay. Solo la lógica antes de llamar a la clase principal BeginPlay se ejecutará antes del modelo BeginPlay.

Porque BeginPlay de Blueprint se llama en el método AActor::BeginPlay()

dar una castaña

Veamos un ejemplo

// Called when the game starts or when spawned
void ABeginplayOrderTest::BeginPlay()
{
    
    
	UE_LOG(LogTemp, Warning, TEXT("CPP Before BeginPlay"));
	Super::BeginPlay();
	UE_LOG(LogTemp, Warning, TEXT("CPP After BeginPlay"));
}

Resultados de la:
inserte la descripción de la imagen aquí

Análisis de código fuente

Los objetos heredados de Actor se transferirán a AActor::BeginPlay() después de llamar a Super::BeginPlay

Y en esta función se transfiere un método BlueprintImplementableEventReceiveBeginPlay(); , que en realidad es el blueprintEvent BeginPlay

ReceiveBeginPlay();Declaración de método:

//AActor.h
	/** Event when play begins for this actor. */
	UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName = "BeginPlay"))
	void ReceiveBeginPlay();

Cuerpo de función completa:

// AActor.cpp
void AActor::BeginPlay()
{
    
    
	TRACE_OBJECT_EVENT(this, BeginPlay);

	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;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_44559752/article/details/127177576
Recomendado
Clasificación