Hi there,
I’m trying to add some C++20 modules in the .cpp file. My project is based on Unreal Engine CPP Quick Start . After doing that, I got a floating rotating object. Now I want to import some modules, and below is my attempt:
Add a module file /Source/QuickStart/MyModule.ixx
export module MyModule;
export float MyFunc()
{
return 20.0f;
}
Modify the build file /Source/QuickStart/QuickStart.Build.cs
using UnrealBuildTool;
public class QuickStart : ModuleRules
{
public QuickStart(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
CppStandard = CppStandardVersion.Cpp20;
}
}
Modify the cpp file /Source/QuickStart/FloatingActor.cpp
#include "FloatingActor.h"
import MyModule; // <--------- note
// Sets default values
AFloatingActor::AFloatingActor()
{
// 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;
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
if (CubeVisualAsset.Succeeded())
{
VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}
}
// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLocation = GetActorLocation();
FRotator NewRotation = GetActorRotation();
float RunningTime = GetGameTimeSinceCreation();
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * MyFunc(); // <----- note
float DeltaRotation = DeltaTime * MyFunc(); // <----- note
NewRotation.Yaw += DeltaRotation;
SetActorLocationAndRotation(NewLocation, NewRotation);
}
Then close the Unreal Editor, locate the project root path, right click “QuickStart.uproject”, click “Generate Visual Studio project files”, open “QuickStart.sln”, and rebuild project.
After that, I get this error:
1>E:\Unreal Projects\QuickStart\Source\QuickStart\FloatingActor.cpp(6): error C2230: could not find module 'MyModule'
1>E:\Unreal Projects\QuickStart\Source\QuickStart\FloatingActor.cpp(42): error C3861: 'MyFunc': identifier not found
1>E:\Unreal Projects\QuickStart\Source\QuickStart\FloatingActor.cpp(43): error C3861: 'MyFunc': identifier not found
I have checked the Solution Explorer and notice that MyModule.ixx is already contained in QuickStart/Source/QuickStart, so I don’t have a clue why the build system skipped it. Do you have same experiences and know how to fix it? Thanks in advance.