In this section, we will delve into creating C++ classes in Unreal Engine. This is a crucial skill for leveraging the full power of Unreal Engine, as it allows for more control and performance optimization compared to Blueprints.
Objectives
By the end of this section, you will:
- Understand the structure of a C++ class in Unreal Engine.
- Learn how to create a new C++ class.
- Implement basic functionality in a C++ class.
- Integrate the C++ class with Unreal Engine's Blueprint system.
- Understanding the Structure of a C++ Class
A typical C++ class in Unreal Engine consists of two main files:
- Header File (.h): Contains the class declaration, including member variables and function prototypes.
- Source File (.cpp): Contains the implementation of the class functions.
Example Structure
// MyActor.h #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyActor.generated.h" UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AMyActor(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; };
// MyActor.cpp #include "MyActor.h" // Sets default values AMyActor::AMyActor() { // Set this actor to call Tick() every frame. PrimaryActorTick.bCanEverTick = true; } // Called when the game starts or when spawned void AMyActor::BeginPlay() { Super::BeginPlay(); } // Called every frame void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); }
- Creating a New C++ Class
Step-by-Step Guide
-
Open Your Project in Unreal Engine:
- Launch Unreal Engine and open your project.
-
Add a New C++ Class:
- Go to the
File
menu and selectNew C++ Class
. - Choose a parent class. For this example, select
Actor
. - Click
Next
.
- Go to the
-
Name Your Class:
- Enter a name for your class, e.g.,
MyActor
. - Click
Create Class
.
- Enter a name for your class, e.g.,
-
Compile the Project:
- After creating the class, Unreal Engine will open your IDE (e.g., Visual Studio).
- Compile the project to ensure everything is set up correctly.
- Implementing Basic Functionality
Adding a Member Variable
Let's add a simple member variable to our class.
Header File (MyActor.h):
public: // A simple integer variable UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="My Variables") int32 MyInt;
Initializing the Variable
Source File (MyActor.cpp):
// Sets default values AMyActor::AMyActor() { // Set this actor to call Tick() every frame. PrimaryActorTick.bCanEverTick = true; // Initialize the variable MyInt = 42; }
- Integrating with Blueprints
Exposing Functions to Blueprints
You can expose functions to Blueprints using the UFUNCTION
macro.
Header File (MyActor.h):
public: // A simple function to increment MyInt UFUNCTION(BlueprintCallable, Category="My Functions") void IncrementMyInt();
Source File (MyActor.cpp):
Using the Class in Blueprints
-
Compile the Project:
- Ensure your project is compiled.
-
Create a Blueprint Based on Your C++ Class:
- In the Content Browser, right-click and select
Blueprint Class
. - Choose
MyActor
as the parent class.
- In the Content Browser, right-click and select
-
Add the Blueprint to Your Level:
- Drag the Blueprint into your level.
- You can now use the exposed variables and functions in the Blueprint Editor.
Practical Exercise
Task
- Create a new C++ class called
MyCharacter
derived fromACharacter
. - Add a
float
member variable calledHealth
. - Initialize
Health
to100.0f
in the constructor. - Create a BlueprintCallable function called
TakeDamage
that decreasesHealth
by a specified amount.
Solution
Header File (MyCharacter.h):
#pragma once #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "MyCharacter.generated.h" UCLASS() class MYPROJECT_API AMyCharacter : public ACharacter { GENERATED_BODY() public: // Sets default values for this character's properties AMyCharacter(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; // Health variable UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Character Stats") float Health; // Function to take damage UFUNCTION(BlueprintCallable, Category="Character Functions") void TakeDamage(float DamageAmount); };
Source File (MyCharacter.cpp):
#include "MyCharacter.h" // Sets default values AMyCharacter::AMyCharacter() { // Set this character to call Tick() every frame. PrimaryActorTick.bCanEverTick = true; // Initialize Health Health = 100.0f; } // Called when the game starts or when spawned void AMyCharacter::BeginPlay() { Super::BeginPlay(); } // Called every frame void AMyCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); } // Function to take damage void AMyCharacter::TakeDamage(float DamageAmount) { Health -= DamageAmount; }
Conclusion
In this section, you learned how to create and implement a C++ class in Unreal Engine. You also learned how to expose variables and functions to Blueprints, allowing for a seamless integration between C++ and Blueprint scripting. This foundational knowledge will enable you to create more complex and efficient gameplay mechanics in your Unreal Engine projects.
Unreal Engine Course
Module 1: Introduction to Unreal Engine
- What is Unreal Engine?
- Installing Unreal Engine
- Navigating the Interface
- Creating Your First Project
Module 2: Basic Concepts
Module 3: Intermediate Blueprints
Module 4: Advanced Blueprints
Module 5: C++ Programming in Unreal Engine
- Setting Up Your Development Environment
- Basic C++ Syntax
- Creating C++ Classes
- Integrating C++ with Blueprints
Module 6: Advanced C++ Programming
Module 7: Advanced Topics
- Physics and Collision
- Rendering and Post-Processing
- Procedural Content Generation
- Virtual Reality Development