Download

Is it possible - Play mode add to scene

Is it possible to add assets into a scene when in play mode.
I mean when I play and get the need to add an asset near my location. I hit the ‘F’ key for example and it place a cube there. When I stop the game the cube is still there as an asset.
How would you blueprint that?

Hello,
You can use “spawn actor from class” to add an actor into a scene. You can “set” a component or a child actor too.

Yes but as soon as you end the game this actor is gone.

You can’t unfortunately (not via blueprints anyway).

A function that spawns an actor in both editor and game world sounds like a useful function to have though.

I will try to make a C++ function for this when I have time.

Thanks I would appreciate that.
My thought on this was that we could make our projects evolve with this functionality. I mean you start with the tools you’re given and end with you building your game entirely in the game while it plays. I love the idea of building my own tools to speed up the development process the more I work with it.

That would be pretty handy actually. There is the “save changes” during runtime ability when you press the “K” key, which I have had mixed results with, but it will also only save changes to existing items, not newly spawned 1s

I’m not sure if this still works, but you used to be able to spawn objects at runtime, select them in the outliner, copy them and then stop the game. Then you could paste them back in. Does this still work?

Oh, i didn’t understand at first read, sorry. I wonder if you could use an instance to save added elements in an array or struct and save this on quit, then use the savegame file to extract added infos. Only an idea on the fly.

No worries mate,
I have not looked into the save functionality yet but if I would be designing it I wouldn’t do read and write as a binary format but instead use XML. This way anyone could parse the file and work with the data to build new innovative functions.

You can use the savegame method to save objects then re-spawn them on BeginPlay but you are still not going to get the spawned object into the “editor world”.

So… I was able to make a simple C++ function that can spawn at runtime an object in the editor instead of the game world.

.h


	UFUNCTION(BlueprintCallable, Category = "Game", Meta = (FriendlyName = "Spawn Actor from Class in Editor"))
	static void SpawnActorInEditor(UClass* Class, FTransform SpawnTransform, AActor* &Result);

.cpp


#include "UnrealEd.h"

void UMyFunctionLibrary::SpawnActorInEditor(UClass* Class, FTransform SpawnTransform, AActor* &Result)
{
	if (Class && GEditor)
	{
		Result = GEditor->AddActor(GEditor->LevelViewportClients[0]->GetWorld()->GetLevel(0), Class, SpawnTransform);
	}
}

In your Build.cs you will need to add “UnrealEd” in the PublicDependencyModuleNames.

(Let me know if you don’t know your way around C++ and I will try to help)

82d5c8afd648355e7dbcf244da5ef900fb80f5de.jpeg

This will spawn the object in the editor only (not visible at runtime) so you can combine this with the default SpawnActorFromClass node if you need to see what you are spawning in game :stuck_out_tongue:

It’s not perfect though and it has it’s limitations… it doesn’t expose any “Expose on Spawn” variables when you select the class for example (like the SpawnActorFromClass does).

That is quite simple! Would be good to see how far we can push this kind of thing to allow full in game level editors essentially.

Yup! there is tons of potential with this (kinda surprised this was not done already :p)