C++ function won't get called from BP

Hello,

I have a function in my C++ GameMode for the map, which I’ve given the UFUNCTION(BlueprintCallable) macro. I call the function from my level blueprint using a Begin Play event.
The function doesn’t respond at all even though I can see the pins firing in the BP at run-time.

Any ideas?

Thank you.

SOLUTION: Originally in my target pin I promoted it to variable since I didn’t know how I was supposed to get the reference to my C++ game mode. This time I used the Get Game Mode node, casted it to my implementation, and used its output pin as the reference. Now my function works. I really don’t get why I had to do it in such a roundabout way and thought that promoting the target variable would’ve worked. Anyone care to explain?

Hey there (:

it would be easier to solve your problem if you could provide us with some code of the Header and CPP file, regarding your broken function.

Also pictures of your blueprint setup would help if it is complicated. (:

The code side: http://pastebin.com/TYQ5aSUs
(wrote it from memory, but should be accurate).

As for the level blueprint I used the ‘Begin Play’ event to call this function.
I passed a reference to the C++ game mode by promoting the target parameter to a variable.


(.cpp)
...
public:
        UFUNCTION(BlueprintCallable, Category = "Custom")
        void TestFunc();
...
(.h)
...
void ProjectGameMode::TestFunc()
{
        GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::RED, "Test called.");
}

Are you sure that you put it this way? Because if yes, you put the Header Code into the C++ and other way round.

Also Try this for the AddOnScreen Message:




GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::RED, TEXT("Test called."));


Whoops that’s embarrassing, but yes I definitely have it the right way round in code. I put breakpoints on the function itself and they’re not triggering so I’m not sure what the problem is.
Is it because I’m trying to access the gamemode through the level blueprint? Is there some special rule against this?

Did you set the game mode in your level? I often forget that one…
Can you reach the TestFunc node in your blueprint?
Are you doing multiplayer and accessing the game mode from the client?

Yep the gamemode is definitely set and the function shows in my list as long as I disable context sensitive. And no multiplayer.

Would you mind sharing your project here or per PM?

I recreated the issue in a new project. The file size is almost a gigabyte even though there’s no starter content. How do I reduce this and upload it for you?

Edit: I created a C++ class derived from the default APawn and added my custom function. In the editor I created a BP derived from my custom APawn and called the function.
This worked, but in my original case calling a function that’s not directly defined by the class of the BP doesn’t seem to work. Why is this?

Edit: Thanks for your help, found a solution, but I don’t understand it. Any chance you could explain?

Fun fact: “Header Code” is C++, what include does is paste the code from file to point there include is, it does not need to be header file but practically anything. If you would place code in header file in .cpp file instead of using include the result would be the same… in normal C++, since UHT needs headers to be in header file. Header files exist because otherwise you would need to repeat and repeat headers in each cpp file, include makes things cleaner.

For the future: You only need to send us the Source, Content, Plugins and Config folders and the uproject file. If that is still too big remove all meshes and textures etc.

You created a variable but did not initialize it, so it’s value was a nullptr. If you promote an input to a variable the blueprint system can’t know it’s value, so it is initialized with a default value - in this case the nullptr.
Do you know that you can debug blueprints? You can set a breakpoint in your custom node and then inspect the value of the variables. That way you could have seen that the variable holds a nullptr.

Note: A screenshot of the call in the blueprint would have been a great help for us helping you. Every bit of information you can provide when asking questions is useful.

Yes, but if he puts the .cpp code above the .h code (which will happen if he switches them like he did above and still use include header.h in the cpp file), wouldn’t this cause errors, because the compiler finds a definition before it findes the actual class and function declaration?

As for BP debugging I was only aware of the ability to follow dots through the code to see if it’s activating. I should have experimented with the BP breakpoints, you are right.

All very good advice that I will take forward, thanks.

In this case I was able to use Get Game Mode to get a reference to my custom Game Mode and cast the result. How would I for instance get other classes that I’ve created?