"UGameplayStatics::GetAllActorsOfClass" is inaccessible

Hey guys! I’ve been getting an error in visual studio which is keeping my project from compiling and i’m not entirely sure why.

You have to include its module in your build.cs file and then include its header in your code.

Is it actually preventing your project from compiling or is it just in your IDE’s error list?

I don’t know about 4.21 as I haven’t migrated from 4.20 yet, but if you’re in 4.20 there’s some known bugs about Visual Studio intellisense. I myself see this inaccessible message quite often for no reason whatsoever. You can always go see the header of UGameplayStatics and see for yourself if the function is actually public or not. Spoiler, it will be :slight_smile:

I don’t why they use error list panel… Epic has guides explaining why it doesn’t work with Unreal code…

It’s actually preventing it from compiling which is odd because I thought it was a false positive at first. The code is part of a course i’m taking for which that code is being prepped for blueprint

Thanks for the reply mate! Although I still don’t quite understand. Would it be helpful if I posted my project files?

Inaccessible means it’s not public. That function only exists to expose the functionality for blueprints. In C++ you use TActorIterator, which is what that function uses:



// With hard coded class
for (TActorIterator<AMyActorClass*> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
  // do stuff with your actor here using ActorItr
}

// With a class parameter called
TSubClassOf<AActor> ClassParam = AMyActorClass::StaticClass();
for (TActorIterator<AActor*> ActorItr(GetWorld(), ClassParam); ActorItr; ++ActorItr)
{
  // do stuff with your actor here using ActorItr
}


The function is static, it is accessible from anywhere if the module and header file are properly included.

Yeah that’s weird, there’s no reason not to use GetAllActorsOfClass in c++. Maybe an issue with your included headers? Make sure that at some point you are including the core includes (engine.h etc… I think ?), usually through including your project.h which itself includes the required stuff.

You could try to directly include the header for UGameplayStatics and see if that works, though only as a debugging tool, it’s too dirty as an actual solution.

This does not explain why your IDE would show an inaccessible error, but that may be a false positive hiding something else.

Is this method r better than actor iterator?

It is an actor iterator, so nope!

The IDE shows an inaccessible error because of the difference between GENERATED_BODY() and GENERATED_UCLASS_BODY(). The latter emits a public specifier at the end and the first one does not. Older source uses the latter macro like in UGameplayStatics



// .. headers

UCLASS()
class ENGINE_API UGameplayStatics : public UBlueprintFunctionLibrary
{
    GENERATED_UCLASS_BODY()

//===============
// some static functions here
//================

    UFUNCTION(BlueprintCallable, Category="Utilities",  meta=(WorldContext="WorldContextObject", DeterminesOutputType="ActorClass", DynamicOutputParam="OutActors"))
    static void GetAllActorsOfClass(const UObject* WorldContextObject, TSubclassOf<AActor> ActorClass, TArray<AActor*>& OutActors);

//=========
// rest of the source here
//============

};


Notice there is no “public:” identifier between the start of the class, the macro and the getallactors function. In c++ if you don’t add an access specifier everything is assumed private. This is an error because the public specifier is supposed to be emiited from the macro which the IDE seems unable to comprehend. Contrast this with new code that uses the GENERATED_BODY() macro where the specifier is not assumed and will be explicitly provided in the source. So everything works there.

That still leaves false positive all over the codebase and apparently Epic has decided this is not important it has still not been fixed in 4.21

Hey all thank you so much for your help!