UGameplayStatics is not a class or namespace name error?!

I created a new cpp class called Gun which inherits from Actor.

I am currently using UE4.16.2

When I attempt to compile the following code:

CPP:

#include “Gun.h”
void AGun::Fire() {
// try and play the sound if specified
if (FireSound != NULL)
{
UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
}
}
Header:

public:	
	// Sets default values for this actor's properties
	AGun();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Gameplay")
		class USoundBase* FireSound;
void Fire();

I get the following error:

CompilerResultsLog:Error: Error E:\UnrealProjects\SuperHot\Source\SuperHot\Gun.cpp(35) : error C2653: ‘UGameplayStatics’: is not a class or namespace name

CompilerResultsLog:Error: Error E:\UnrealProjects\SuperHot\Source\SuperHot\Gun.cpp(35) : error C3861: ‘PlaySoundAtLocation’: identifier not found

It would be useful if you could include your full header and cpp file including ‘include statements’. Are you sure you have included all relevant classes?

This is everything I have included, I am not certain that I have included all relevant classes as it is not working yet when I look at the example within the firstpersoncpp file they run the same code without including any extra files

This is exactly what I was missing, I have placed my own sounds into an engine I have created before but this is the first time i’m implementing sound through c++ in UE4, I was wondering why the firstpersoncharacter c++ class does not need to include this, I assume it is included somewhere within the hierarchy but I was unable to find it

Yup, This is why I was asking about including relevant classes. You are inheriting from Actor. GameplayStatics is included in Pawn.cpp and since Character Inherits from Pawn, it has access to it.

#include “Kismet/GameplayStatics.h”

2 Likes