Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.19
more ▼

asked Apr 26 '18 at 07:34 PM in Using UE4

avatar image

6 1 2 3

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 26 '18 at 07:50 PM

avatar image

897 12 14 22

avatar image ThompsonN13 Apr 26 '18 at 10:09 PM

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.

avatar image goncasmage Apr 26 '18 at 10:13 PM

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

avatar image ThompsonN13 Apr 26 '18 at 10:43 PM

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

(comments are locked)
10|2000 characters needed characters left

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?

more ▼

answered Apr 26 '18 at 09:36 PM

avatar image

6 1 2 3

avatar image goncasmage Apr 26 '18 at 09:54 PM

Next time, don't forget to comment on the answer, instead of creating a new answer ok? ;)

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

alt text

In that funcition you should do something like this:

alt text

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 :)

help-1.png (269.8 kB)
help-2.png (107.2 kB)
avatar image AdamColeJones Apr 27 '18 at 09:50 PM

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. https://youtu.be/NSMYiH9UzuQ

Thank you for your help :-)

avatar image ThompsonN13 Apr 27 '18 at 10:50 PM

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.

alt text

alt text

capture2.png (176.6 kB)
capture.png (113.0 kB)
avatar image ThompsonN13 Apr 27 '18 at 10:57 PM

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.

avatar image ThompsonN13 Apr 27 '18 at 11:01 PM

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,

avatar image AdamColeJones Apr 30 '18 at 02:19 AM

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

avatar image AdamColeJones Apr 30 '18 at 02:55 AM

Update: It works perfectly and I couldn't be happier, Thank you for your help :-D

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question