英文:
How do i use parameters with constructors?
问题
我在UE5中编程,创建了一个空间哈希。我需要为哈希声明一个特定的单元大小,并决定使用构造函数。
以下是类似的代码:
HSpatialHash.h
UCLASS()
class XXX_API UHSpatialHash final : public UObject
{
GENERATED_BODY()
public:
explicit UHSpatialHash(const float PCellSize) { CellSize = PCellSize; }
}
错误信息:
错误 C:\Users\user\OneDrive\Documents\Unreal Projects\attrition\Source\project name\Private\HSpatialHash.h(18):
错误 C2338:static_assert 失败:'You have to define UHSpatialHash::UHSpatialHash() or UHSpatialHash::UHSpatialHash(const FObjectInitializer&). This is required by UObject system to work correctly.'
我尝试添加FObjectInitializer参数,但没有效果。我尝试将其放在cpp文件中,仍然没有效果,我不知道该怎么办。你能帮助我吗?
英文:
I am programming in ue5 and i was creating a spatial hash. i needed to have a declared cell size for the hash, and i decided to use the constructor
Here is a similar code
HSpatialHash.h
UCLASS()
class xxx_API UHSpatialHash final : public UObject
{
GENERATED_BODY()
public:
explicit UHSpatialHash(const float PCellSize) {CellSize = PCellSize;}
}
Error message:
error C:\Users\user\OneDrive\Documents\Unreal Projects\attrition\Source\project name\Private\HSpatialHash.h(18):
error C2338: static_assert failed: 'You have to define UHSpatialHash::UHSpatialHash() or UHSpatialHash::UHSpatialHash(const FObjectInitializer&).
This is required by UObject system to work correctly.'
I tried to add the FObjectInitializer parameter, but it didn't work. I tried to put in in the cpp file, nothing, i don't know what to do. Can you help me?
答案1
得分: 0
如果您继承自UObject,您必须提供默认构造函数或带有FObjectInitializer
参数的构造函数。您可以使用NewObject<UHSpatialHash>()创建对象,该函数会为对象执行一些内部初始化逻辑,无法向其传递参数。
我决定使用构造函数
根据您的需求,您可能应该更改您的决定。
您是否需要它成为具有反射和编辑器可见性的UObject
?如果是这样,最好只需添加一个带有您想在构造函数中传递的参数的“Initialize()”方法,并在创建后调用它,如下所示:
UCLASS()
class xxx_API UHSpatialHash final : public UObject
{
GENERATED_BODY()
public:
inline void Initialize(const float PCellSize) { CellSize = PCellSize; }
}
以及创建对象的方式:
UHSpatialHash* spatialHash = NewObject<UHSpatialHash>(this);
spatialHash->Initialize(cellSize);
如果您只希望它成为一个简单的类而不具备反射功能,您可以使用常规的C++类并使用自定义构造函数。
另一种选择是使用USTRUCT()
。它不能做与UCLASS()
一样多的事情,但由于您不打算从中继承,这可能已经足够了。您可以定义自定义构造函数,而无需考虑FObjectInitializer
参数。
英文:
If you inherit from UObject, you have to provide either a default constructor or the one with the FObjectInitializer
parameter. You create the object using NewObject<UHSpatialHash>() which does some internal initialization logic for the object and you can't pass parameters to it.
> i decided to use the constructor
Depending on what you want to do, you probably should change your decision.
Do you need it to be a UObject
with reflection and editor visibility? Because then, it would be easier to just add an "Initialize()" method with the parameters you wanted to have in the constructor and call it after creation, like so:
UCLASS()
class xxx_API UHSpatialHash final : public UObject
{
GENERATED_BODY()
public:
inline void Initialize(const float PCellSize) { CellSize = PCellSize; }
}
and for creation:
UHSpatialHash* spatialHash = NewObject<UHSpatialHash>(this);
spatialHash->Initialize(cellSize);
If you only want it to be a simple class without reflection, you can just use a regular C++ class and use your custom constructor.
Another option would be to use a USTRUCT()
instead. It can't do as much as the UCLASS()
, but since you do not intent to inherit from it, it may be enough. You can define a custom constructor and do not need to care for the FObjectInitializer
parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论