Integrating C++ and Blueprints
This semester I used one project for multiple classes. One was an entry-level game design course where we used Blueprints to script and another was a programming course where we use C++. We started by creating a Base Pickup using C++ and inheriting from that class, creating multiple pickups. Health, Shield, Spring Shoes, and Speed Boost.
We’re also using an interface to share functions between the Pickup and the player.
iInteract.h
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "iInteract.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UiInteract : public UInterface;
class WVDOOM_API IiInteract
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interaction")
void OnInteract(AActor* PlayerActor);
};
iInteract.cpp
#include "iInteract.h"
Pickup.h
#pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "iInteract.h" #include "Pickup.generated.h" class UBoxComponent; UCLASS() class WVDOOM_API APickup : public AActor, public IiInteract { GENERATED_BODY() public: // Sets default values for this actor's properties APickup(); public: UPROPERTY(EditAnywhere, BlueprintReadWrite) USceneComponent* PickupRoot; UPROPERTY(EditAnywhere, BlueprintReadWrite) UStaticMeshComponent* PickupMesh; UPROPERTY(EditAnywhere) UBoxComponent* PickupBox; UFUNCTION(BlueprintCallable, Category = Pickup) void SendPickupToActor(AActor* PlayerActor); void OnInteract(AActor* PlayerActor); };
Pickup.cpp
#include "Pickup.h" #include "Engine/World.h" // Sets default values APickup::APickup() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; PickupRoot = CreateDefaultSubobject<USceneComponent>(TEXT("PickupRoot")); RootComponent = PickupRoot; PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh")); PickupMesh->AttachToComponent(PickupRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale); } void APickup::SendPickupToActor(AActor* PlayerActor) { FVector actorLocation = PlayerActor->GetActorLocation(); FVector pickupLocation = PickupMesh->GetComponentLocation(); PickupMesh->SetWorldLocation(FMath::Lerp(PickupMesh->GetComponentLocation(), actorLocation, 0.01f)); }
We haven’t implemented the interface yet as the current character does not inherit from a C++ class but will in the future.
Creating a Blueprint from this C++ class and then make children of that Blueprint.
One of the biggest problems with using this kind of project between a group of new students and more experienced students is that the newer students felt overwhelmed with generating the project files, making sure they had all the proper SDK’s when many had never heard of an SDK before. All the students were able to compile the project and eventually got more comfortable with the process.
The project is still being used in some of the classes and can be found here.