static functions callable from both c++ and blueprints

Hi,

I have 2 c++ classes : A and B

I have 2 blueprints A_BP who inherits from A, and B_BP who inherits from B

I’d like to create a c++ class “MyMaths” whose every functions are statics and can be used in both my c++ classes and my blueprints

How to do so please ?

Blueprints can use static Ufunctions



public :

UFUNCTION (BlueprintCallable)
static MyFunc(...);


thanks Bruno, and when creating my new class “MyMaths”, which class should be its parent ? I have to define a parent class, should i chose “None” ?

You can use “Blueprint Function Library” class for that. Any subclass of “UObject” can do it, but since Epic added a FunctionLibrary class for that, why not use it…

1 Like

Thank you so much

Can i call any of these “Blueprint Function Library” functions from a another c++ class ? Or these can only be called from blueprints ?

Yes, just include its headers to use them in Cpp

This is awesome. Thanks again Bruno

Just to add - if you do want your static function to be exposed to blueprint, you must implement GetWorld()


virtual UWorld* GetWorld() const override;

That’s not correct. You can pass a WorldContext=Object metadata to static uFunctions.
So overriding GetWorld() is not a requirement.

You only need to do this if your function requires a [FONT=Courier New]UWorld* pointer for something.

Minimal example:



UFUNCTION(BlueprintCallable, meta = (WorldContext="WorldContext"))
static void AwesomeFunction(const UObject* WorldContext)
{
    UWorld* World = GEngine->GetWorldFromContextObjectChecked(WorldContext);
    // do stuff with World
}


The context object is then set automatically, and won’t show up as an input pin.

Ah, OK my bad. Thanks for the clarification.