[UE4 C + +] generate character within the specified area

preface

This article will tell you to generate roles within a specified range.

Create C + + files

Right click the blank area of the folder, create a new C + + file, and inherit the Actor class.

. h file

Adding a box component is mainly used to specify the creation range.

UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
		class UBoxComponent* SpawningBox;

Declare a function that creates random points.

UFUNCTION(BlueprintPure)  
	FVector GetSpawnPoint(); 

Here, let's talk about the difference between BlueprintPure and BlueprintCallable we saw before.

Declares the type of element created

UPROPERTY(EditAnywhere, BlueprintReadOnly)
		TSubclassOf<class ARole> PawnToSpawn;

Generally, when defining an object attribute, we will directly write it like UClass* DamageType, but the official recommends a tsubclassof < > What are the benefits of writing it like this? If we write it like this, our choices will become diversified. As long as it is a subclass of this class, it can be put into our attribute, that is, if we want to do a function in the future, Generate different enemies at the specified location, then we can extract the common points of the enemy and make a base class. All enemies inherit this base class. In this way, our choices will be diversified.
For the current function, I generate a role class, and then a blueprint inherits it, then our editing interface will appear:

Declare the function that generates the role class.

UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
	void SpawnMyPawn(UClass* PawnClass, FVector const& Location);

A new function tag BlueprintNativeEvent appears here. We compare it with blueprintimplementationableevent to promote our understanding:
Blueprintimplementationableevent: a function can be declared in C + + (it cannot be defined, but the blueprint is rewritten). The function is called in C + +, and the blueprint rewritten implements the function.

BlueprintNativeEvent: a function can be declared and defined in C + +, called in C + +, and the blueprint rewrites and implements the function (the blueprint can rewrite or not rewrite the C + + parent function), but when it is implemented in cpp, its name should be in accordance with [FunctionName]_Implementation is named instead of [FunctionName].

. cpp file

< constructor >
Add header file: #include "Components/BoxComponent.h"
Create our box instance in the constructor.
SpawningBox = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawningBox"));

<GetSpawnPoint()>
Add header file: #include "Kismet/KismetMathLibrary.h"
The RandomPointInBoundingBox function will randomly generate a location in this area according to the location and size of the box.

FVector ASpawnVolume::GetSpawnPoint()
{
	FVector Extent = SpawningBox->GetScaledBoxExtent(); // Get the length, width and height of the box
	FVector Origin = SpawningBox->GetComponentLocation();  // Get the location of the box
	FVector Point = UKismetMathLibrary::RandomPointInBoundingBox(Origin, Extent); // Generate random location
	return Point;
}

< spawnmypawn implementation >
Add header file: #include "Role.h" this header file is the class of the element you want to prevent.
Get the world, and then place our class instance at some point in the world.

void ASpawnVolume::SpawnMyPawn_Implementation(UClass* PawnClass, FVector const& Location)
{
	if (PawnClass) {
		UWorld* MyWorld = GetWorld();

		if (MyWorld) {
			ARole* Create = MyWorld->SpawnActor<ARole>(PawnClass, Location, FRotator(0.f));
		}
	}
}

Blueprint implementation

Finally, according to this connection method in the blueprint, the function of generating character s in the specified area can be realized.

This role should be created at the beginning (call the SpawnMyPawn function). The role is obtained from (pawntospan) and the location is obtained from GetSpawnPoint.

Keywords: C++ Game Development UE4 visualstudio

Added by MidOhioIT on Mon, 10 Jan 2022 23:09:53 +0200