# UE4 how to call a c++ function in Blueprint

**URL:** <https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395>\
**Category:** Blueprint\
**Tags:** unreal-engine, UE4, CPP, Blueprint, question\
**Created:** [March 24, 2015, 4:07pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395 "2015-03-24T16:07:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![philUnreal\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philUnreal\_1](https://forums.unrealengine.com/u/philUnreal_1)\
**Post date:** [March 24, 2015, 4:07pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/1 "2015-03-24T16:07:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d575feb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d575feb6](https://forums.unrealengine.com/u/anonymous_user_d575feb6)\
**Post date:** [March 24, 2015, 4:22pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/2 "2015-03-24T16:22:22Z")

</div>

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():

> **[Function Specifiers](https://docs.unrealengine.com/5.1/en-US/function-specifiers/)**
>
> Keywords used when declaring UFunctions to specify how the function behaves with various aspects of the Engine and Editor.

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](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

---

<div class="post-metadata">

**Author:** ![philUnreal\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philUnreal\_1](https://forums.unrealengine.com/u/philUnreal_1)\
**Post date:** [March 24, 2015, 4:36pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/3 "2015-03-24T16:36:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d575feb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d575feb6](https://forums.unrealengine.com/u/anonymous_user_d575feb6)\
**Post date:** [March 24, 2015, 4:42pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/4 "2015-03-24T16:42:36Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![philUnreal\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philUnreal\_1](https://forums.unrealengine.com/u/philUnreal_1)\
**Post date:** [March 24, 2015, 5:42pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/5 "2015-03-24T17:42:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![philUnreal\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philUnreal\_1](https://forums.unrealengine.com/u/philUnreal_1)\
**Post date:** [March 24, 2015, 5:48pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/6 "2015-03-24T17:48:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d575feb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d575feb6](https://forums.unrealengine.com/u/anonymous_user_d575feb6)\
**Post date:** [March 24, 2015, 6:29pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/7 "2015-03-24T18:29:09Z")

</div>

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

> **[Input](https://docs.unrealengine.com/5.1/en-US/input-in-unreal-engine/)**
>
> Different methods to create and setup input in Unreal Engine

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

---

<div class="post-metadata">

**Author:** ![philUnreal\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philUnreal\_1](https://forums.unrealengine.com/u/philUnreal_1)\
**Post date:** [March 24, 2015, 7:52pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/8 "2015-03-24T19:52:54Z")

</div>

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](https://docs.unrealengine.com/latest/INT/Gameplay/Input/index.html) … 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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d575feb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d575feb6](https://forums.unrealengine.com/u/anonymous_user_d575feb6)\
**Post date:** [March 25, 2015, 8:42am UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/9 "2015-03-25T08:42:13Z")

</div>

![35761-input1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/3/e/13ec94c914e953c95b16a52d539518383e28f5d4.jpeg)

![35762-input2.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/3/3/e332487d4f37d57eba8235bcb530f7cc953c0272.jpeg)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d575feb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d575feb6](https://forums.unrealengine.com/u/anonymous_user_d575feb6)\
**Post date:** [March 25, 2015, 8:57am UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/10 "2015-03-25T08:57:27Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/d/e/3de4afe7b5fb95e8fdf64bc81e75bcfc7c4c4c6b.jpeg)

---

<div class="post-metadata">

**Author:** ![philUnreal\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@philUnreal\_1](https://forums.unrealengine.com/u/philUnreal_1)\
**Post date:** [March 25, 2015, 2:32pm UTC](https://forums.unrealengine.com/t/ue4-how-to-call-a-c-function-in-blueprint/312395/11 "2015-03-25T14:32:14Z")

</div>

It is working. Thanks a lot Joe.
