Unreal Learning Notes 13—C++ Static and Dynamic Loading

I. Introduction

        We can easily add various required components in the blueprint, so how to implement it in C++ code. The code is divided into static and dynamic loading. Regardless of static or dynamic, the loaded content includes resources and resource classes. Resource classes are usually blueprint classes with resources.

2. Realization

        When implementing static or dynamic loading, you need to create the required types in the constructor in advance. The code is as follows:

	MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MyScene"));
	MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
	MyParticle = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyParticle"));
	MyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBox"));
	MyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("MyAudio"));

Then set its parent-child level

	//设置父子级
	RootComponent = MyScene;//将MyScene作为默认的根组件
	MyMesh->SetupAttachment(MyScene);
	MyParticle->SetupAttachment(MyScene);
	MyBox->SetupAttachment(MyScene);
	MyAudio->SetupAttachment(MyBox);
2.1. Static loading of resources

        Static loading must also be in the constructor, the code is as follows:

	//静态加载资源
	static ConstructorHelpers::FObjectFinder<UStaticMesh>TempStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Pipe.Shape_Pipe'"));
	MyMesh->SetStaticMesh(TempStaticMesh.Object);
	static ConstructorHelpers::FObjectFinder<UParticleSystem>TempParticle(TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Fire.P_Fire'"));
	MyParticle->SetTemplate(TempParticle.Object);
	static ConstructorHelpers::FObjectFinder<USoundWave>TempSoundWave(TEXT("/Script/Engine.SoundWave'/Game/StarterContent/Audio/Collapse01.Collapse01'"));
	MyAudio->SetSound(TempSoundWave.Object);

The method to obtain the path in "TEXT" is as follows:

First, find the resource in the editor content, then right-click "Copy Reference", as shown in Figure 2.1.1, and then add the copied reference to the above TEXT.

Figure 2.1.1
2.2. Static loading of classes

        The statically loaded class must also be in the constructor. The code is as follows. Note: The difference between classes and resources is that "_C" must be added after the class, otherwise there will be uncontrollable compilation errors or runtime crashes.

	//静态加载类,复制引用后,文件名后面必须加上"_C",不然会运行崩溃
	static ConstructorHelpers::FClassFinder<AActor>TempMyActor(TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));
	MyActor = TempMyActor.Class;

        In addition, the loaded class created here cannot be displayed in the scene immediately, it is only in the memory. If "UPROPERTY(VisibleAnywhere, BlueprintReadOnly)" is added when the variable is defined, it will only be displayed in the editing interface. If you want It needs to be instantiated to display in the scene, but the instantiation cannot be in the constructor. The code is as follows:

	if (MyActor)
	{
		UE_LOG(LogTemp, Warning, TEXT("My Actor is:%s"), *MyActor->GetName());
		AActor* TempSpawnActor = GetWorld()->SpawnActor<AActor>(MyActor, FVector(100, 500, 100), FRotator::ZeroRotator);
	}
2.3. Dynamically loading resources

        Dynamic loading usually uses the Load method, which is very similar to Unity's method. Usually it is not placed in the constructor. This is different from static loading. The specific code is as follows. The resources loaded here will be displayed in the scene. For example, the code will replace the previously statically loaded resources.

	//动态加载资源
	UStaticMesh* TempStaticMesh = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_NarrowCapsule.Shape_NarrowCapsule'"));
	if (TempStaticMesh)
	{
		MyMesh->SetStaticMesh(TempStaticMesh);
	}
2.4. Dynamically loading classes

Dynamically loaded classes are the same as statically loaded classes. "_C" must be added after the copy reference name. The code is as follows:

	//动态加载类
	UClass* TempClass = LoadClass<AActor>(this, TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'"));
	if (TempClass)
	{
		AActor* SpawanActor = GetWorld()->SpawnActor<AActor>(TempClass, FVector::ZeroVector, FRotator::ZeroRotator);
		UE_LOG(LogTemp, Warning, TEXT("Class Name:"), *TempClass->GetName());
	}

Classes loaded in this way will also be directly presented in the scene.

3. Summary

3.1. Static loading must be implemented in the constructor.

3.2. For statically loaded classes, after copying the reference, "_C" must be added to the end of the file name, otherwise it will cause various problems such as compilation failure or runtime crash.

3.3. Static loaded classes are in memory and cannot be explicitly presented in the viewport or scene. Dynamically loaded classes can.

Guess you like

Origin blog.csdn.net/zhangxiao13627093203/article/details/134932221