sinbad / SPUD

Steve's Persistent Unreal Data library
MIT License
300 stars 44 forks source link

Level data for actor ref in components - is this fix appropriate? #54

Closed thomas-oconnell closed 1 year ago

thomas-oconnell commented 1 year ago

Hello, I hope you're well, and thank you for your fantastic work on this plugin.

I have an Actor Component (InteractionComponent) that handles interactions for the player, including holding an object (in this case, a level-placed actor). I store a reference to the held object actor (HeldObjectActorReference) in the InteractionComponent. After saving with a held object attached and then reloading, HeldObjectActorReference would be lost because Level data couldn't be found when working on the actor reference, stored within InteractionComponent, stored within the player (at least, this was seemingly what I was running into!).

In SpudPropertyUtil::RestoreContainerProperty, I changed:

if (auto Actor = Cast<AActor>(RootObject))
    Level = Actor->GetLevel();

To:

if (auto Actor = Cast<AActor>(RootObject))
{
    Level = Actor->GetLevel();
}
if (!IsValid(Level))
{
    if (UActorComponent* ActorCompLevelCheck = Cast<UActorComponent>(RootObject))
    {
        Level = ActorCompLevelCheck->GetOwner()->GetLevel();
    }
}

This seems to have resolved the issue, and the player retains the actor reference in the component upon reload. I wanted to ask - will this potentially introduce problems elsewhere?

At this time, the InteractionComponent is marked with SaveGame within the player class, as is the HeldObjectActorReference property in InteractionComponent.

sinbad commented 1 year ago

Hi, thanks for raising this and figuring out a potential solution. I'm not at work for a few days but I can't see how this would do any harm since it's just adding an additional fallback - even though I don't entirely understand why it works without looking at the code closer. It's probably the timing of when the character is restored vs the level?

I don't believe it will cause any problems, perhaps you could submit it as a pull request, that way you'll get the credit for it 😃 I'll take a closer look and merge it when I'm back in the office.

Cheers Steve