在 Unreal 中使用 stl 容器
Using stl containers with Unreal
我可以使用 std::vector、std::unordered_map 代替 TMap、TVector 等吗?
我需要一个 class 来序列化它的所有信息,并且我有已经可以与 stl 一起使用的代码。
我测试过尝试对它们使用 UPROPERTY,但这是不可能的(有什么办法吗?)。但我同意。
我需要知道的是序列化整个 class 时会发生什么,其中一些成员是 stl 容器。
你不应该使用它们。
You should not use standard C++ library for other then communicating with external libraries and if you use them you should avoid using UFUNCTION and UPROPERTY on them.
https://answers.unrealengine.com/questions/180421/how-to-use-c-standard-library-std.html
如果需要序列化属性,我建议使用Rama的系统(UE Wiki)。我在我的游戏中使用它来 save/load 数据到二进制文件,但它可以很容易地扩展到通过网络发送数据。
基本上,在该系统中,您正在重载 <<
运算符。优点是您可以在一个地方定义所有结构,并且可以使用一个函数来保存和加载。 注意 我在尝试编译如下代码时遇到了编译问题:
TArray<UObject> array = // init
// ar is container to serialization
ar << array;
但我通过将 UObject
s 映射到 UStruct
s 来解决它,所以代码像
TArray<FStruct> array = // init
// as is container to serialization
ar << array; // now, this will compile
是UE 4.15的,可能以后版本解决了
如果你想更多地了解我对拉玛系统的实现,你可以看看我的游戏仓库TCF2 (GitHub)。您正在寻找 /Source/GameSave/[Public|Private]/SaveGameCarrier
- 这是我主要的 class 用于保存。可以在 /Source/Inventory/Public/InventoryHelpers.h
中找到映射助手的示例
我可以使用 std::vector、std::unordered_map 代替 TMap、TVector 等吗?
我需要一个 class 来序列化它的所有信息,并且我有已经可以与 stl 一起使用的代码。
我测试过尝试对它们使用 UPROPERTY,但这是不可能的(有什么办法吗?)。但我同意。
我需要知道的是序列化整个 class 时会发生什么,其中一些成员是 stl 容器。
你不应该使用它们。
You should not use standard C++ library for other then communicating with external libraries and if you use them you should avoid using UFUNCTION and UPROPERTY on them. https://answers.unrealengine.com/questions/180421/how-to-use-c-standard-library-std.html
如果需要序列化属性,我建议使用Rama的系统(UE Wiki)。我在我的游戏中使用它来 save/load 数据到二进制文件,但它可以很容易地扩展到通过网络发送数据。
基本上,在该系统中,您正在重载 <<
运算符。优点是您可以在一个地方定义所有结构,并且可以使用一个函数来保存和加载。 注意 我在尝试编译如下代码时遇到了编译问题:
TArray<UObject> array = // init
// ar is container to serialization
ar << array;
但我通过将 UObject
s 映射到 UStruct
s 来解决它,所以代码像
TArray<FStruct> array = // init
// as is container to serialization
ar << array; // now, this will compile
是UE 4.15的,可能以后版本解决了
如果你想更多地了解我对拉玛系统的实现,你可以看看我的游戏仓库TCF2 (GitHub)。您正在寻找 /Source/GameSave/[Public|Private]/SaveGameCarrier
- 这是我主要的 class 用于保存。可以在 /Source/Inventory/Public/InventoryHelpers.h