I have a TAssetPtr<AActor>
, and it references an actor in an unloaded level. If I have opened the containing level in the editor, then calling LoadSynchronous()
on that Asset pointer will give me a pointer to the actor. If, however, I have not opened the world in the editor, then LoadSynchronous()
returns null.
What do I need to do so that LoadSynchronous()
works?
That’s why they are renamed TSoftObjectPtr
in 4.18.
In the case of my code, spawning the actor anew will not work. It is for an importer plugin and I am trying to maintain a reference to an imported actor from my own asset class that lives outside the level.
With some further testing, LoadSynchronous
does seem to retrieve an actor from unloaded map. Maybe my code is mistaken in some other way.
if there is not an instance of the actor currently spawned, then you cant reference it without spawning it. if you have the actors class, you can get the default values of that class, but to get the changed values from a specific instance of an actor, you need it to be spawned.
you could use objects or structs that hold data for actors that are not spawned yet. they could contain a reference to a class to spawn from, as well as other initialization data for that actor. I use objects like this for handling inventory items, so i can have the item data in menus, without spawning the actor.
I figured it out. It turns out calling TAssetPtr<AActor>::LoadSynchronous
actually does work.
Before 4.18 an asset pointer pointing to an Actor in any map other than the editor’s current map will show up as “None”, same as if it was unassigned, even if the asset is loaded. This made diagnosing the problem difficult. Right clicking on the field and choosing Copy will still allow you to get the long path name, which looks something like /Game/FooMap.FooMap:PersistentLevel.ModelRootActor
.
The problem was actually rather simple. I was forgetting to call Modify on the object containing the asset pointer property. Then when I hit Save All, the asset pointer still had a long path name that referred to a nonexistent actor.
Thank you very much for your help! It turns out you can reference an actor in a UWorld (e.g. a StaticMeshActor that you’ve placed in the editor, and then saved the resulting map) with an asset pointer, even in UE4.16. I’m not sure if you’d call such an actor “spawned” or not since “SpawnActor” was called in a previous run of the engine then the actor was saved to disk.