Download

Best way to use a static function library in c++? Instancing etc...

Hi,
I am building up a static function library for use both in BP and c++, and I was wanting to use one of the static lib member funcs in my c++ pawn class.

Now I know I need to create an instance of the static lib object in order to invoke it’s member funcs… but I was curious if it was more *efficient *to create the instance in the ‘begin play’ of the pawn or to create the instance in the pawn’s member function where I specifically want to use the static lib func.

Thanks again!

(I can post code but doesn’t seem necessary…?)

You don’t need to create an instance of the static lib object to be able to use its static member functions. This works:

BPFuncLib.h


//includes etc.

UCLASS()
class PROJECT_API UBPFuncLib : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	virtual ~UBPFuncLib();

	UFUNCTION(BlueprintCallable, Category = "Your Category", Other Function Specifiers)
	static FYourReturnType Function(FYourFunctionParameters Parameters);
	
};


#include "BPFuncLib.h"

UBPFuncLib::Function(Parameters);

In case I’m understanding your question/problem correctly, you don’t have to worry about efficiency.

2 Likes

Ah interesting. See I was using Rama’s example:

and I realized that setting the (edit: didn’t mean to use the word virtual here) member functions to static allowed me to then do what you said (I was tripping a break trying to create an instance of the static lib class so I had to do some digging).

Would you be so kind as to explain as to why making it a virtual is useful and what does the ‘~’ do in that case as a prefix?

Thank you again!

The ‘~’ character denotes the destructor of the class. I don’t think you actually need it, I usually add it just in case I need it later.
It’s virtual so that any derived class destructors are called in order when a pointer to a base class is deleted.