USphereComponents showing up as "undeclared identifiers" despite necessary includes

Hello all. I am having an issue with two USphereComponents inside of my class.

Any mention of them in the constructor gives me this error :

C2065 ‘DetectionSphere’ undeclared identifier

USphereComponent is forward declared in my .h file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "BehaviorTree/BehaviorTree.h"
#include "PlayerCharacter.h"
#include "TimerManager.h"
#include "ISpudObject.h"
#include "MotherCharacter.generated.h"

class USphereComponent;

UCLASS()
class ATTEMPTTWO_API AMotherCharacter : public ACharacter, public ISpudObject
{
	GENERATED_BODY()

public:
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spheres")
	USphereComponent* DetectionSphere;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spheres")
	USphereComponent* KillPlayerSphere;

and has the necessary include in the .cpp file:

#include "MotherCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "MotherSpawner.h"
#include "Components/SphereComponent.h"

AMotherCharacter::AMotherCharacter()
{
	PrimaryActorTick.bCanEverTick = true;

	DetectionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Detection Sphere"));
	DetectionSphere->SetupAttachment(RootComponent);
	DetectionSphere->SetGenerateOverlapEvents(true);

	KillPlayerSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Kill Player Sphere"));
	KillPlayerSphere->SetupAttachment(RootComponent);
	KillPlayerSphere->SetCollisionProfileName("OverlapAll");
}

Below is a picture of the errors I get when I try to compile:

I have not had any issues with this file for months, and all of a sudden today it wont compile no matter what I try. I’ve regenerated my project files as well but to no avail. Every post I’ve found online is similar, but not identical to my issue.

Any help is appreciated!

You’re looking too far down in the error list. That’s not a “real” error but a secondary error coming from one of the earlier ones. From the screen-shot I know something else is wrong because of the error reporting a syntax error on line 10.

I suspect there’s an error further up telling you that AMotherCharacter() is not a function of the AMotherCharacter class and that you’ve written a definition for the constructor but not declared it in the header anywhere. But I can’t be 100% sure on that with only snippets.

Generally you want to try and resolve errors from the top down for each file.

1 Like

Thank you for your response, I have added the constructor into the header and all the same errors are still showing up, with the syntax error being at the top of the list. Do you think this is indicative of a separate issue entirely?

You’d need to share your new source files and the new errors. Get the errors from the output window and not the VS error list.

Copy that, here are the source files and errors as they are now.

.h:


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "BehaviorTree/BehaviorTree.h"
#include "PlayerCharacter.h"
#include "TimerManager.h"
#include "ISpudObject.h"
#include "MotherCharacter.generated.h"

class USphereComponent;

UCLASS()
class ATTEMPTTWO_API AMotherCharacter : public ACharacter, public ISpudObject
{
	GENERATED_BODY()

public:
	AMotherCharacter();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spheres")
	USphereComponent* DetectionSphere;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spheres")
	USphereComponent* KillPlayerSphere;

.cpp:


#include "MotherCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "MotherSpawner.h"
#include "TimerManager.h"
#include "Components/SphereComponent.h"

AMotherCharacter::AMotherCharacter()
{
	PrimaryActorTick.bCanEverTick = true;

	DetectionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Detection Sphere"));
	DetectionSphere->SetupAttachment(RootComponent);
	DetectionSphere->SetGenerateOverlapEvents(true);

	KillPlayerSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Kill Player Sphere"));
	KillPlayerSphere->SetupAttachment(RootComponent);
	KillPlayerSphere->SetCollisionProfileName("OverlapAll");
}

Error list from output window:

Build started at 4:17 PM...
1>------ Build started: Project: AttemptTwo, Configuration: Development_Editor x64 ------
1>Using bundled DotNet SDK version: 6.0.302
1>Running UnrealBuildTool: dotnet "..\..\Engine\Binaries\DotNET\UnrealBuildTool\UnrealBuildTool.dll" AttemptTwoEditor Win64 Development -Project="C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\AttemptTwo.uproject" -WaitMutex -FromMsBuild
1>Log file: C:\Users\JakeS\AppData\Local\UnrealBuildTool\Log.txt
1>Creating makefile for AttemptTwoEditor (command line arguments changed)
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\AttemptTwo.Build.cs : warning : Referenced directory 'C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\C\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Plugins\SPUD\Source\SPUD\Public' does not exist.
1>Building AttemptTwoEditor...
1>Using Visual Studio 2022 14.36.32546 toolchain (C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532) and Windows 10.0.22621.0 SDK (C:\Program Files (x86)\Windows Kits\10).
1>Determining max actions to execute in parallel (24 physical cores, 32 logical cores)
1>  Executing up to 24 processes, one per physical core
1>  Requested 1.5 GB memory per action, 19.41 GB available: limiting max parallel actions to 12
1>------ Building 7 action(s) started ------
1>[1/7] Compile [x64] Module.AttemptTwo.4.cpp
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(10): error C2059: syntax error: ';'
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(12): error C2065: 'DetectionSphere': undeclared identifier
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(13): error C2065: 'DetectionSphere': undeclared identifier
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(14): error C2065: 'DetectionSphere': undeclared identifier
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(16): error C2065: 'KillPlayerSphere': undeclared identifier
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(17): error C2065: 'KillPlayerSphere': undeclared identifier
1>C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\Source\AttemptTwo\MotherCharacter.cpp(18): error C2065: 'KillPlayerSphere': undeclared identifier
1>[2/7] Compile [x64] Module.AttemptTwo.2.cpp
1>[3/7] Compile [x64] Module.AttemptTwo.1.cpp
1>[4/7] Compile [x64] Module.AttemptTwo.3.cpp
1>Total time in Parallel executor: 2.38 seconds
1>Total execution time: 3.57 seconds
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.MakeFile.Targets(44,5): error MSB3073: The command ""C:\Program Files\Epic Games\UE_5.3\Engine\Build\BatchFiles\Build.bat" AttemptTwoEditor Win64 Development -Project="C:\Users\JakeS\OneDrive\Documents\Unreal Projects\AttemptTwo\AttemptTwo.uproject" -WaitMutex -FromMsBuild" exited with code 6.
1>Done building project "AttemptTwo.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 10 up-to-date, 0 skipped ==========
========== Build completed at 4:17 PM and took 03.831 seconds ==========

You should share your full .h. I suspect there’s something wrong there given all the includes you have but don’t need for that snippet.

You may also have something wrong in your AttemptTwo.Build.cs.

Here is the full .h file, and I will look into the build.cs file as well.


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "BehaviorTree/BehaviorTree.h"
#include "PlayerCharacter.h"
#include "TimerManager.h"
#include "ISpudObject.h"
#include "MotherCharacter.generated.h"

class USphereComponent;

UCLASS()
class ATTEMPTTWO_API AMotherCharacter : public ACharacter, public ISpudObject
{
	GENERATED_BODY()

public:
	AMotherCharacter();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spheres")
	USphereComponent* DetectionSphere;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spheres")
	USphereComponent* KillPlayerSphere;

	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player")
	APlayerCharacter* playerChar;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player")
	bool bIsSeenByPlayer = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
	bool bHasJustSpawned = true;

