Spawn actor in sublevel

Hello,

I need to spawn 3 pawns just after opening a new sublevel. So I created 3 actors to spawn the 3 pawns but they wont spawn at all.
When I use that method in my persistant level, it works perfectly fine.
I think the mistake comes from the “Get all actors of class” when trying to get the 3 spawners, because it’s empty when I put the spawners in the new sublevel.
Thanks in advance for any help/advice.

Where are you running this script? Have the spawners been created yet before you “get all actors of class”? Have you tried printing the length of that array output of “get all actors of class” and see the length make sure you actually found the spawners? Also you know this will spawn all the “pawns” at the same location because you are only getting the “0” index out of the array of spawners. Lastly, you could clean up the transform scripting by just pulling off the “Get” node coming from the “get all actors of class” and saying “get actor transform” and plug that directly into the “Spawn Actor From Class”. Instead of taking 3 steps to do it by “get world location, rotation and scale” and creating a transform from that. Just a tip.

I’m running this script in the blueprint of an actor (in my persistant level) that makes the level open on boxtrigger.
I have created the spawners by just placing them in the level.
I do know that it’ll make the actor at the same location, I just want it to work for the index 0 and then change it later.

I just tried to print the length of the array and it always shows 0.

I’m new to UE4 so I might have done a mistake with the level thing. I just used the “open level” function in order to have the player teleported to the next level, which works.

thanks for your answer

What is before this line of execution? How does this fire off? Because if it is off something like event begin play this won’t fire just from opening a sub-level if the actor you run this in exists in the persistent level it has already “begun play” before the sub-level opened, that could be why this stuff never gets created and the array is empty

Yes, the open level is just before this line in the screenshot and it doesn’t fire off for the moment.

Can you post a screenshot because I have a feeling that is where the issue is.

Ok so I have an “on component begin overlap” event which is linked to the first screenshot here, then it goes to the second one here and the last part is the the screenshot in my thread’s question.

I am more confused now, I can’t figure out this logic. Also the screen shots are not very clear, so I have to zoom in very close and I can’t follow what is being spawned by what and when. Please re-do the screen shots. Start from the VERY beginning, I want to see the red execution nodes that actually initiate your script and then each bit of script relating to opening the sub-level and spawning the pawns. Then we will go from there.

Alright sorry, i’ll put them in the right order.

As I said before, it works when I put the spawners in the persistant level but when I delete them in the persistant level and create them in the Level2 (my sub-level) then it doesnt work (as long as I know, the “get all actor of class” is always empty)

Wow…not sure where to start with this. Your script should flow logically, I should be able to understand what you are doing just by reading the nodes, however there are multiple areas where the logic seems to break down.

  1. You should not have to keep casting to your game instance every 2 or 3 nodes I am seeing a “cast to game instance”. Do it once, save the output as a variable and use that variable to do whatever you need to do. That will help make your code more readable. Same thing with your player character
  2. The second screen shot is still bad, there were so many other things I had questions about I decided to just skip trying to figure out what is going on there
  3. When you post screen shots of long lines of code, make sure you have the end of the first one in view when you take the second screen shot and so on that way people reading it can follow exactly where the code is going, it only takes one little pin to be off to cause a problem and if we can’t see everything we may be chasing our tail for a while
  4. Image 3, you are creating an “array of monsters” by “getting all actors of class”, does this array populate? Do you have “monsters” currently spawned in the level when this event is called? If not your “monsters array” will be empty
  5. Image 4, you get all actors of class again but this time it is the “spawners”, you said you placed in the level already, so this array should not be empty.
  6. Image 4, you use the “array of monsters” to pull the actor class out to use as the input for the spawn actor from class node, if that array is empty as I stated in point 4, this will call a null actor to be spawned.
  7. Additionally, it seems redundant to use the array to get the “actor class” for the spawn actor from class node, you already filled that array with a specific “actor” so it would only ever pull that actor out no matter how many indexes you had in the array. I would just specify the actor directly in the Spawn Actor from Class node.

Thanks for all of your advice ! I’ll try to do my best to make my code better.

The array of monsters populates yes, all of this line works when I create the spawners in the persistant level, so with the array of monsters populating, the “get all actor of class” functions working and everything.
When I create the spawners in the sublevel, only the second “get all actor of class” is not working, so the one with the spawners. So I feel like there is something missing to “tell” the code that I want to look for spawners in the sublevel.

Thanks again for the advice, I hope you unterstand more what I want to do.

So basically all of this runs in the persistent level. So when the player overlaps your trigger this code fires off. But in Image 2 is where you open the sub level correct? So while the sub-level is loading the rest of this execution path is firing off. So most likely it hasn’t yet created the spawners in the sub-level by the time it reaches the point where it needs to “get all actors of class”. A simple way to test this is add a delay before you “get” all the spawners. So throw a delay in there for like 3 seconds and see if that solves the problem.

Yes, the sub level is opened in image 2.
It seems like adding a delay completly stops the following lines to execute as I can’t even get a print string right after the Delay.

Where did you add the delay? Do you have a screen shot?

Just before getting all the spawners. I tried to move it to other places and it keeps stopping the flow

So quickly reading through some documentation on sub-levels, and now I have another question for clarification. All of this runs in the “persistent” level? And works fine? You create the spawners in the persistent level and everything is fine when you open the sub level? But what you want is to create this in the sub-level level BP? Because again your screen shots don’t include what BP these are in, so I am getting confused following how and where you are calling your events. A delay in the persistent level should not stop execution altogether. So some more clarification would be helpful. If you haven’t done so yet, I would stop the logic in the persistent level once the “sub level” is opened and from the “sub level’s level BP” GET all the spawners and run the rest of your code.

Also, print strings from every where. Print the length of each array, print the “class” that comes off the “GET” and you put into the spawn actor from class node OR better yet, just select the class you want to spawn directly, save yourself a step that could be causing unnecessary problems as I’ve said before, you are only ever going to pull the same class out of that GET node.

Exactly, I first tested this in the persistent level, it worked perfectly, the right monster spawned at the right spawner. And then wanted to adapt it for the sub-level.
This BP is the event graph of an actor that has a trigger box (so with the “on component begin overlap” event in the screenshots). This actor is in the persistent level.
You’re right, i’ll try this one right now !

Seems like it’s working now as I get the spawners, but now it’s the monsters that I can’t get, I’ll try to keep the first “get all actor of class” in the persistent level BP so it’ll work.
Thanks again for all your help and your time. It is very appreciated :slight_smile: !
Have a good day

No problem. Glad we made some progress. From the sound of it though you’re using the GET node before the actors are created. So just check the logic and make sure the actors exist before you try and GET them and you should be fine. Good luck!