Download

How to serialize a variable not UPROPERTY?

I make inventory through a 2d array. Since 2d arrays can not be marked as UPROPERTY, I need to find a way to serialize them somehow differently. I know that I can declare an array of structures, but this method does not really like it and I would like to find another way.

Use TMap<int32,int32> instead of native type.

As far as I know, TMap has a lower element search speed than an ordinary 2d array. I’m interested in the question of how to serialize not uproperty variables

You can’t.
Serializer relies on UProperty reflection system.
If you don’t build your own serialization system for it then the engine can’t do it.
A UProperty somewhere is needed to store these values if you want to use FArchive and Serialize() functions.

Maybe I could try. Where can I start?

Don’t use a 2d array, as long as you know the size in both directions beforehand you can easily use a 1d array with clever indexing.

if you have a 2D array, it’s going to be easier change that for a TMap instead of trying to use UE in a wrong way, the great majority of the systems inside UE are very robust, there is no need to use array]], if you want more than that, you can always use structs inside TArrays, this is the same case of people asking about how to use std:: libs inside UE you simply can’t

The engine absolutely does support serialization outside of the property system. Property serialization is just a small layer built on top of the lower level archive serialization. There are many cases where it’s necessary to do serialization for non-reflected types. You just override UObject::Serialize, call Super::Serialize(Ar), then add:


Ar << MyVariable;

Just be aware that doing this means you need to take care if you will ever need to modify the code and load data serialized previously with an older build.

There’s nothing inherently wrong about using a 2D array. UE4’s property and reflection system has a lot of restrictions caused by implementation limitations; doing something that isn’t supported by the reflection system doesn’t imply misuse of the engine. It just means that you’re giving up some benefits, and should have a clear reason why you’re choosing to do so.

From his post I understood it isn’t a member of any UObject.
In that case you are storing a full object and not just the 2d array which is completely different situation.

Thanks for the answer. I tried to serialize the 2d array this way and I did it! But I’m a little scared that I can break something by interfering with standard serialization.
So I noticed that the editor after changing the variables UPROPERTY always asks if you need to save them. With my non-UPPERPERTY variable this does not happen, and I have to save it by changing some other UPPERPERTY variable. How can I notify the editor that a variable has been changed and serialization needed?

For the purposes of that prompt, UE tracks changes at the package level rather than specific properties. So after you’ve changed something directly, just call this on the containing object:


Object->Modify();

Thank you, it really helped me a lot.