	UPROPERTY()
	FGuid SpudGuid;

	UBehaviorTree* GetBehaviorTree() const;

	UFUNCTION(BlueprintImplementableEvent)
	void TurnTowardsPlayer();

	UFUNCTION(BlueprintImplementableEvent)
	void TurnToOriginalPos(float zrot);

	UFUNCTION(BlueprintImplementableEvent)
	void KillPlayerBP();

	UFUNCTION(BlueprintCallable)
	void InitializeOnSpawn();

	UFUNCTION(BlueprintImplementableEvent)
	void InitializeOnSpawnBP();

	UFUNCTION(BlueprintImplementableEvent)
	void DespawnIfBlocked();

	UFUNCTION(BlueprintImplementableEvent)
	void CallDespawnIfNotSpotted();

	UFUNCTION()
	bool bIsLineTraceToPlayerBlocked();

protected:
	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Behavior Tree", meta = (AllowPrivateAccess = "true"))
	UBehaviorTree* BehaviorTree;

	UFUNCTION()
	void DetectionSphereBeginOverlap(UPrimitiveComponent* MyComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
		int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION()
	void DetectionSphereEndOverlap(UPrimitiveComponent* MyComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
		int32 OtherBodyIndex);

	UFUNCTION()
	void KillSphereBeginOverlap(UPrimitiveComponent* MyComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
		int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	FTimerHandle PotentialDespawnTimer;
	
	FTimerHandle TimerAfterSpawn;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
	float TimeWasBlocked = 0.0f;

	UPROPERTY()
	float CurrentTime = 0.0f;

private:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Rotation", meta = (AllowPrivateAccess = "true"))
	float ZRotPrePlayerInRadius;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Detection", meta = (AllowPrivateAccess = "true"))
	bool bPlayerInDetecSphere = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Detection", meta = (AllowPrivateAccess = "true"))
	bool bIsGivingUpSearch = false;

	void DisableWasJustSpawned();

	bool bCalledDespawn = false;
};

That all looks fine to me. If it’s still not building after you fix the build.cs the only other thing I can recommend is simplifying the whole change down to an empty class and then adding things back a bit at a time.

1 Like

Yeah, I was planning on doing that if I had no luck on here. I appreciate the help and will make sure to post an update if anything else comes up!