Spawning actors during level load

I would like to be able to spawn an actor once my level loads, such that this actor appears in the level as if it had been saved with the level. In other words, I don’t want to spawn the actor in response to a begin play event, a keypress, or any other hook that requires the game to be running or simulating. I want it to be created at some point during the actual level load and be present in the editor just like an actor that I had manually placed in the level.

Additionally, I do not want this spawned actor saved with my level when I select Save in the editor.

I understand how to create blueprints and spawn actors. I just can’t seem to figure out where this particular behavior can be implemented. All of the examples I can find for dynamically spawning actors do so in response to a begin play, keypress, or similar event. Any ideas?

with unreal engine, unfortunately, you’d have to do some “mind trick”.
the reality is in the engine, level load finished before you can spawn any object that could be destroyed, that means player, barrels etc.
so what all devs do are just putting a loading screen that blocks your view, while everything is busy doing their thing, and then fade in/switch to gameplay screen.
BeginPlay is the most convenient event for this, how you want to approach it though is up to you.
(say only possess after everything is done, the camera will then snap to the one from your pawn)

Unfortunately one of the requirements for what I am trying to do is that these dynamic actors need to be visible in the editor as well as the game (they are algorithmically generated based on data available outside the engine at the time the level is loaded). So that “mind trick” won’t work for my situation :frowning: If there was an equivalent of BeginPlay for the editor I might be able to make something like that work - I don’t even care about hiding it visually from the user of the editor, I just need a place to hook into it.

Thanks for your insights.

Guess I wasn’t aware of your actual question to load level with actors in Editor.
Yes then it is possible to load character with preset state in editor, you just have to save it with the level that’s it.
If you see the outliner on your right, all the white text named ones gets load to editor, if you are in play mode, all the blue text named ones are spawn dynamically.
You can set your pawn to default pawn that way it will not spawn anything crazy when you hit play/simulate, and you can later possess or do what ever you want.

simple way would be drag and drop from your Asset browser into scene, and it will be consider a permanent level object.
If you want to generate a lot of stuff in editor’s editing mode( not play in editor), then you might need to check the scripting/c++ side of editor.
they are just callbacks really, so shouldn’t be too hard to find.

Sorry, we seem to be talking in circles a bit. As I stated in my original post, I do not want these objects saved into the level. I want to dynamically generate them upon level load regardless of whether you are working in the editor, playing in the editor, or running the game. The only mechanism to dynamically generate them seems to be when playing in editor or playing in the game (e.g. BeginPlay), but then you do not see them when just working in the editor.

Conceptually, this doesn’t seem like something that should be hard to do. Unreal can save objects into a level, it can load those objects when the level is initially loaded, so after that point why couldn’t I add some more of my own dynamically? That is disappointing.

Sounds like it’s not possible with Blueprints. Bummer :frowning: I guess I’ll look into the C++ side of things. Appreciate your effort though - thanks!

actually your statement is kinda contradict itself by current engine/editor framework.
see, there are 3 states in your last post:

  1. editor, where you can create permanent level object(either manually or through script), spawn subobject with construction script from a blueprint, edit them. anything that is white text named will be saved with map, unless you choose to delete them before you save, or have some editor plugin that save them into a data file that you can load them back.
  2. play in editor, where you can spawn objects dynamically where in construction script or from event flow, but level itself won’t be changed unless you manage to save a state
  3. play as published game, you can’t change the level in a significant way(at least not from stock engine), Rama did a lot for in engine editing, which is sort of a way to not let his future customer have to subscript to UE4.

So yeah, with stock engine, it is not possible to achieve what you want. You’d have to modify the engine quite a lot to achieve what you need.

Why do you need them in the editor though? If they are dynamicaly created when loaidng the level what meaningful actions can you do with them in the editor, won’t anything you do with them be lost when you hit play?

Is this just to get some form of visual feedback while editing the level?

Why would they be lost when you hit play? Currently, if I manually place an actor in a level, save that level, and hit play the actor stays in the level. Instead of manually placing the actor and saving it into the level, I just want to create it during (or right after) the level data is loaded.

More specifically, when a level is done loading I query an external database. Based on the results of that query, I add certain actors to the level. I consider those actors to be part of the level, even though the information used to generate them is not stored with the level data. I want them to be visible when editing the level, and changing certain properties on them would write information back out to the database. When the level is saved, these actors are not saved as part of the level data - the information used to recreate them only exists in the external database.

