How would I spawn the player at a specific point when opening a new level?

I’m making a 2D top down game similar to The Legend of Zelda: A Link to the Past

I have a level named “Town” that acts as a level hub. When the player walks into a house or shop, they are teleported to a new level containing the insides of the house or shop. The problem starts here, when the player walks back outside they will always spawn at the initial player spawn point. I want the player to spawn outside of the door they just walked out of.

How would I set a specific spawn point so my player can seamlessly exit buildings and end up where they should be?

The GameMode class has a ChoosePlayerStart function, that you can override in its blueprint/C++ class.
I’d say, you keep an integer (“StartIndex” for instance) with the index of the PlayerStart you want to choose, in a GameInstance class.
Then, when the player goes through the door, you change the index, from 0 to 1.
In the overriden function, you get all actors of class PlayerStart, and return the one at the index you saved in the GameInstance class, and that way, you get different starts depending on where you exited another level.

If you didn’t understand anything i said or need a hand let me know.

1 Like

Thank you for your help, unfortunately I’m still a little lost. I’m very new to unreal so I get lost pretty easily. This is what I have at the moment.

I made a GameMode BP, but I couldn’t find the “ChosePlayerStart” function. Is it called something else or have I done something wrong?

Next time, don’t forget to comment on the answer, instead of creating a new answer ok? :wink:

So if you go to your GameMode blueprint, you can override the ChoosePlayerStart function by clicking this:

In that funcition you should do something like this:

So, just in case you don’t know what a PlayerStart is, it’s an actor you can place in your level that the GameMode will use to spawn the player. You can use as many as you want. In your case, you’d want two PlayerStarts, located next to the two different locations you want the player to spawn in. You would then use the integer you save in the GameInstance class to choose which of those to use in the ChoosePlayerStart function.

By the way, the GameInstance class is a class that will not change even when you open a new level, it will remain consistent from the point when you start the game until you exit the game, that’s why you have to save this integer in the GameInstance class.

If you haven’t created your own GameInstance class, do so, and remember to pick GameInstance as its parent.

After that, add an integer variable to it.

After that, go to your Project Settings → Project → Maps and Modes → GameInstance (at the end) and set it to your newly created GameInstance class.

Now, wherever you have the logic for your door, you can access the GameInstance class you created and modify the integer in that class, in order for the GameMode to know which PlayerStart to use when spawning the player.

If you’ve got any more questions let me know :slight_smile:

i would suggest one change to your method here and that would be to store all the player starts in an array instead of using a get all of class node. the reason being is that its more reliable and consistent. now i dont know how all nodes function in the back end but i do know that when using a get all of class theres no guarantee that you will get the same results in the same order every time so it becomes better to set implicitly what you want to occur.

1 Like

Yes i agree! I was wondering that actually, but what you said is more correct!

i just try to do things to control the behavior of scripts so odd things dont happen. id hate to package and ship a game then have the character whos trying to exit to the town end up in the volcano down the road haha

Thank you guys so much for all of your help but I’m completely lost. I have no clue what an array is and I’m having trouble understanding where I need to place certain things. I created a Youtube video to showcase my layout because I’m terrible at explaining. Problems choosing a spawn location. - YouTube

Thank you for your help :slight_smile:

so due to your setup and the way you spawn the character i made a little modification to the standard to make things easier and made a little example. the basic idea here is that when you travel from one level to another you have a start and end actor, so you have one actor that will be something like on begin overlap with the player open tree house level, then in your case you spawn the character when the level starts which makes things easy since you are already setting the spawn location. so what we are going to do is to set a variable just prior to leaving the level then we are going to get the variable when beginning the new level and we will have a location that is associated with that variables value. it should make more sense once youve made it.

to begin we need to create a variable that wont be lost between levels. the best location for this will be in the game instance as the game instance is one of the few “blueprints” that persist between levels. if you dont already have a custom game instance then you will need to make one and set it as the one to use. to make a game instance you will need to: from the main window with the scene on it click the green add new button, then select blueprint class, next search for game instance select the one that says gameinstance exactly not one of the ones below it. next name and open the new gameinstance blueprint. all we need to do in this blueprint is to create a variable, in this example i made the variable a integer named DestinationSpawner. now that the game instance is created we need to set it to be the one used in the game. from the main window go to edit, then project settings. in project settings select maps and modes, then near the bottom look for game instance and set the value to the one you created. the next steps ill add in another post below.

alright now i just watched the rest of your video and you almost have this whole thing down.

this section will be on the first picture above. im sure this is somewhat similar to what you have now to open the new levels. basically all you need to do here is just before you open the level you want to set the variable in the game instance that pertains to the destination.

the last piece is to set the location to spawn at when you begin the level. for this i used a select node. the way a select works is that it set the return value pin to the option of the index number, so if the index is 1 then the select will use option 1. in this case the options are vectors but you could easily set it to be the location of actors in the level.

as a last note also you said you didnt know what an array is, an array is basically a list. in the case of what we use here its an array of variables, so you could have an array of integers which would just be a list of integers. its a good way to store information. for each piece of information in an array there is an associated index number which can be used to get certain items from the list. its function would work similar to the select node that i used in the example, you have a list of locations and select one based off its index,

Thank you so much for taking your time to explain this to me. This helped a lot for sure, thank you. :smiley:

Update: It works perfectly and I couldn’t be happier, Thank you for your help :smiley: