after upgrading to UE5 from 4.27 i noticed that my save games did not work any more. after some digging i found out that the deserialization of actors invalidated their scene component hierarchy. this results in all components being at root level and also duplicates being created. finally leading to crashes and other nasty behavior.
here is the save code
TArray<uint8> ObjectData;
FMemoryWriter MemoryWriter(ObjectData, true);
FObjectAndNameAsStringProxyArchive Archive(MemoryWriter, true);
Archive.ArIsSaveGame = bOnlyPropertiesWithSaveFlag; //when true, only SaveGame flagged properties are saved
MemoryWriter.SetIsSaving(true);
Obj->Serialize(Archive);
//then write the buffer to disk
and here is the loading code
TArray<uint8> ObjectData; //get loaded from disk before
FMemoryReader ActorReader(ObjectData, true);
FObjectAndNameAsStringProxyArchive Archive(ActorReader, true);
ActorReader.SetIsLoading(true);
Object->Serialize(Archive);
i found out that setting Archive.ArIsSaveGame = true fixes the problem as it makes the serialization only save the properties that are flagged as SaveGame. however if possible i would want to save everything.
does anybody know another way around that? or is there another, new way of serializing objects in UE5 that should be used instead? which properties of the actors are overwritten that cause this behavior? so many questions ^^
It’s not immediately obvious to me where in AActor::Serialize(FArchive& Ar) that the actors “scene component hierarchy is being invalidated”. How do you know this is happening? Where did you see it documented that this is what is happening?
well, in my application i load a “game” by first loading it’s original map, then deserializing all objects. this deletes objects that were deleted by the user and creates those who were added as well as change properties.
to find out where the problem lies, i simply commented out the line with the “Serialize” and everything was fine. of course the changes made by the user were not loaded but the hierarchy was fine. (same for Archive.ArIsSaveGame = true)
with the line enabled, i had the broken hierarchy. for instance i have a camera in the map with Actor->SceneComponent->CameraComponent. this was loaded as Actor->SceneComponent, Actor->SceneComponent and Actor->CameraComponent. so the scene component seemed to be duplicated and the camera component was at the wrong place.
note that this counts for the root actor as after this call all child components get serialized as well. as far as i could see, this has no effect.
to sum it up, my assumption is based solely on the fact that commenting out the line makes the problem disappear.