Calling a function from c++ class in blueprints

I am having what I would consider a strange issue involving calling a C++ defined function in my blueprint.
The C++ looks like this:
in MyClass.h
UFUNCTION(BlueprintCallable) void MyFunction();
in MyClass.cpp
void MyClass::MyFunction() { GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("This should print to the screen when called, right?")); }
The blueprint isn’t giving me any compile errors, it even will execute a blueprint print string tied to the same event as the function when I call it through the blueprint, but for some reason the c++ message, and any other code in the function called will never execute. What could be wrong here?


This is probably something simple (or completely screwed, we will see).

First of all the UCLASS where is this UFUNCTION must be Blueprintable, so make sure it is.

Also, like in C++, the function must be run by an instance of this class, so you must create/spawn it somehow. Or you can make MyFunction static.

Can you paste a screenshot of a blueprint where you call it? How does it look like?

Don’t you need to add a “Category” for Blueprintable, BlueprintCallable functions? :X


Something of note, when I hover over the MyClass getter, it says “LeadPetal Reference Current value = none” I have it declared as a variable in the blueprint. Do I have to call a constructor somewhere like in normal code?

You don’t need Blueprintable to make funtion BlueprintCallable. Bluprintable is a specifier that allows you to blueprint class infirent to this C++ class, its not required to call funtions in to it, at most you will need BlueprintType specifier so your class can be helt in blueprint varable. For example AWorldSettings inherits NotBlueprintable specifier from AInfo (as all classes based of AActor inhirents blueprintable), but it does not stop it to have it blueprint exposed content

Categories are optional parameters… I think, they are great for keeping things organized in blueprints. Fear not, my function currently has one.

Oh… I was using it all time wrong. Thanks for correction.

You need to create instance of your class by spawning it before hand and set it to that varable. Or you can make your funtion static, then you dont need instance of class, but then funtion behaves like it does not belong to class and you need to access instance of a class in C++ if you want to do something inside that class

Like eXi wrote it below: I’m not sure if this is mandatory but it is convenient when classes and functions are in the same Category.

Also, do you create/spawn MyClass instance somewhere? It is possible that the MyClass node is referencing to nothing? If it is UObject you might want to look at this:

If this is Actor you need to spawn it somewhere.

I remember an error telling “…needs to have a category set”. when i didn’t use one. Maybe it was a UPROPERTY where i forgot it, but i mean this happens with UFUNCTION exposed to blueprints too.

You would be exactly right, It was a late night last night.

That means? Have you solved it? (:

Not exactly, I believe I have whittled down the problem to being a lack of a spawned class. My inability to spawn a class that the blueprint will reference is holding me back from finding if it in fact is the solution.

I am going to go ahead and declare this question answered. It wasn’t really a properly worded question in the first place I feel, but as programming questions aren’t often the clearest questions with the clearest answers for someone like me it is fair to say all of you who participated in the comments ultimately contributed an answer that was sufficient.

Basically, any function declared in a class, if not static, needs to have that class instantiated and accessible inside the blueprint referencing it’s functions. I am still not entirely clear on how to do this, It would be ideal to be able to call a class constructor inside a blueprint as a engine default behavior, but the solution for me simply required a few static functions and the day was saved.

Thank you to all who contributed. You all make me smile.

Ok, so i will mark your question as resolved with this answer as the right one.

You can still reopen this question if you still need help. Spawning a class to use its function isn’t that hard, but “my inability to spawn a class that the blueprint will reference” sounds more like you just don’t know how to do this. :smiley:

So if you need help with the Blueprint thingy, just ask (:

Will do, There are many things I don’t know how to do, but since I haven’t given an honest amount of research and effort toward that end yet, I don’t feel it is a fair question to ask. (I have seen several similar questions that likely have sufficient answers on the answerhub anyway.) Thanks again.