18 KiB
Part 2 (C++): Create the Player
← Previous: Part 1 (C++) - Project Setup | Back to Index | Next: Part 3 (C++) - Create the Bullet →
Overview
In this part, we'll create the player ship as a C++ class called ASTGPawn.
Time comparison:
- Blueprint approach (Part 2): ~60 minutes (12 variables via UI + Blueprint nodes)
- C++ approach (this part): ~15 minutes (copy-paste code)
Step 2.1: Create STGPawn C++ Class
In Unreal Editor
-
Go to Tools → New C++ Class (top menu bar)
-
In the "Choose Parent Class" window:
- Click "Pawn" (NOT Character - we want simple 2D control)
- Click "Next"
-
In the "Name Your New Actor" window:
- Name:
STGPawn - Path: Should be
Source/BulletHellGame/(default) - Click "Create Class"
- Name:
What Happens Next
- Unreal generates
STGPawn.handSTGPawn.cpp - Your IDE opens with the new files
- Project compiles automatically (~30-60 seconds)
- Unreal Editor refreshes
💡 TIP: If compilation fails, check the Output Log in Unreal Editor
Step 2.2: Define Player Variables in STGPawn.h
Now comes the magic! Instead of clicking UI 60+ times, we'll copy-paste all variables at once.
Open STGPawn.h in your IDE
Find the class definition (should look like this):
UCLASS()
class YOURPROJECTNAME_API ASTGPawn : public APawn
{
GENERATED_BODY()
public:
ASTGPawn();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
Replace the entire file with this
⚠️ IMPORTANT: Replace
YOURPROJECTNAME_APIwith your actual project's API macro (e.g.,BULLETHELLCPP_APIif your project is named "BulletHellCPP"). The macro name is your project name in UPPERCASE followed by_API.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "InputActionValue.h"
#include "STGPawn.generated.h"
// Forward declarations
class UCameraComponent;
class USpringArmComponent;
class UStaticMeshComponent;
class UBoxComponent;
class UInputMappingContext;
class UInputAction;
UCLASS()
class YOURPROJECTNAME_API ASTGPawn : public APawn
{
GENERATED_BODY()
public:
ASTGPawn();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// ===== COMPONENTS =====
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UStaticMeshComponent* ShipMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
USpringArmComponent* SpringArm;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UCameraComponent* Camera;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UBoxComponent* Hitbox;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UStaticMeshComponent* HitboxIndicator;
// ===== ENHANCED INPUT =====
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
UInputMappingContext* DefaultMappingContext;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
UInputAction* MoveAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
UInputAction* FireAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
UInputAction* SpecialAction;
// ===== MOVEMENT & BOUNDARIES =====
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
float MoveSpeed = 750.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
FVector2D BoundsMin = FVector2D(-850.0f, -450.0f);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
FVector2D BoundsMax = FVector2D(850.0f, 450.0f);
// ===== FIRING =====
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
float FireInterval = 0.08f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
float BulletSpeed = 2200.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
int32 VolleySize = 3;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
float VolleySpread = 12.0f;
// ===== LIVES & SCORE =====
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
int32 MaxLives = 3;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Stats")
int32 CurrentLives = 3;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Stats")
int32 Score = 0;
// ===== SPECIAL ABILITY =====
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Stats")
bool bSpecialUsed = false;
// ===== INPUT FUNCTIONS (Enhanced Input) =====
void Move(const FInputActionValue& Value);
void StartFire(const FInputActionValue& Value);
void StopFire(const FInputActionValue& Value);
void UseSpecial(const FInputActionValue& Value);
// ===== GAME LOGIC =====
void FireShot();
void TakeHit(int32 Damage);
void HandleDeath();
void AddScore(int32 Points);
private:
FTimerHandle TimerHandle_Fire;
bool bIsFiring = false;
FVector2D MovementInput;
float FireTimer = 0.0f;
};
What Just Happened?
You just defined 12 gameplay variables + 5 components + 4 input actions with default values in ~30 seconds (copy-paste time)!
In the Blueprint tutorial, this would require:
- 12 variables × 5 clicks each = 60 clicks
- ~15 minutes of manual work
- High chance of typos
Time saved: 14.5 minutes ⚡
Step 2.3: Implement STGPawn Constructor
Open STGPawn.cpp in your IDE
Replace the file content with:
#include "STGPawn.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
ASTGPawn::ASTGPawn()
{
PrimaryActorTick.bCanEverTick = true;
// Root component
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
// Ship mesh - cone shape pointing upward
ShipMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipMesh"));
ShipMesh->SetupAttachment(RootComponent);
ShipMesh->SetCollisionProfileName("NoCollision");
// Load cone mesh from engine
static ConstructorHelpers::FObjectFinder<UStaticMesh> ConeMesh(TEXT("/Engine/BasicShapes/Cone"));
if (ConeMesh.Succeeded())
{
ShipMesh->SetStaticMesh(ConeMesh.Object);
ShipMesh->SetRelativeScale3D(FVector(0.5f, 0.5f, 0.7f));
ShipMesh->SetRelativeRotation(FRotator(90.f, 0.f, 0.f)); // Point forward
}
// Hitbox - small for bullet-hell precision
Hitbox = CreateDefaultSubobject<UBoxComponent>(TEXT("Hitbox"));
Hitbox->SetupAttachment(RootComponent);
Hitbox->SetBoxExtent(FVector(25.f, 25.f, 10.f));
Hitbox->SetCollisionProfileName("OverlapAllDynamic");
Hitbox->SetGenerateOverlapEvents(true);
// Visual hitbox indicator (small sphere)
HitboxIndicator = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HitboxIndicator"));
HitboxIndicator->SetupAttachment(RootComponent);
HitboxIndicator->SetCollisionEnabled(ECollisionEnabled::NoCollision);
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereMesh(TEXT("/Engine/BasicShapes/Sphere"));
if (SphereMesh.Succeeded())
{
HitboxIndicator->SetStaticMesh(SphereMesh.Object);
HitboxIndicator->SetRelativeScale3D(FVector(0.05f, 0.05f, 0.05f));
}
// Camera setup for top-down view
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(RootComponent);
SpringArm->SetRelativeRotation(FRotator(-90.f, 0.f, 0.f)); // Top-down
SpringArm->TargetArmLength = 1200.f;
SpringArm->bDoCollisionTest = false;
SpringArm->bInheritPitch = false;
SpringArm->bInheritRoll = false;
SpringArm->bInheritYaw = false;
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
}
void ASTGPawn::BeginPlay()
{
Super::BeginPlay();
CurrentLives = MaxLives;
// Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void ASTGPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Movement with bounds clamping
if (!MovementInput.IsZero())
{
FVector NewLocation = GetActorLocation();
NewLocation.X += MovementInput.Y * MoveSpeed * DeltaTime; // Forward/Back
NewLocation.Y += MovementInput.X * MoveSpeed * DeltaTime; // Left/Right
// Clamp to bounds
NewLocation.X = FMath::Clamp(NewLocation.X, BoundsMin.X, BoundsMax.X);
NewLocation.Y = FMath::Clamp(NewLocation.Y, BoundsMin.Y, BoundsMax.Y);
SetActorLocation(NewLocation);
}
// Auto-fire when holding fire button
if (bIsFiring)
{
FireTimer -= DeltaTime;
if (FireTimer <= 0.0f)
{
FireShot();
FireTimer = FireInterval;
}
}
}
void ASTGPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up Enhanced Input bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
// Movement
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ASTGPawn::Move);
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Completed, this, &ASTGPawn::Move);
// Fire
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Started, this, &ASTGPawn::StartFire);
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Completed, this, &ASTGPawn::StopFire);
// Special
EnhancedInputComponent->BindAction(SpecialAction, ETriggerEvent::Started, this, &ASTGPawn::UseSpecial);
}
}
void ASTGPawn::Move(const FInputActionValue& Value)
{
MovementInput = Value.Get<FVector2D>();
}
void ASTGPawn::StartFire(const FInputActionValue& Value)
{
bIsFiring = true;
FireTimer = 0.0f; // Fire immediately
}
void ASTGPawn::StopFire(const FInputActionValue& Value)
{
bIsFiring = false;
}
void ASTGPawn::FireShot()
{
// Will implement in Part 3 when we create bullets
UE_LOG(LogTemp, Warning, TEXT("FIRE!"));
}
void ASTGPawn::UseSpecial(const FInputActionValue& Value)
{
if (!bSpecialUsed)
{
bSpecialUsed = true;
UE_LOG(LogTemp, Warning, TEXT("SPECIAL ABILITY ACTIVATED!"));
// Will implement enemy/bullet destruction in Part 4
}
}
void ASTGPawn::TakeHit(int32 Damage)
{
CurrentLives = FMath::Clamp(CurrentLives - Damage, 0, MaxLives);
if (CurrentLives <= 0)
{
HandleDeath();
}
}
void ASTGPawn::HandleDeath()
{
SetActorHiddenInGame(true);
// Will notify GameMode in Part 6
}
void ASTGPawn::AddScore(int32 Points)
{
Score += Points;
}
Step 2.4: Compile the Code
In Unreal Editor
- Click "Compile" button (bottom right of the viewport)
- Wait for compilation (~30-60 seconds)
- Check for errors in Output Log
OR
In your IDE
- Build the project (
Ctrl+Shift+Bin Visual Studio, orCtrl+F9in Rider) - Wait for build to finish
- Unreal Editor will hot-reload automatically
Expected Result
- ✅ Compilation succeeds
- ✅ "C++ Classes" folder in Content Browser now shows
STGPawn - ✅ You can right-click STGPawn → "Create Blueprint Class based on STGPawn"
Step 2.5: Create Enhanced Input Assets
Unlike the deprecated Axis/Action Mappings, Enhanced Input uses data assets that you create in the Content Browser.
Create Input Actions
- In Content Browser, right-click → Input → Input Action
- Name it:
IA_Move - Double-click to open, set Value Type to
Axis2D (Vector2D) - Save and close
Repeat for:
IA_Fire(Value Type:Digital (bool))IA_Special(Value Type:Digital (bool))
Create Input Mapping Context
- In Content Browser, right-click → Input → Input Mapping Context
- Name it:
IMC_Player - Double-click to open
- Click "+" to add mappings:
For IA_Move:
- Add
IA_Move - Click "+" next to it to add keys:
- W → Modifiers: Add "Swizzle Input Axis Values" (YXZ)
- S → Modifiers: Add "Swizzle Input Axis Values" (YXZ), then "Negate"
- A → Modifiers: Add "Negate"
- D → (no modifiers needed)
For IA_Fire:
- Add
IA_Fire - Add keys: Space Bar, Left Mouse Button
For IA_Special:
- Add
IA_Special - Add keys: X, Right Mouse Button
- Save the Input Mapping Context
Step 2.6: Create Blueprint Child (for Visual Assets)
Now we create a minimal Blueprint that inherits from our C++ class. This Blueprint is ONLY for visual assets and input asset references!
- In Content Browser, navigate to
Content → Blueprints - Right-click → Blueprint Class
- In "Pick Parent Class" window:
- Click "All Classes" dropdown at top
- Search for
STGPawn - Select it
- Click "Select"
- Name it:
BP_Player - Double-click to open
In the Blueprint Editor
Components (Left Panel):
- You'll see all components we created in C++ (ShipMesh, Hitbox, Camera, etc.)
- NO NEED to add them manually - they're already there from C++!
Assigning Input Assets (Class Defaults):
- Click the "Class Defaults" button in the toolbar at the top of the Blueprint Editor (or press
Alt+Enter) - In the Details panel on the right, scroll down or search for "Input"
- You should see our custom Input properties (NOT the built-in Actor input settings):
- Default Mapping Context → Assign
IMC_Player - Move Action → Assign
IA_Move - Fire Action → Assign
IA_Fire - Special Action → Assign
IA_Special
- Default Mapping Context → Assign
⚠️ NOTE: If you only see "Block Input", "Auto Receive Input", and "Input Priority" - those are built-in Actor settings. Make sure you clicked "Class Defaults" button in the toolbar, and look for our custom properties labeled "Default Mapping Context", "Move Action", etc. They should be in a category called "Input" that we defined in C++.
Variables (My Blueprint Panel):
- You'll see all 12 variables we defined in C++ (MoveSpeed, BoundsMin, etc.)
- NO NEED to create them - they're already there from C++!
- You can see their default values (750.0, etc.) - all from C++!
What to do in Blueprint:
- Assign the input assets (as shown above)
- In Part 9, we'll assign visual meshes/materials here
Compile and Save BP_Player
Step 2.7: Test the Player
- Drag
BP_Playerfrom Content Browser into the level viewport - Position it near the center (coordinates around X=0, Y=0, Z=0)
- Set Auto Possess Player:
- With
BP_Playerselected in the viewport, look at the Details panel on the right - Scroll down to the Pawn section (or search for "Auto Possess")
- Find Auto Possess Player and set it to "Player 0"
- With
- Press Play (
Alt+P)
Expected Result
In Play Mode Window:
- You see the cone/cube player ship from above (top-down view)
- Ship is positioned at center of screen
- Background is the default gray/blue Unreal grid
- Camera follows the player from above
When pressing WASD keys:
- W - Ship moves upward on screen (toward top)
- S - Ship moves downward on screen (toward bottom)
- A - Ship moves left
- D - Ship moves right
- Movement stops immediately when you release keys (no sliding)
- Ship stops at the green debug border (the play area bounds)
- You can toggle this border off in BP_Player → Details → Debug → "Show Debug Bounds"
When pressing Space:
- You see "FIRE!" message appear in top-left corner of screen (yellow text)
- Message appears repeatedly while holding the button
- No bullets yet (we'll add those after creating bullet class)
When pressing X:
- "SPECIAL ABILITY ACTIVATED!" appears in top-left corner (cyan text)
- Pressing X again does nothing (ability used up)
Visual Check:
- Ship model is visible (cone or cube shape)
- No errors in Output Log
- Frame rate counter shows stable FPS (if enabled)
To exit Play mode: Press Esc or click the "Stop" button in the toolbar
Comparison: What We Just Did
Blueprint Approach (Part 2)
- Create BP_Player Blueprint ✅
- Add 5 components manually (clicking, dragging)
- Add 12 variables one-by-one (click +, type name, select type, compile, set value) × 12 = 15 minutes
- Create Blueprint nodes for movement logic (~30 nodes, connecting wires)
- Create Blueprint nodes for firing logic (~20 nodes)
- Create Enhanced Input assets
- Total: ~60 minutes
C++ Approach (This Part)
- Create STGPawn C++ class ✅
- Copy-paste header file with all variables 30 seconds
- Copy-paste implementation file
- Compile
- Create Enhanced Input assets (same in both approaches)
- Create BP_Player (inherits everything from C++)
- Total: ~15 minutes
Time saved: 45 minutes ⚡⚡⚡
What's Next?
In Part 3, we'll create the bullet class in C++. You'll see how defining bullet behavior in code is much cleaner than Blueprint nodes.
← Previous: Part 1 (C++) - Project Setup | Back to Index | Next: Part 3 (C++) - Create the Bullet →