I dynamically spawn 3 spheres (actor-blueprints) on “event begin play”. However, I want each sphere to be spawned on a different stream-level. Because the real project has not 3 spheres but more like 300.000. So I need stream-levels for performance reasons.
But there is no build-in option in the editor (that I could find) to implement this.
Adding the code to the stream-level’s blueprint sadly still spawns them into the persistent level.
It took a while to figure out how to spawn into a streamed level when using streaming levels, but after all my experimenting, it was quite easy in the end for what I was trying to achieve, so I hope this might help you out Napoleon.
I decided to switch my project over to using streaming levels, and when I tried to spawn my characters and objects, they all were spawned in the persistent level and were not visible on the streaming levels. After a bit of experimenting, here are the steps that I use to accomplish this:
Create a new blueprint actor (I called mine SpawnHelper) and compile it (it really has no code in it at all).
Drop the actor SpawnHelper into each level where I will want to spawn objects at runtime, and position them where I want to spawn.
Open the streaming level directly in the editor, not the persistent level that contains all your streamed levels. This could get tedious with lots of streaming levels as you will have to do steps 4 and 5 for each streaming level.
Select the SpawnHelper Actor in the world Outliner then right click in the Event Graph of the Level Blueprint and you can create a reference to SpawnHelper Actor. It will will show the reference as coming from persistent level, but since you are editing the streaming level directly in the editor this is fine.
In the Event Begin Play, use SpawnActorFromClass, and set up the pins on how you want to spawn your actors, but you must drag a pin from the SpawnHelper reference to the Owner pin on the SpawnActorFromClass function call. This will then spawn your actor in the sub level where the SpawnHelper instance is.
You can then verify that this is all working fine by opening up the main level that contains all your streaming levels and playing in the editor. If you go to the world outliner and click the little down arrow to the right of the type column you can select level. Then you can see all your spawned actors and they won’t show up as being in the persistent level any more, they will actually show up as being in the right streaming levels.
My project is actually controlling the streaming levels from blueprints using LoadStreamLevel and UnloadStreamLevel, so I’m afraid I haven’t tested this with distance based streaming. I’m hoping if my game world ever gets big enough eventually I’ll switch the game areas to using distance based streaming, i’m just keeping things smaller for testing and proof of concept.
The “Owner” part is the most important part here. When spawning a new Actor and setting the Owner parameter in the SpawnActor node will ensure they spawn in the same streaming level. I don’t really like the rest of the implementation as it’s very static and manual work,you can roll your own solution of course.
For C++ I have another solution for after the Actor is spawned and needs to move:
I’m not sure that’s the correct solution but using only the ‘rename’ causes some weird issues in some of my cases:
Add:
// Remove from world
GetWorld()->RemoveActor(Actor, true);
// Rename and move it to the level and add it to the list
Actor->Rename(*Actor->GetName(), Level);
Level->Actors.Add(Actor);