Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expansion regarding the serialization of a struct #134

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions manual/scripting/cpp/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ if (!document.HasParseError())
To implement data serialization for custom native type or custom data container add 3 methods in `Serialization` namespace as in an example shown below. It can be used for defining specialization implementation for template types too.

```cpp
struct MyCustomNativeData
API_STRUCT(NoDefault) struct MyCustomNativeData
{
Vector3 Direction;
float Length;
// This, combined with API_STRUCT(NoDefault) allows this type
// to be used in scripting environment (C#, Visual Script, etc)
DECLARE_SCRIPTING_TYPE_STRUCTURE(MyCustomNativeData);
// Expose all the fields to the scripting api with API_FIELD()
API_FIELD() Vector3 Direction;
API_FIELD() float Length;
};

// C++ doesnt allow us to implement automatic serialization for structs with something convenient like API_AUTO_SERIALIZATION();,
// so in this case we have to manually tell it how to serialize and deserialize our data.

#include "Engine/Serialization/Serialization.h"
namespace Serialization
{
Expand All @@ -66,6 +73,7 @@ namespace Serialization
}
inline void Serialize(ISerializable::SerializeStream& stream, const MyCustomNativeData& v, const void* otherObj)
{
// Populate stream with the struct data
stream.JKEY("Direction");
Serialize(stream, v.Direction, nullptr);
stream.JKEY("Length");
Expand All @@ -78,6 +86,16 @@ namespace Serialization
DESERIALIZE_MEMBER(Length, v.Length);
}
}

// If you want your struct to be compatible with automatic binary serialization (you would need that if you are planning
// to mark your struct with NetworkReplicated tag, as an example), then you should also add this template. This helps
// Read/WriteStream in deducting which binary serialization strategy to use for your struct (in our case we would want it as a POD struct).

template<>
struct TIsPODType<MyCustomNativeData>
{
enum { Value = true };
};
```

If your code needs to auto-serialize the custom data type field or property but it should be not exposed to scripting (as it can be C++-only) use `Hidden` attribute.
Expand Down