Can I create a static function in C++ which I can access from any Blueprint?

I’d like to create a function that runs a relatively complex math operation. I have the code already written in C++, but I’d like to be able to access this function from any blueprint, so that any of my actors (or even my level) can use it.

It’s in a custom MathsUtility class that I’m using to hold it as a static function, is there any way I can set this static function up to be access from any blueprint?

1 Like

Yes, same way as you do it with any other functions, UHT will detect that this is static function and will generate node that won’t require “Target” Input

UFUNCTION(BlueprintCallable, Category = "Something")
static void DoSomething(UClass* Something);

Ofcorse your class you put this in need to be UObject related and have UCLASS() in order for this to work, look on UGameplayStatics on engine source code for example

1 Like

Hm. I had tried this with BlueprintPure and BlueprintCallable, but did not see my static function in the level blueprint available for calling. I will attempt again.

i updated anwser :slight_smile:

Also if you uncheck the “Context Sensitive” box in the blueprint thing when you right click to find a function, it’ll make EVERYTHING show up, including your custom stuff. That’s how I figured out how to see my C++ functions in blueprint.

Well it works for me, you had that error when you first tried it?

Yeah I got it working. I had tried using BlueprintImplementableEvent in there and didn’t take it out so compiling was failing. It made me think it wasn’t possible.

Thanks

do you guys have a clue on how to do the same with BlueprintNativeEvent?

UFUNCTION(BlueprintNativeEvent, Category = "local")

static T getSomething();

static T getSomething_Implementation();

the above give a compilation error :confused:

If I recall correctly UE4 doesn’t support exposing template types to blueprint. There are a few exceptions for collections like TArray, but the type needs to be known or it won’t work.

T means some predefined type, not a templated. sorry if it has confused you

I tried this and Unreal Engine 4 crashed with no trace.