In this topic, I will show a tutorial on how to fulfill parts of the Mega Challenge of the UE4 SimpleShooter game that you would need to do after finshing the course.
— Switching Weapons —
Open UE4 editor.
Make new weapons by duplicating BP_Rifle. Do not “Create Child Blueprint Class” of BP_Rifle.
Change the name, mesh Max Range and Damage of each Blueprint Weapon.
Create inputs for allowing the mouse scroll wheel to switch weapons.
Switch to Visual Studio Code.
Add to ShooterCharacter.h these highlighed code in the respective places:
Add to ShooterCharacter.cpp these highlighed code in the respective places:
-
Replace the Gun code in Begin Play with this:
-
Add this code to the SetupPlayerInputComponent function:
-
Make the SwitchWeapons function like this:
-
For the FMath::Clamp you need the right include:
-
Change “Gun” to “Gun[ActiveIndex]” in the Shoot funtion:
Switch back to UE4 editor.
Click “Compile”.
Open BP_ShooterCharacter, click “full blueprint editor”.
- Go to Class Defaults. In the Details Panel you can click on “Gun Class” to expand it because it is an array. Change the options to the weapon Blueprints (e.g. “BP_Rifle”. I have renamed mine to “BP_Weapons_Rifle” and etc.).
Hit Compile and Save and play the game!