Using "BlueprintCallable" in C++ for Unreal Engine 5
- Christian Carter
- Feb 2, 2024
- 3 min read
Updated: Feb 4, 2024
Unreal Engine is a powerful game engine that offers a pretty seamless combination of visual scripting and low-level programming with C++. Unreal's Blueprints is an amazing visual scripting system that offer an approachable way to make your game using ZERO C++ code. There is often debate on whether Blueprints, or C++ are the "better" way to approach game development, when in reality, they were meant to be used together. A crucial component to this bridge between the two, is the use of the 'UFUNCTION(BlueprintCallable)' macro that will take your blueprints to the next level. Here are a few reasons why (and a little 'How-To') you should start using this in your workflow today:
1. Direct Blueprint Access for C++ Functions
Using the declaration of UPROPERTY(BlueprintCallable) is very straight forward. Simply declare this macro preceding your declaration of a function and voila! This will allow you to write whatever complex, mind-bending, world-altering function you want in C++, and have it directly accessible in your blueprint with ZERO MESS. Pretty dang powerful stuff!
Here is an example of how these are declared in the header file for our upcoming project "Eggsodus"
Note, these functions still have a body in the .cpp as usual:
As you can probably tell, the actual FUNCTION isn't really different at all in syntax or anything. But using these can yeild powerful results as you can them directly call these by right clicking in a blueprint of the same class, typing in the name of the function, and using pins for inputs and outputs in the blueprint that will be piped directly to your C++ function.
In the example above, I am calling the C++ function "ReturnChickens" that is then returning an array of the actors that have spawned in this blueprint. I can then use that output however I would like.
2. Unreal Engine Performance and Optimization
Though you could build an entire game using blueprints alone, C++ still gets the crown when it comes to speed and optimization. Using this hybrid approach of relying on the power and speed of c++, with the accessibility of blueprints will help to improve your workflow to achieve the best of both worlds. Keeping your critical and computationally exhausting code within C++ will give you a major performance boost when compared to running strictly on blueprints.
3. RE-USE-ABILITY. It's What's for Breakfast
Using these macros to bridge the gap between C++ and Blueprints force you to think about the way you write your code. A major upside to thinking in the mindset of using these functions, is writing to make these funcitons accessible EVERYWHERE in your game. Once you can fine-tune the C++ function, making this publicly available everywhere in your game will not only streamline your development, but ensure consistentcy throughout your project that will cut down on bugs that no one wants to fix. One example that we use often is managing menus and UI throughout the whole game with one GUI Manager that is chuck-full of blueprint accessible functions. From anything to moving menus, updating HUD, changing player settings, etc., it can all be done in one place, and accessible anywhere throughout the game.
A Few Extra Treats for your Knowledge Bucket...
Here are a few important tips and tricks about using these functions:
UFUNCTION(BlueprintCallable) functions have to be made public. If they are not public, they will not be accessible in your blueprint, then you are probably cry, everyone will laugh and then you are going to be all embarrassed. And that just wouldn't be worth it now would it?
You can still debug your code while using these functions! Breakpoints work just the same in both blueprint and c++, so don't worry about missing out on debugging functionality.
Last but not least... Remember, you are losing out on a MEGA-POWERFUL tool if you aren't taking advantage of these functions!
For a more in-depth tutorial on how to use these functions, check out this tutorial here on our YouTube Channel!