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.

  1. 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);
}

  1. Creating a New C++ Class

Step-by-Step Guide

  1. Open Your Project in Unreal Engine:

    • Launch Unreal Engine and open your project.
  2. Add a New C++ Class:

    • Go to the File menu and select New C++ Class.
    • Choose a parent class. For this example, select Actor.
    • Click Next.
  3. Name Your Class:

    • Enter a name for your class, e.g., MyActor.
    • Click Create Class.
  4. 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.

  1. 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;
}

  1. 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):

void AMyActor::IncrementMyInt()
{
    MyInt++;
}

Using the Class in Blueprints

  1. Compile the Project:

    • Ensure your project is compiled.
  2. Create a Blueprint Based on Your C++ Class:

    • In the Content Browser, right-click and select Blueprint Class.
    • Choose MyActor as the parent class.
  3. 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

  1. Create a new C++ class called MyCharacter derived from ACharacter.
  2. Add a float member variable called Health.
  3. Initialize Health to 100.0f in the constructor.
  4. Create a BlueprintCallable function called TakeDamage that decreases Health 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.

© Copyright 2024. All rights reserved