Can you use a Blueprint Function Library in an Object Class

Your Object needs its own “World Context”, This is how you can do that in c++

.h file

#pragma once

#include "CoreMinimal.h"
#include "UObject/Object.h"

#include "RRObject.generated.h"
/**
 * 
 */
UCLASS(BlueprintType, Blueprintable)
class IKEABR_SERVER_API URRObject : public UObject
{
	GENERATED_BODY()

protected:
	struct FWorldContext* WorldContext;


	UFUNCTION(BlueprintCallable)
		void SetWorldContext(UObject* NewWorldContext);

	UFUNCTION(BlueprintCallable)
		UObject* GetWorldContextObject();

	//~ Begin UObject Interface
	virtual class UWorld* GetWorld() const final;
	struct FWorldContext* GetWorldContext() const { return WorldContext; };
	
};

.cpp file

#include "RRObject.h"

void URRObject::SetWorldContext(UObject* NewWorldContext)
{
        WorldContext = new FWorldContext();
	WorldContext->SetCurrentWorld(NewWorldContext->GetWorld());
}

UObject* URRObject::GetWorldContextObject()
{
	return WorldContext->World();
}

class UWorld* URRObject::GetWorld() const
{
	return WorldContext ? WorldContext->World() : NULL;
}

Then set the world context object with “SetWorldContext” from somewhere else (like the game instance) and you can call blueprint function library functions within any child blueprints of this class.

4 Likes