How to simply spawn Actor variable?

Some noob question sorry, but can’t find it anywhere.

I know there is a popular SpawnActorFromClass function, but that will not work for me. Instead, I need something even more simpler - spawn actor directly from existing variable in Blueprints:

So I wouldn’t have to specify not the Actor class, not it’s Transform(variable already have all those things) and so on.

It is saved Actor from the previous game session which I store in SaveGame.
I can save this Actor well.
I can destroy such Actor when BeginPlay.
But I can’t spawn it back…

1 Like

You have to spawn from class. The class itself should have exposed variables so you can pass in the previous instances data.

2 Likes

As far as I know you can’t directly restore an object instance from SaveGame, but like Rev0verDrive says you’d have to spawn a new instance from class (blueprint node == purple, not blue), then write the data from the SaveGame back into the new instance. I’d write a method for that class to be initialized with save game data, instead of publicly exposing properties. That takes some time to do but then you can make sure instances are only initialized once and that those properties are not being modified later on when they shouldn’t be.

// Public method to initialize private properties once:

UMyActor::InitializeMyActor(const FS_MyActorSaveData& InData) {
  check(WasInitializeMyActorCalled == false);
  WasInitializeMyActorCalled = true;

  // Initialize instance from InData
}

I remember there’s also a way to mark a blueprint property as “exposed on spawn” or something, I can’t remember the last time I used that. It bugs entirely on widgets (props not set during… OnInitialized I think?), and I avoid blueprints when I can anyway.

2 Likes

There’s also the issue of actor obj references in save game not storing any modified values outside of parent class declared variables… Transform etc. Custom variables will revert to defaults. Customs have to be stored in a SG struct.

I store specifics in custom structs and have events/functions tailored to spawning them. Simple data passes I do with exposed variables. Complex I do manual sets using the spawn return reference.

1 Like

there’s quite some info about this.
here you can see a few other forum posts and some video tutorials.

2 Likes

(post deleted by author)

1 Like

Not sure I fully understand what you meant here. Can you provide an example?

If there would be such a possibility it would be great. But how exactly you can write in the data? I still don’t understand why it is not possible to replace entire Object reference with another one, ■■■■ it feels obvious…

Just checked, it’s real. That just ruined my entire ‘simple’ plan to achieve Save/Load actors :frowning:

To simply spawn an Actor variable in Unreal Engine (usually in Blueprints or C++), you can follow these basic steps depending on the context:


:small_blue_diamond: In Blueprints:

  1. Use the “Spawn Actor from Class” node:

    • Right-click in the Event Graph and search for Spawn Actor from Class.
    • Set the Class input to the Actor class you want to spawn.
    • Set the Spawn Transform (Location, Rotation, Scale).
    • Optionally set Spawn Parameters if needed.
  2. Store the spawned Actor in a variable:

    • Drag off the return value of the Spawn node and promote it to a variable.
    • This is your Actor variable.

    Example:

    Spawn Actor from Class → Promote to Variable → "MySpawnedActor"
    

:small_blue_diamond: In C++:

FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();

FVector SpawnLocation = FVector(0.f, 0.f, 100.f);
FRotator SpawnRotation = FRotator::ZeroRotator;

AMyActor* SpawnedActor = GetWorld()->SpawnActor<AMyActor>(AMyActor::StaticClass(), SpawnLocation, SpawnRotation, SpawnParams);

Make sure AMyActor is the type of Actor you want to spawn, and that you include the appropriate header.


:white_check_mark: Summary:

  • Use Spawn Actor from Class in Blueprints and promote the output to a variable.
  • Use GetWorld()->SpawnActor<>() in C++ and store the return in an Actor pointer.

Let me know if you need an example for a specific type of Actor or use case (e.g., spawning with delay, from another class, etc.)!

Thanks for your extensive reply, but you provided here just a basic stuff I already knew before creating this thread. Read the thread, I need to Save and Load Actor in it’s entirety, not just the Transform.

By entirety I mean all of it by time it was Saved. All kind of variables states, so it would load not a ‘fresh new’ Actor with just correct position, but with correct everything.

But after a day digging on that I think I won’t get it so simple. There is a hundreds SaveGame products on Marketplace with different prices, no free such stuff I think. Which are do that thing I described here - save all wanted Actors and Load them out automatically.

Looks like an AI. lots of suspicious profiles are invading again. Some people trying to be helpful with AI content, but most with few posts and bots. If it’s not EPIC, bots here are up to no good. Watch out with links.

I write methods to initialize my fresh object instances with the data they need, public method to initialize private properties once. Pics for example (ignore that it’s widget example):

Image gets passed to Child Widget, but String does not - #8 by Roy_Wierer.Seda145

C++ example I posted in post 3:

How to simply spawn Actor variable? - #3 by Roy_Wierer.Seda145

I think it is at runtime, but I haven’t worked with save game systems enough to tell if a fully copy is possible in that system. Usually that would be overkill and lead to huge files. Like, how would you in such case tell the system what not to write to it? For example, an exact snapshot would contain exactly where you are in a character animation, or exactly how your player camera is transitioning from A to B, a lot of data which is useless in case A but required for someone else in case B. Those properties are usually filled in automatically when running the program and have no need for 1:1 storage. The data that we do want to store isn’t always (hopefully) that much. You’d get away with storing just a spawn point for actors in many games. If games back in the day of PS2 were to store full snapshots at any time 1:1 it wouldn’t fit on a memory card.

https://wiki.pcsx2.net/Savestate

I tested a save game system from the marketplace a few years back, maybe Savior, maybe not, and wasn’t impressed. Because, it claimed to be such an automated process “hit buttons: save state, load state, done” but I came to the exact same conclusion. I had no idea how much data was pushed into the save game, why I would need the framework if I was going to end up manually controlling it, and why for example, I loaded the save game and ended up with a camera being attached in completely the wrong position (probably pushed into the savegame from a transition state). What I remember is that I asked this question to the creator and got the reply that that wasn’t automated :sweat_smile: . Well… Obviously I didn’t try such framework again. Currently I just write small structs of data to SaveGame (like a player’s settings) or even to INI if I want it to be editable as text.

1 Like

From all your good points, this one is concluded it all.

I also checked my Actors at runtime with the breakpoint and looked inside, like ALOT of stuff in there, underestimated that. Seems like a really bad idea to save entire actor, already retreated from such a plan. Will make manually Save/Load functionality just for the properties I need.

And btw, I think yesterday I found it anyway(not sure, whole answer of this guy is unexplored area for me):

1 Like

Serialization would be the way to go if you absolutely NEED to store the entire actor. Yet, you cannot do this in BluePrint. Has to be done in C++.

Every Actor in the game world is stored in memory (RAM). References are pointers to the memory that’s holding the data. Think of pointers as addresses to specific boxes holding specific folders and files.

When you Save Game you’re storing specific memory data to a flat file. Data is literally being written to a file on your drive. When the game closes all the “memory” data is cleared. This means the pointers/references no longer exist.

When designing your game you need to think about subsystems and functionality that are going to be implemented down the road. The design stage, preferably scope stage, is where you tweak and plot based on future needs.

How do I simplify this actor for SaveGame functionality? What aspects can I isolate/modularize to make this heavy process easier and faster down the road. End of day I want to store the smallest amount of data possible.

Take for example the character. You do not need its movement velocity or any other movement related states. You just need a transform. Everything else movement wise should be defaulted, so when you spawn back in you’re simply standing at the last place you where and looking in the correct direction.

Character cosmetics, if any, should be a very simplified data format. Mainly ID’s (Name type) that can be used to look up data table entries for the heavy data.
e.g. Pants = Mblue_shorts / FPink_shorts, Shirt = MTango_top / FGrunge_topb etc
This could easily be packed in a small struct.

Character Inventory should be a Player State actor component. The SG version should be a pretty close mirror to the PS version except for Object References. They would require a new struct having an element entry for each specific obj reference.

Example of my inventory system and the SG struct I would create to store a copy.
My inventory is a named slot based setup. “Data Inventory” is a struct that uses named slots to hold quantity counts. Simple and fluid. All other inventory items are spawned and attached actors, thus Actor Obj References. If they are null the slot is empty.

Save Game Inventory is only holding absolute crucial information.

1 Like