我正在尝试创建一个自定义类,然后可以在我的蓝图中使用它。我需要这个类来保存玩家信息,比如姓名和他们图片的路径。到目前为止我所做的没有编译或构建没有错误,我不知道如何修复它,因为我从未使用过它
header file #pragma once
#include "Object.h"
#include <iostream>
#include "PlayerClass.generated.h"
/**
*
*/
UCLASS()
class PROTOTYPE2_API UPlayerClass : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Switch Variables");
string playerName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Switch Variables")
string playerTeam;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Switch Variables")
string picPath;
UPlayerClass(const FObjectInitializer& ObjectInitializer);
UFUNCTION()
void importPic(string picPath);
};
.cpp 文件
#include "Prototype2.h"
#include "PlayerClass.h"
UPlayerClass::UPlayerClass(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
playerName = "";
playerTeam = "";
picPath = "";
}
void UPlayerClass::importPic_Implementation(Fstring picPath)
{
}
最佳答案
我发现我的 importPic 函数需要
UFUNCTION(BlueprintCallable, Category = "Import")
virtual void ImportPic(const FString& Path);
并且根据您是否希望您的类显示为蓝图变量或能够成为蓝图类,您可以将顶部标题更改为
UCLASS(BlueprintType) // for blueprint to show up as variable
UCLASS(Bleuprintable) // for blueprint to be able to be made as a blueprint class
构建和编译的代码:
头文件
#pragma once
#include "Object.h"
#include "PlayerClass.generated.h"
UCLASS(BlueprintType)
class PROTOTYPE2_API UPlayerClass : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Variables")
FString playerName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Variables")
FString playerTeam;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Variables")
FString picPath;
UPlayerClass(const FObjectInitializer& ObjectInitializer);
UFUNCTION(BlueprintCallable, Category = "Import")
virtual void ImportPic(const FString& Path);
};
.cpp -
#include "Prototype2.h"
#include "PlayerClass.h"
UPlayerClass::UPlayerClass(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
playerName = "";
playerTeam = "";
picPath = "";
}
void UPlayerClass::ImportPic(const FString& Path)
{
}
关于c++ - 如何创建与蓝图一起使用的自定义 C++ 类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31276794/