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.
Ok so would I do all my scripting in my game instance? I’m casting to my CharacterSelect widget and doing a bind event to on clicked for my first button. This then spawns the actor and then possesses it. I don’t have any input to start this script though as begin play isn’t a option. I’m trying to upload a capture of my script but it just stays at 0% uploaded.
Currently though my character swapping is done in a level via a button on my character select widget which decides which mesh you end up with but this method won’t let me save my selection. You’ve been a great help so far as I didn’t consider making my own game instance or controller and I didn’t know that game instance was persistent and cleared upon exiting the game. My mind is so foggy from trying to make a save slot and trying to set the default pawn that I can’t think straight. Been stuck for 3 days. I thought game development would be easy. Boy was I wrong.
Ok so would I do all my scripting in my game instance?
what script are you referring to? you shouldnt need to put any script in your game instance. if your talking about the script for selecting the character and setting the variable then that really depends on how you plan to implement things. at the most basic you could have a widget with two buttons, one for each character and on clicked have the widget set the variable in the game instance and load the first level. but there are many ways for it to be done.
i dont really know what your trying to do in the picture you posted but it isnt necessary from what i can tell.
below is a script in a basic two button widget as described above. this is the type of basic thing id use for testing so theres room to make it fancier, but basically you could create this widget from anywhere when the time was right, so like just after the player clicks start game on the main menu you could create a little character select screen. you would have the script to create the widget in a dummy main menu level bp or from another widget.
Ah I see now. I was trying to set my variable in the instance. But now with your example script I see you can cast to your instance so you can get a reference to your character select variable and also set it. Thanks you’ve been a great help.
ThompsonN13, you are an absolute legend my friend! I was questioning if this would work and I thought I would need to make an array with a pool of choosable characters but it’s so simple to see now where I went wrong and how this works. The fog is gone from my mind. How do I mark this as answered?