英文:
TSubclassOf<> isn't storing the class type
问题
我正在尝试创建一个激光束角色,当玩家按下“Fire”动作映射时,它会生成。但我无法让TSubclassOf<>正常工作,以便我可以生成创建激光的角色。我可以声明变量并编译项目,但似乎无法在项目的其他任何地方使用模板,因为该变量未初始化?我将包含我的代码以帮助解释发生了什么。
这是我的LaserTagCharacter.h文件。我已声明了LaserClass变量,我可以毫无问题地编译此代码。
#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()
TSubclassOf<class ALaserTagLaser> LaserClass;
protected:
// Called when the player fires a laser beam
void OnFire();
};
切换到LaserTagCharacter.cpp文件,我已包括一些调试代码和创建模板类的主要目标。我创建了模板类以在SpawnActor函数中使用它,该函数将生成LaserTagLaser的实例。
#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 the player uses the fire action binding
void ALaserTagCharacter::OnFire()
{
UWorld* World = GetWorld();
FVector SpawnLocation = GetActorLocation();
FRotator SpawnRotation = GetControlRotation();
if (LaserClass)
{
UE_LOG(LogTemp, Warning, TEXT("Yes"))
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No"))
}
World->SpawnActor<ALaserTagLaser>(LaserClass, SpawnLocation, SpawnRotation);
}
当我编译、播放并按下“Fire”动作映射时,从输出日志中获得的两条消息是:
LogTemp: Warning: No
以及...
LogSpawn: Warning: SpawnActor failed because no class was specified
如果你对如何修复这个问题以使TSubclassOf<>正常工作有任何见解,那将非常棒!
英文:
I'm trying to create a laser beam actor that spawns when the player presses the "Fire" action mapping. I can't get TSubclassOf<> to work so that I can spawn the actor that creates the laser. I can declare the variable and compile the project, but I can't seem to use the template anywhere else in the project because the variable isn't initialized??? I'll include my code to help explain what's happening.
Here is my LaserTagCharacter.h file. I've declared the LaserClass variable and I can compile this code with no problems.
#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()
TSubclassOf<class ALaserTagLaser> LaserClass;
protected:
// Called when player fires a laser beam
void OnFire();
};
Moving over to the LaserTagCharacter.cpp file, I've included some debug code and my main objective with creating the template class. I created the template class to use it in a SpawnActor function that will spawn an instance of LaserTagLaser.
#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();
if (LaserClass)
{
UE_LOG(LogTemp, Warning, TEXT("Yes"))
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No"))
}
World->SpawnActor<ALaserTagLaser>(LaserClass, SpawnLocation, SpawnRotation);
}
The 2 messages I get from the output log when I compile, play, and press the "Fire" action mapping are:
>LogTemp: Warning: No
and...
>LogSpawn: Warning: SpawnActor failed because no class was specified
If you have any insight on how I can fix this and get TSubclassOf<> to work properly, that would be awesome!
答案1
得分: 2
你的代码看起来没问题,但你应该在头文件中在UPROPERTY内添加一个指定符号,并可能添加一个类别,只需给它一个标题。
UPROPERTY(EditDefaultsOnly, Category = "Setup")
TSubclassOf<class ALaserTagLaser> LaserClass;
在TSubclassOf
之前,这样UPROPERTY
将在虚幻蓝图编辑器中作为下拉菜单可编辑。然后,你必须创建一个基于ALaserTagLaser
的新蓝图。之后,打开下拉菜单,你将只看到ALaserTagLaser
的类和子类。选择你创建的蓝图,在蓝图编辑器中点击编译按钮,然后它应该可以工作...
英文:
Your code seems ok, but you should add in the header file a specifier inside UPROPERTY, and maybe a Category just give it a title
UPROPERTY(EditDefaultsOnly, Category = "Setup")
TSubclassOf<class ALaserTagLaser> LaserClass;
above the TSubclassOf
so that UPROPERTY
will be editable in the blueprint editor inside unreal as a drop-menu. Then you must a create a new blueprint based on the ALaserTagLaser
. After that, open the drop-menu and you will see only classes and childs of classes of the ALaserTagLaser
. Select the blueprint you created, click the compile button inside the blueprintEditor, and then it should be working...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论