Regardless, all I really want is an event that I can hook into that fires when the level data is done loading, so that I can add my actors to the level and have them exist just like a hand-placed actor in a level. This doesn’t seem like a strange thing to want to do, and I can come up with all kinds of scenarios where this functionality would be useful. I’m surprised such a thing doesn’t exist.

I did come across a delegate that can be added to the level blueprint called “OnLevelLoaded_Event”. The name implies it would be what I want, but I cannot find any information on it anywhere so I don’t know exactly what it does or how you would use it. I’ve tried hooking it up, but it doesn’t seem to be getting called.

I would create a blueprint actor spawner that queries the database and places the desired actor at location in the level. You could add mesh component placeholder for visual representation in the editor (click the Rendering:Actor Hidden In Game Checkbox to hide during runtime) when placed in the level. I would use Event Begin Play in the Level Blueprint to execute the Spawner Actors during level load.

I think I see what you are saying, but it still doesn’t quite get the behavior I am going for. The actors I want to generate during level load were never placed in the editor to begin with, so using a placeholder as you suggest that gets hidden in game doesn’t help.

I really just need a way to hook into the end of the level loading process and add some actors to the level that weren’t placed there. And I need this to happen whether the level is loading in the editor or the game. So when I choose Open Level in the editor, my generated actors will appear in the level.

I’m sure there are plenty of people using procedural level data with UE4. My guess is either they are using BeginPlay (which means they cannot see their data in the editor), or they are working in the C++ source and not at the Blueprint level.

Old thread, but I need exactly the same as FlippedBit described: a callback when a level has been loaded.
I want to generate procedurals at editing time and NOT after StartPlay(). All methods in GameMode are called in Play mode, not in edit mode.

Any more ideas? Thank you for helping.

This thread is super old now and I’m also still looking to have this question answered. I need an event that’s fired around when the editor loads, but after all the assets are loaded. Has anything changed?

You would have to look into the actual engine source code. In order for the editor to start it has to be initialised with all the settings and configurations and behaviour loaded up .

You would need to find this particular block/blocks of code and then add you’re own stuff into it so it would do things to the editor before it opens.

I doubt there is anyway to do this with blueprints. Maybe contact epic directly they may be able to help you pin point the parts of the engine source you would need to look at/modify

I have an overridden EditorEngine class (You can set the EditorEngine override class in the Engine.ini) where I implement any code that needs to be loaded with the editor in the Init function. This happens around the 86% mark when loading the editor, I can’t tell you if the assets are gatherable at this point, I rely on all external data at this point.

Then I implement the same code (Obviously more streamlined for game execution) in the GameInstance::StartGameInstance function when executing standalone. At this point a default World has been created, in which I am able to dynamically spawn all my assets into and then setup the default game mode and spawn the player, so by the time it has left StartGameInstance, I have my world ready to play.

i would say… count the frameRate - while loading is still in progress, its veeeery low, if you know your target system, you should “see” when its loaded. Dirty, but should do, whatever you want to do with that event :slight_smile:

Why not just use a construction script on an actor to do this? Or a blutility?

Tricky! I’m writing a plugin for distribution so ini files might not be an option. For now I’ve managed to trigger stuff when the plugin window is fired and it seems to show by default. The user has to access the plugin window to enable the job it’s doing each time anyway, so that solves it for me.

I’ll keep that in mind though, it sounds useful. I’m wondering if I’ll have to override the viewport class in the same way and then figure out a way to write to the ini file. I don’t see anything stopping me from doing that then forcing a reload.

Unfortunately I’m not using blueprints, I didn’t check what subforum we were in before replying. :slight_smile:

That does raise a possibility though. Is there any way to include in a plugin a blueprint with a constructor that will fire even if it’s not in the world?

It might be a possibility with an OBJECT rather than an ACTOR and the new blutility panel thats in 4.9, but I haven’t really tried that yet. I normally have blutilities on actors that I have in the level, but definitely worth a shot

If you don’t know how to do it, just say so. Everyone second guessing the guy, and not listening. And here we are, another frustrated user, wanting the same thing. Luckly OP had made another post, where he finally got an answer.
To all the Nay-Sayers and the WhyDontYouDoItThisWayInsteaders:
https://forums.unrealengine.com/development-discussion/c-gameplay-programming/15671-spawning-actors-during-level-load?p=228179#post228179
Now this thread can be closed finally. And I can finally go to bed.

Marc Audy
Unreal Engine Developer