Unreal Can't Compile After Adding Gun C++ Class

When I created the Gun C++ class, instead of creating in the default folder, I created in an Actors folder under the default. Creation was successful and VS2019 v 16.7 opened the .h and .cpp files.

Now, whenever I click Compile in Unreal Editor, I get an error message that Gun.h can not be opened. I’ve tried moving the C++ files to the default folder. I’ve tried refreshing the VS project from within the Editor, and I’ve tried adding the filepath to the include directories proeprty in VS. Still happens every time unless…

If I first Build the project in VS, then go into Unreal and compile, it will compile.

Any thoughts on the cause of this issue and possible fixes?

Could you show me your solution and header/cpp files?

Here are the Gun files:
Gun.h -

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"

UCLASS()
class SIMPLESHOOTER2_API AGun : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AGun();

	void PullTrigger();


protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

private:
	UPROPERTY(VisibleAnywhere)
	USceneComponent* Root;

	UPROPERTY(VisibleAnywhere)
	USkeletalMeshComponent* Mesh;

};

and Gun.cpp -

// Fill out your copyright notice in the Description page of Project Settings.


#include "Gun.h"
#include "Components/SkeletalMeshComponent.h"


// Sets default values
AGun::AGun()
{
	// 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;

	Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	SetRootComponent(Root);

	Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
	Mesh->SetupAttachment(Root);
}

// Called when the game starts or when spawned
void AGun::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AGun::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AGun::PullTrigger()
{
	UE_LOG(LogTemp, Warning, TEXT("You've been shot"));
}

Here is the .sln file:

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28315.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Engine", "Engine", "{94A6C6F3-99B3-346E-9557-ABF9D4064DBD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Games", "Games", "{8E2F6A87-1826-34F4-940C-CC23A48F9FE4}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UE4", "Intermediate\ProjectFiles\UE4.vcxproj", "{629E7246-6611-42B1-BB07-55B34B85FCCD}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimpleShooter2", "Intermediate\ProjectFiles\SimpleShooter2.vcxproj", "{17D79A47-0F6F-48FA-88AD-82DAF917489A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Visualizers", "Visualizers", "{1CCEC849-CC72-4C59-8C36-2F7C38706D4C}"
	ProjectSection(SolutionItems) = preProject
		..\..\..\..\..\..\..\Program Files\Epic Games\UE_4.25\Engine\Extras\VisualStudioDebugging\UE4.natvis = ..\..\..\..\..\..\..\Program Files\Epic Games\UE_4.25\Engine\Extras\VisualStudioDebugging\UE4.natvis
	EndProjectSection
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		DebugGame Editor|Win32 = DebugGame Editor|Win32
		DebugGame Editor|Win64 = DebugGame Editor|Win64
		DebugGame|Win32 = DebugGame|Win32
		DebugGame|Win64 = DebugGame|Win64
		Development Editor|Win32 = Development Editor|Win32
		Development Editor|Win64 = Development Editor|Win64
		Development|Win32 = Development|Win32
		Development|Win64 = Development|Win64
		Shipping|Win32 = Shipping|Win32
		Shipping|Win64 = Shipping|Win64
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.DebugGame Editor|Win32.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.DebugGame Editor|Win64.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.DebugGame|Win32.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.DebugGame|Win64.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.Development Editor|Win32.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.Development Editor|Win64.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.Development|Win32.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.Development|Win64.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.Shipping|Win32.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{629E7246-6611-42B1-BB07-55B34B85FCCD}.Shipping|Win64.ActiveCfg = BuiltWithUnrealBuildTool|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame Editor|Win32.ActiveCfg = Invalid|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame Editor|Win64.ActiveCfg = DebugGame_Editor|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame Editor|Win64.Build.0 = DebugGame_Editor|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame|Win32.ActiveCfg = DebugGame|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame|Win32.Build.0 = DebugGame|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame|Win64.ActiveCfg = DebugGame|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.DebugGame|Win64.Build.0 = DebugGame|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development Editor|Win32.ActiveCfg = Invalid|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development Editor|Win64.ActiveCfg = Development_Editor|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development Editor|Win64.Build.0 = Development_Editor|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development|Win32.ActiveCfg = Development|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development|Win32.Build.0 = Development|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development|Win64.ActiveCfg = Development|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Development|Win64.Build.0 = Development|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Shipping|Win32.ActiveCfg = Shipping|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Shipping|Win32.Build.0 = Shipping|Win32
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Shipping|Win64.ActiveCfg = Shipping|x64
		{17D79A47-0F6F-48FA-88AD-82DAF917489A}.Shipping|Win64.Build.0 = Shipping|x64
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(NestedProjects) = preSolution
		{629E7246-6611-42B1-BB07-55B34B85FCCD} = {94A6C6F3-99B3-346E-9557-ABF9D4064DBD}
		{17D79A47-0F6F-48FA-88AD-82DAF917489A} = {8E2F6A87-1826-34F4-940C-CC23A48F9FE4}
	EndGlobalSection
EndGlobal

When first created under the Actors folder, both Unreal and VS produced error messages that Gun.h could not be opened despite Gun.h being created in Unreal and being open for editing within VS. Adding the filepath to the Actors folder allowed VS to see Gun.h but not Unreal. So I could compile in VS but the results didn’t flow to Unreal (changes to the Gun and later to the skeltal mesh did not show up in Unreal despite C++ code and VS compile).

Finally, compilation from the Unreal Editor worked this morning without first compiling in VS. Very weird behavior.

And now, after completing a couple more sessions, the bug returns. VS can build with nor problem, then I can compile in Unreal. But, if I try to compile in Unreal first, I get an error that Gun.h can not be opened.

Sorry by solution I meant a screenshot of it although I’m now realising that it’s not going to show anything useful as it’s not necessarily going to show where files are on disk.

Unreal only adds your Source directory to the include path as well as any folders named “Public” within that. So if you’re trying to include something it has to be relative to that Source directory or the current file you’re writing that include in.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms