Adding A Grabber Component
Within our game of escaping the room we want the player to be able to just barely escape the door before the door closes again when they leave the pressure plate. What if we wanted to make it so that the player has to pick up an object and place in the pressure plate to open the door, that way the player can place the object and not have to worry about the door closing. There is a few things that we need to do in order to be able to add this “Grabber” component, essentially the same way we added in our “OpenDoor” component for the door. So to add this component to our player, which is a temporary actor at the moment and only appears when you hit play in the editor, we first have to tell the “Game Mode” which “Default Pawn” to use as our player. To do this we first have to create a “Default Pawn” Blueprint as well as a “Game Mode” Blueprint. The “GameMode” is basically all your parameters and rules for your game, anything from what inventory items a player starts with, how many max lives the player has, any time limits or a score that needs to be reached to win the game, all these things are found within the “GameMode” of our game. If you open up your “Project Settings” in the Unreal editor and go to Maps and Modes, you will see that the mode selected is “GameMode” and nothing below it can be altered.
What we have to do is make a Blueprint of the GameMode, Blueprints are very useful in this situation because they help when you are making templates of assets, they are very clear and visual. Blueprints also help prevent you from hard coding changes to assets. Making a Blueprint of our current GameMode is simple you just have to go down in the “Content Browser” tab and under “C++ Classes” find the one that says “Building_Escape_GameMode”, then you simply right click on it and selecting “Create Blueprint Class Based on Building_Escape_GameMode”. This will prompt you to change the name of the Blueprint it makes it easy and clear to read if you suffix the current name simply with “_BP” at the end. Once that Blueprint is created we can alter our “Default Pawn” actor. We will do the same to our “Default Pawn” and make a Blueprint of it. To do this we will have to hit “Play” on the Editor and once we do that then we have to “Eject” ourselves from the player and we will be able to see the “Default Pawn”. Then it is simply a matter of selecting it either in the editor itself or in the World Outliner. Once selected we bring the dropdown menu for “Blueprints” at the top of the editor and selecting “Convert Selected Actor to Blueprint Class..”. This will prompt you to select the folder to place your new Blueprint Class into and also to rename it, and the same as we did with the GameMode Blueprint and change the change simply to “DefaultPawn_BP”. Now that the Blueprint is created we can simply drag and drop it into our world. With our “DefaultPawn_BP” created we can now make the grabber component for it. In the “Content Browser” we will use “Add New” and add a “New C++ Class..” When prompted to choose the Parent Class we will select “Actor Component” because it is going to be attached to the Actor itself. Rename the Component to “Grabber”, hit “Create” and it will open up the C++ code in your Code Editor. It will have created the “Grabber.cpp” file and the “Grabber.h” file with the standard boilerplate code inside them. In the Unreal Editor now you will see in your “Content Browser” tab that there is a new “Grabber” component added. From here it is as easy as adding the “OpenDoor” component to our “Door”. We open up the “DefaultPawn_BP” tab and select “Add Component” and search for our new “Grabber” component.
Just like that our new “DefaultPawn_BP” now has the “Grabber” component added to it, even though the grabber component isn’t doing anything right now. Now with our GameMode Blueprint made and our Default Pawn Blueprint made as well we can go into our “Project Settings” and under “Maps & Modes” we can now change our “Default GameMode” to the new Blueprint version of it that we created. Selecting our Blueprint GameMode will then allow us to alter the other drop downs below it as well. In the “Default Pawn Class” dropdown we will change it and select our “DefaultPawn_BP” that has our “Grabber” Component added to it as well.
As mentioned earlier, our “Grabber” component at the moment doesn’t have any functionality to it, it is just attached to our “DefaultPawn_BP”. In next week’s blog we will go over adding the functionality needed to be able to pick up the object in our world.