Need help with GetAllActorsOfClass() in a static function library

Hi,

I am really new to programming, so the answer is probably obvious but I just can’t figure it out for hours now.

Basically I created a new class deriving from BlueprintFunctionLibrary to have some static functions in it that I can use anywhere. One of the functions would return a TArray with all the actors of the specified class using GetAllActorsOfClass(). However I am having trouble defining the function, no matter what I try it gives me different errors. Here is my code:

MyBlueprintFunctionLibrary.h

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

/**
 * 
 */
UCLASS()
class PROTOTYPEGAME_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:

	TSubclassOf<class AMasterCell> MasterCellClass;
	
	static TArray<AMasterCell> GetAllCells(AGameMode* CurrentGameMode);	
	
};

MyBlueprintFunctionLibrary.cpp

#include "PrototypeGame.h"
#include "MyBlueprintFunctionLibrary.h"

TArray<AMasterCell>  UMyBlueprintFunctionLibrary::GetAllCells(AGameMode* CurrentGameMode)
{
	TArray<AMasterCell*> ExistingCells;

	UGameplayStatics::GetAllActorsOfClass(CurrentGameMode->GetWorld(), MasterCellClass, ExistingCells);

	return ExistingCells;
}

This gives me various errors at the GetAllActorsOfCall() line. The parameter MasterCellClass is underlined and the error says:

-a nonstatic member reference must be relative to a specific object.

I can resolve this by declaring MasterCellClass static (however not sure why). But the second error is what I can’t get rid of, ExistingCells is underlined as well saying:

-a reference of type “TArray &” (not const-qualified) cannot be initialized with a value of type “TArray”

Can someone please enlighten me whats wrong? I tried to google the errors but the explanations didn’t really make sense to me probably because of the lack of my experience.

Any help would be really appreciated.
Thanks.

There’s a typo in your function signature (both the declaration and definition)

You’ve written the return type as

TArray<AMasterCell>

when it should be

TArray<AMasterCell*>

As for the other issue which you worked around, within a static function you cannot use a non-static variable because you need an instance, an actual object of type UMyBlueprintFunctionLibrary from which to extract the variable. An instance of a BP library would of course, be illogical.

In any case I’d consider whether you really want to make MasterCellClass a member variable (static or otherwise) of the library. This is not how BP libraries are conventionally used.

If you look at KismetSystemLibrary.h for example, these are meant to be used a collection of static functions that are self-contained and can be conveniently called from blueprints or C++. Perhaps considering making mastercellclass a parameter.