Pointer persistence

How do I store a pointer to a Component in such a way that it survives uasset serialization (save asset/close editor/reopen editor) ?

I tried both

UPROPERTY()
UActorComponent* MyPointer;

and

UPROPERTY()
TWeakObjectPtr<UActorComponent> MyPointer;

Those two yield invalid pointers after reload. Note that I assign these pointer in Editor mode, without using Play at all, as part of tooling for building levels. Is there a property keyword I’m missing?

Do you mean a pointer to an object in the world? Typically I set it to EditAnywhere in the UPROPERTY macro. From there I assign the pointer to the object from the editor properties panel with objects that are placed in the world already.

Yes, objects in the world. Interesting that this works for you, I’m pretty sure my first attempt was with a full-blown (EditAnywhere, BlueprintReadWrite, Category=blah). I’ll try it again when I get to my machine.

Let me know where that gets you. Also, there is a SaveGame UPROPERTY attribute you could try using.

Description of SaveGame:
https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Properties/Specifiers/SaveGame/index.html

Ok, you are right, this works now, but only for AActor*, not for Component*. There must be a pointer patching mechanism that only works for some types of objects. I generalized my question a bit too much, I’ll edit it to say Component only.

The perhaps this is something you would be looking for:

I assume you want dynamic UActorComponent generation? I am not too sure on how to do it in the editor… but this might work for gameplay.

Thanks, but yeah the Component already exists, it is owned by an actor also in the World. I hacked my solution by storing both the Owner pointer (an AActor* will serialize properly) plus the name of the Component I’m after (since they are unique in a given actor). Then I got a dumb method that iterates through components until it finds the one with the same name.

This is Editor code, so it will not affect game runtime performance, but I’d like a better solution.

There are many ways to query your Actor for a component it may have. For instance, if you have (an know you will always have) one component of a certain class type, why not just use the following methods listed here in the documentation listed below?

So for the actor pointer you could call UActorComponent* OtherComponent = OtherActor->FindComponentByClass(); Where UActorComponent can be a pointer to any object, or object of a class deriving from, UActorComponent.

I use GetComponents(), then iterate until I find the Name I’m looking for. All these components are of the same class.

It would be cool if the pointer patching was extended to all UObjects, since this is hardly documented.

However, your issue is resolved now right?

Yes, but I’d still like an answer to this question, as my solution is not the answer to it :slight_smile: Or at least other devs looking for that answer will see there is none yet.