UE4 how to call a c++ function in Blueprint

Hi all,
Hope all of you are doing well!

I create a class in c++ from Visual Studio and i want to call a function from Blueprint!

Here is my c++ codes:

Hy header

/**
*
*/
class NAVMESH3RDPERSON_API GameLink
{
public:
GameLink();
~GameLink();
static bool GetKeyIsPressed();
static void SetKeyIsPressed(bool b);
};

My source:

#include “NavMesh3rdPerson.h”
#include “GameLink.h”

static bool keyIsPressed = false;

GameLink::GameLink()
{
}

GameLink::~GameLink()
{
}

bool GameLink::GetKeyIsPressed()
{
return keyIsPressed;
}
void GameLink::SetKeyIsPressed(bool b)
{
keyIsPressed = b;
}

===

After, from UE4, i create a blueprint and try to call one of those 3 c++ function!

Thank you for your attention.

hi,

You need to add the UFUNCTION() macro:

UFUNCTION(BlueprintCallable,Category="AnyString")
GetKeyIsPressed();

Do that before each function declaration, but construstors and destructors wont work with that.

List of parameters for UFUNCTION():

In your case “BlueprintCallable” is the right parameter already, but there are much more. You also always have to add a category. The String you put in there does not matter. Its only for sorting purposes in the BP-Editor.

I think you have to use the UCLASS() and GENERATED_UCLASS_BODY() macros as well, to make it work:

https://docs.unrealengine.com/latest/images/Programming/Development/ManagingGameCode/CppClassWizard/CodeEditing.jpg

Feel free to ask if something is not clear yet!

BTW: Marking your code as code in the asnwerhub-editor makes it much easier to read ;D

Thank you so much for your answer. My functions are not appears in Blueprint but i notice from your screen shot that you create your class as an actor. I create an empty c++ class. Now i will try it in you way. I will let you know about the result. Thanks Joe.

Hmm, I think I missunderstood you. You want to call this functions from anywhere in your project? Thats a littlebit more tricky. Or do you want to inherit from that class in blueprint? Then the solution above should work…

Sorry for the delay … i came from lunch:
“Hmm, I think I missunderstood you. You want to call this functions from anywhere in your project? …”

  • Yes that’s it. Basically i notice that the key press node (A key, B key, …, Z key, Space, etc) work only in Level bluprint: it is not working in my created blueprints. Finally i decide to create a bool variable from level blueprint but i could’t have access on it from my blueprint.
  • Finally i Google search and i saw that someone post that we can only create static variable form c++.
    • I may be wrong try it this way but. Let me know if you still missunderstand.

BTW i am currently trying the solution above and i will let you know about the result.

That key presses dont work sounds not normal… Did you try to set up the input and implement those input-events like so?:

Those should defenetly work in any Blueprint you have and you wouldnt need to touch C++ ;D

I don’t know if UE4 doesn’t like me but it is not working by fallowing your steps from the link Input in Unreal Engine | Unreal Engine 5.1 Documentation … but i will try it again at home. Maybe there is an error on the UE4 software installation or something. I will try again on a colleague’s machine.

35762-input2.jpg

Is none of your Input working? Not even Mouse input like turning characters head or something? Then you should defenetly try this:

35764-input.jpg

It is working. Thanks a lot Joe.