How to reference "this" class for BlueprintCallable

I need to get a reference to my own class to be used in Blueprints for a UI. However apparently UFUNCTION(BlueprintCallable) does not allow for references.
What is the correct way to get a reference to my class in blueprints to be used as the target?

This class I have specifically can also be made a static class, however I am curious on how you would

  1. Reference self class
  2. How to make this a static class to be used in UGameplayStatics

For reference, this does not work:

	UFUNCTION(BlueprintCallable, Category = "Resource Manager")
	USResourceManager& GetResourceManagerClass() { return *this; }

You can use GetClass() to get the actual class of an object. Similarly, there’s a Blueprint GetClass node.

So it would be:

UFUNCTION(BlueprintCallable, Category = "Resource Manager")
UClass *GetResourceManagerClass() { return GetClass(); }

This works in getting the class of the object, however I need to reference this in Blueprints.

GetResourceManagerClass() in this case also requires a target, however I am attempting to get this specific instance of ResourceManager to be used non-statically in Blueprints. The return object in this case is also “Object Class” which I suppose is fine as I can cast it to ResourceManager. However I need to not have a “target” pin on the BP for GetResourceManagerClass()

To get rid of the “target” pin, make the function static:

UFUNCTION(BlueprintCallable, Category = "Resource Manager")
static UClass *GetResourceManagerClass(UObject *SomeResourceManager) { return SomeResourceManager->GetClass(); }

I feel like I might be missing the point though. What are you starting with and what are you trying to get to?

Essentially I have a singleton class in this case, however I wanted to attempt to have it perform non-static as it isn’t necessary when calling the functionality through C++. The problem is when it gets to Blueprints, I cannot use any of the functionality without a reference to the ResourceManager, and it appears that BP does not allow me to do this unless I have an extended blueprint of the class and to instantiate everything over this blueprint instead. ( I don’t want to have a BP class for this, and rather use the native C++ class ).

I want to call functionality on my class through reference to the instance and not by declaring it static.

Okay. If your ResourceManager functions can be made static, I’d recommend doing so. You can even create a BlueprintFunctionLibrary if you want to extend your C++ with blueprints.

If your functions actually do require a reference to a ResourceManager instance, a good place to put that instance is on a subclass of GameMode and populate it in the construction script. Unfortunately, you will still need to cast the GameMode to your subclass in order to gain access to the ResourceManager instance.

I just reread your comment and you totally should be able to call a BlueprintCallable static function defined in C++ from Blueprints. Your class might need to extend UBlueprintFunctionLibrary Blueprint Function Libraries in Unreal Engine | Unreal Engine 5.2 Documentation

Reflection system don’t support references (&) of UObject only pointers of objects and (maybe never tried that) references of a pointer. In general UObject sould refrence only with compinters i never seen other case,

Safest way to do it is to keep pointer to a more global class, let’s say AGameMode and then make static function which will grab that varable from game mode and return it like this

.h

UCLASS()
class AMyShinyGamemode : AGameMode {
      GENERATED_BODY()
public:
     virtual void BeginPlay() override;

     UPROPERTY()
     USResourceManager* ResourceManager;

     UFUNCTION(BlueprintPure, meta=(WorldContext="WorldContextObject"),  Category = "Resource Manager")
     static USResourceManager* GetResourceManager(UObject* WorldContextObject);
};

.cpp

void AMyShinyGamemode::BeginPlay() {
     Super::BeginPlay();
     ResourceManager = ()->SpawnActor<AYourShinyResourceManager>();
}

USResourceManager* AMyShinyGamemode::GetResourceManager(UObject* WorldContextObject) {

     if(GEngine) {
         UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
         AMyShinyGamemode* GameMode = World->GetAuthGameMode<AMyShinyGamemode>();
         if(GameMode) return GameMode->ResourceManager;
     }
     return nullptr;
}

Note that making UBlueprintFunctionLibrary is not needed it’s something some people misundersand, static functions nodes works from anywhere. UBlueprintFunctionLibrary is made for function not related to specific class or are related to non-UObject class

Thanks for this answer, this was primarily the method I was going to use to resolve this problem.

Is there a reason UE chooses not to allow for references in UObjects?

The node in BP for this function also requires an execution pin. Why does something like GetPlayerController not require an execution pin whereas something like this function does?

What if you try this…:

UFUNCTION(BlueprintCallable, Category = "Resource Manager")
const USResourceManager* GetResourceManagerClass() {
     return GetDefault<USResourceManager>();
}

Or maybe this:

 UFUNCTION(BlueprintCallable, Category = "Resource Manager")
 const USResourceManager* GetResourceManagerClass() {
      return GetMutableDefault<USResourceManager>();
 }

It’s due to fact UObjects are managed, they can be only stored in pointers and be created only via UObject functions (NewObject, SpawnActor), thats why refection system only supports that, other reason is blueprints supports only one kind of each type and refrence of that type, which is non-pointer, but UObject is exception to that due to fact they can be only pointers.

Ah yes my mistake, you should use BlueprintPure insted of BlueprintCallable

Why default? Object needs to be created first

Because he says he want it to act like a static object, so there’s no need to instantiate the object in world since the values in the class will be the same for every actor reading from it, through the blueprint nodes.

But he would interact with default object insted one in the game, any memoery modifications there could also mess up state of spawned objects in the future

Where does GetDefault() get the object from, if not initialized by NewObject? Does it take the first instance of NewObject or is this a different case?

It return class default object (CDO) contain defaults and it works like matrix for new objects, you can read defaults from it, but you should not modify it as you will modify state of new objects in future

Thanks a lot for the help, gained a ton of knowledge from your answers.