How to save 'AActor' to 'GameInstance'?

Hi

I’m trying to save ‘AActor’ using ‘TArray’

Ex) TArray ArrActor;

The save works fine, but the problem is that when the level is reloaded, the value stored in ‘Tarray’ disappears because it is a reference

So I want to make a copy, not a reference, but I want to know what to do

Thank you for reading the long article and I want to find the answer.

You cant save Actors directly to gameinstance IF was spawned runtime / in game and world get destroyed or new level loaded :slight_smile:
After End play engine will destroy all spawned actors…

What you want to save exactly?

I suggest two possible method for this:

Method 1:
Dont reload your level, if you want restart your level (that means you dont load actually new level), do some restart logic in your gamemode. Set your player back to start position, destroy not needed actors, set everything back to normal and leave actors in world what you need to leave there.

Method 2:
Instead of try to save and reference actors directly you can save needed actor data for respawn. For example make a struct array which contain actor type enum, transformation and other saveable data.
After your level get reloaded, you can iterate through on this array and you can spawn classes based on actor type (with switch), you can set their transformation (location, rotatiom, scale etc.) and you can initialize with some other data which you save in struct array :slight_smile:

Basically you need make a list of actors with all needed data and you can respawn them on restart :slight_smile:
Of course this is proof of concept and maybe other solutions exist, but im sure actor saving directly will never work if level get unloaded and actor was spawned in runtime :wink:

Thanks for the reply!
There is one question.
Can not use TArray in TMap?

Now I want to write

EX)

But it does not compile because it indicates an error that it does not support containers. Do you know any other methods?

Hey.
Create an ustruct which store your array and that struct you can store in tmap :wink: