In this two-part tutorial, we will set up the base VR player pawn so that we can actually see our motion controllers track and have some very basic input set up.
The first part of the tutorial is just creating a new C++ pawn class, adding a pawn blueprint that inherits from the C++ class and hooking this up to the game mode. If you are already familiar with this process then you can straightaway skip to the next part. The second part of the tutorial is where all the actual VR components are added.
The first thing that we would want to start off with is opening the visual studio project. If you are following along from the previous tutorial, let’s start by opening the visual studio project by using the “File->Open Visual Studio” menu button in the editor.
Open the visual studio project is open you should be able to notice a few things. A C++ class is already created for you which implements the Game Mode base for the project. You should be able to see this class in the content browser window in the editor as well as the solution explorer in VS. The other important thing to notice is that although the game mode class is created, it is not being used in the project. You can see this in the “project settings-> Maps and Modes” section. The screeenshot below shows all this.
Game Mode Blueprint:
Opening the auto-generated game mode base .cpp and the .h files in VS, we can see that the classes dont really have any implementation in them and are just basic skeletal code that might utilize in the future. For now, we don’t really require to implement anything in the game mode base class. We will just hook it up so that the project is using the class.
Let’s start by creating a game mode blueprint class that inherits from the auto-generated Game mode base class. In the content browser window, Lets first start by creating folders under “Content“. The hierarchy should be “Content/Blueprints/BaseGame“. We will be putting all the game related blueprints in this folder. Navigate into the BaseGame folder, right click in the content browser and create a new blueprint class. This will open up a dialog box to choose the parent class. Expand the “All Classes” option near the bottom of the dialog and start searching for the auto-generated game mode base class using the name of the class. Select the class once you find it and hit the “Select” button. This will create a game mode blueprint in the BaseGame folder. Lets name this game mode as “VRGameMode“.
Next, we need to set the game mode of the project the game mode that we just created. To do this, open the project settings and under the Default GameMode setting, select the blueprint that we just created. This is it for now regarding the game mode. We will need to come back to it later when we have to hook up our player pawn.
Player Pawn C++ Class:
“The Pawn class is the base class of all Actors that can be controlled by players or AI. A Pawn is the physical representation of a player or AI entity within the world.“
Let’s start by creating a pawn that will be controlled by the player. This pawn will have the required components for head tracking and motion controller tracking.
Create a C++ class by clicking on File->Create C++ Class. In the dialogue that shown, select “Pawn” as the parent class.
Click next and enter the details of the player pawn class. Name of the class is “cVRPlayerPawn” and path “Rift_From_Blank/BaseGame/Player/“. This class will contain all the code for the player. All the components will be set up here.
Next, we will create a pawn blueprint that inherits from the C++ pawn class that we have just created. We will use this BP in the editor and might use it in the future for hooking up any functionality that might not be supported in C++.
To start creating the pawn blueprint, create the relevant folders. I will be placing the blueprint in the “Content/Blueprints/BaseGame/Player” folder. Right click on the folder in the content browser and select the option to create the blueprint. Click on the show all classes checkbox at the bottom of the dialog that is shown to pick the parent class. Type in “cVRPlayerPawn” this will show you the option to pick the C++ class that we have just created.
Clicking on select will create the pawn blueprint. Lets name this “VRPawn”.
Next, we need to set up the game mode to use the pawn that we just created. Let’s open the VRGameMode asset that we created and hooked up earlier in the tutorial and set the “Default Pawn Class” to “VRPlayer” by clicking on the drop-down box and selecting the pawn that we just created.
At this point, Let’s test if the game mode and the pawn are both hooked up correctly. To do this we will add a log in the pawn C++ Class. Let’s start by including the engine header file to get access to the logs.
Next, In the begin play function, we will add the code to display the log on screen
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, "Player Pawn Base Begin Play", true);
Let’s hit the compile button in the UE editor window for the changes to take effect. Previewing the game in the editor at this point, we should see the message show up in the editor window.