How can I spawn an actor as owned by a particular streaming level?

I’m trying to spawn actors inside a certain streaming level, and I need to make them “children” of that level, so that when it unloads, the actors go with it.

How do I do that in blueprints?

The documentation shows there’s an OverrideLevel option for the SpawnActor function, but it’s not exposed in blueprints for some divine reason. I don’t want to use C++.

Thanks.

1 Like

In the fold out details of the spawn node, you get to set the owner. As long as the owner is set to something that is actually from the level, when the level unloads, the actor will get destroyed.

Otherwise, by default, everything is spawned in the persistent.

1 Like

What if the level is empty, though?

Are there any “default” actors I can rely on to always be there, in a level?

And what if I have multiple levels loaded at the same time? How can I get one actor from a specific loaded level?

1 Like

Just put an actor BP in the level, and you can say ‘hey, that spawned this’ :slight_smile:

I think even just a reference to a static mesh would do the trick ( not sure, haven’t tried ).

1 Like

Make it the spawner.

1 Like

Thanks, it thought about it, but I was hoping there’s a more professional solution.

I guess, I have to do the workaround. Thanks again.

But wait, what if I have multiple streaming levels loaded at the same time? I’ll have multiple of those “spawner” actors. How do I know to which level each belongs?

1 Like

How about creating a Game Instance BP, and storing each spawner’s reference there? You can hardcode a string variable with a specific name pertaining to that level, since the game instance reference can be called anywhere, you’ll have a reference to whichever spawner you want at your virtual fingertips

So you mean creating a string var in the spawner actor, and dropping that actor in each of my levels? But how would I reference those actor instances in the game instance?

What you can do is, add an Actor Tag to your spawner actors in the level, save them in game instance, then call them using that specific tag through the game instance.

It’s simple:

  • Create logic to add the input actor reference to your array

  • Now in your Spawner actor’s BeginPlay Event, drag off and get Game Instance, then cast to your specific Game Instance BP, and then drag off from that Game Instance reference and call your Custom Event, and pass Self reference to it (Self being the Spawner BP):

  • Finally, add an “Actor Tag” to your Spawner that is present IN THE LEVEL:

  • Then you should be able to Get its reference according to your specific tag anywhere. Since you want to identify spawners according to level, you can put this code in the level’s BP:

Also, if your spawner is loaded, you don’t even need to do all of that Game Instance stuff, just add an actor tag to your spawner and in your level BP, use the “Get All Actors Of Class With Tag“ node to get that exact actor.

If you need a reference to your actor, that is a child of your level, you don’t even need that node. You can literally get a reference to your actor right inside the level BP. Do note that this only works in the Level BP.

  • Select your Spawner Actor in the level:
  • Go back to your Level BP, RMB and there will be an option to create a reference to the spawner directly:
  • Once again, this only works in the level BP, and only if the actor is a child of the level BP.

Good luck!

1 Like

Use SpawnActor with FActorSpawnParameters and set OverrideLevel to your target ULevel from the Streaming level’s GetLoadedLevel(). This ensures the actor is owned by that streaming level instead of the persistent level :grinning_face:

1 Like

I have a lot of overlapping streaming levels, and sometimes I have to set the owner to get object to destroy when the level unloads. I never have a problem finding something to own the object, because if the level is empty, there’s no point in using it :rofl:

1 Like

Thanks a lot for your detailed answer. Mama warned me about using the Level BP though. I’ll probably have hundreds of small levels and I would have to adjust the level BP of each.

Here’s where I’m at right now:

I’m hand placing actors in a streaming level, like PickUps. When you place them in the level, their owner becomes the level? I guess?

In game, you may pick up some, and leave some.

Now, when I unload that streaming level, the PickUps left get “saved” to a SaveGame Object (how is not relevant here).

When I reload ALL my streaming levels, I’m destroying all those PickUp actors and re-spawning only the ones that were “saved”. But which owner to assign to them? It has to be something in their own specific level.

So if I could get some actor from their level as the owner WHEN I SAVE THEM, alongside the rest of their save data, that would solve it.

I know, I could put an actor in each level specifically for this. And use it as owner. But again, I’d have to do that for hundreds of levels. And then maybe differentiate through a tag, as you said. And that relies on tags being entered manually correctly. It gets complicated quick.

What I’m trying to find out here is the most efficient, simplest way to designate an owner to actors in a specific level, when I save them, without going through the workarounds. Something that I can rely on to be there when I reload that level.

Thanks again for your help.

Yes, the reason I mentioned empy level is because maybe all the actors in a specific level get destroyed at runtime.

And then you need something to latch on to as an owner, from that level.

I was wondering if an empty level has “default” actors that are there from the start, not visible. Something you can rely on to be there always.

What’s your method, by the way? How do you know which owner to get from multiple levels, when you destroy actors? How do you know which actors to destroy when a certain level is unloaded?

1 Like

Thanks, but I’m not using C++. Is there a way in blueprints?

The level BP is an object, maybe you can get a ref to that?

Just as each of my levels streams, the actors that are going to spawn something know it has to have an owner, and they just set themselves as that.

1 Like

Thanks, I just found that out. I was about to reply.

I had no idea, LevelScriptActor is actually the level BP.

So I’m just going to have to GetAllActors of that class, and see which belongs to a specific level using GetLevel + GetOuter functions, and use it as owner. The rest is history.

And I think this is the best you can get with blueprints.

Thanks a lot for the help, guys.

2 Likes