我正在 UE4 中制作激光标签游戏,但使用 TSubclassOf 时遇到很多困难。
首先,我在 LaserTagCharacter.h 文件中声明 LaserClass,如下所示。我还创建了一个名为 OnFire() 的函数,当玩家使用“Fire”动作绑定时会调用该函数。
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "LaserTagCharacter.generated.h"
UCLASS()
class LASERTAG_API ALaserTagCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values
ALaserTagCharacter();
UPROPERTY(VisibleAnywhere, Category = "Laser")
TSubclassOf<class ALaserTagLaser> LaserClass;
protected:
// Called when player fires a laser beam
void OnFire();
};
现在我想实现 OnFire() 函数,以使用 SpawnActor 函数从 ALaserTagLaser 类创建激光。我就是这样做的。
#include "LaserTagCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "LaserTagLaser.h"
#include "Engine/World.h"
// Sets default values
ALaserTagCharacter::ALaserTagCharacter()
{
}
// Called when player uses fire action binding
void ALaserTagCharacter::OnFire()
{
UWorld* World = GetWorld();
FVector SpawnLocation = GetActorLocation();
FRotator SpawnRotation = GetControlRotation();
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
World->SpawnActor<ALaserTagLaser>(LaserClass, GetActorLocation(), GetActorRotation(), SpawnParameters);
}
所有这些代码都会编译,但是当我单击播放、打开输出日志并使用我的“Fire”操作绑定时,我收到此错误。
LogSpawn: Warning: SpawnActor failed because no class was specified
我很困惑,因为我明确指定了我想要生成的类。如果有人能提供一些见解那就太好了。