Hey guys I'm gonna start by saying I'm really new to UE4 so bare with me please. I'm trying to make a game that spans multiple levels and game modes and I'm struggling to figure out how to do it and maintain my character selection. I've scoured here and YouTube looking for the answer and I can't find the right solution.
Basically I have a main menu which opens my character select and upon choosing a character (I have 2 to choose from) it opens my game select map. In this transition I load the selected actor and possess it and then remove from parent as my default pawn is set to none on my gamemode. In this map I've created 2 doorways so my players can select a game type. It's just some geometry and the character selection works and they can move about. In my script if they hit the trigger box on the left door it loads my "obby" level but because my default pawn is none it forgets the previous selection and just sort of floats there with no ability to do anything.
There seems to be a couple of ways to do it but I can't figure it out. I was thinking maybe saving it to a save game object and loading it each time I transition between levels but can I delete this save upon closing the game? I also can't figure out how to save a mesh as most videos explain how to save integers and transforms.
Thanks for any help.
the easiest method ive found to do this is to set a variable in the gameinstance for the type of character you want then in a player controller on begin play spawn the character and posses it. the reason to use the game instance is that it persists between levels and only exists in the current play session so it will be reset every time you restart the game. you will also need to come up with a way to set the variable in the game instance through the use of a widget or something but im sure you will be able to figure that out.
ok so to begin the setup here you will need three blueprints: Game Instance, Game mode, and Player Controller. you will need other blueprints later.
in the game instance all your going to need to do is create a variable which will be the character class that you are going to want the player to be. so just create a variable and name it something descriptive (i used character selection here). next you are going to want to set its type to character and make sure that its a class reference (the purple one). now we are done here, see first picture for more info.
next comes the game mode which will be another easy step. here you just need to set a few defaults. we are going to want to set the default pawn class to none so that no character is spawned from here at the beginning of the game and we are going to want to set the default player controller to the one we created (if you havent created this yet do so now we will fill it in next).
Now the last thing we need to script is the player controller (see the second picture for reference). so in this script we are going to need to get the character variable from the game instance then spawn that character ans posses it. to do this we right click the graph and search for get game instance which will give us a reference to the current game instance when the game is run. drag off this reference and search for cast to (insert your game instance name here), this cast basically tells the script if the current game instance is of the specified type. now drag off the blue return pin which will be named as _ and search for the character variable you created earlier. next we need to create the spawn actor node which can be found by right clicking the graph and searching for spawn actor from class. last we just need the posses node which you can find by dragging off the return value on the spawn and search for posses. after that just hook everything up as shown in the picture below.
the last step is to set the game instance and game mode so that the game will actually use them. to do this go into the project settings (from the main viewport window go to edit then project settings), then project -> maps and modes. in this section look for default modes and default game mode and set this to the one you created (you can modify this later as needed for your game). then near the bottom you will see game instance, this also needs to be changed to the one you created. remember you can always use the search functionality to find these settings easier.
there are several bit here that you will want to tweak to your needs but this is the basics of everything to get you started. let me know if you need some more help. good luck.
answered Apr 18 '18 at 02:21 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here