Store a reference to a BP event in C++ and call it
I would like advice from programmers on UE4 about this question: from a custom blueprint node that expands in a UK2Node_CallFunction, how can I send to a C++ function the reference of an event created in blueprints? Then, with that reference, what is the good way to fire it when necessary, from anywhere?
If you have the same answers for BP Functions instead of BP Events, I'm also interested.
Leads I'm on:
Thank you in advance, I really need your help there...
I managed to do it this way:
1 - My node : It's a "pure" node that I managed not to get purged during the first phases of blueprint compilation - somehow - and which leads to an exec wire. The idea : it's creating an UObject that can be stored in a variable of the blueprint, and then some functions can be called on it later. One of the function must call the nodes that are connected to the "pure" node by the exec wire, like an event would do. Somehow, it's like a "stored event", but it also stores a lot of other things and requires some inputs, which an event couldn't do.
2 - Expansion : At BP-compilation time, the node create an intermediary Custom Event with a custom name so that's it is unique. This event only exists during compilation and is later forgotten, when all blueprint-script is translated to executable code by kismet compiler.
The node also replaces itself with a call to a function in a blueprint function library I made. One of the arguments of this function is a FName : the name of the event that was just created.
3 - The function : Later on, when that function is executed by the object owning the node, a simple custom delegate I registered is bound to the event with its FName and with the reference to the calling object. At last, when another function is called, that same delegate is used to fire the event in the source object, now that it is bound.
The important thing to consider is that it's not possible to bind the delegate at compile time, or do anything that would refer to the newly created event, because it's not compiled nor registered yet. I had to store its name somewhere and wait for the end of the compilation to finally bind it. I also did the same with newly created properties in the source object, for the same project with veeeryyy specific needs ^^
I'm wondering if you're looking at this the wrong way.
If you want to have a BP event callable from C++, you would just create a function in C++ and mark it BlueprintImplementableEvent. Which means it doesn't have to have an implementation on C++ as it will be implemented in Blueprints.
You could add BlueprintCallable in there and call it from Blueprints as well.
Which would let you create an Event in blueprints callable from C++.
it could have any parameters you like too.
Calling blueprint functions from C++ doesn't make much sense though. that's not how you should try to use those. You often wanna create C++ functions and call them from Blueprints though.
BlueprintImplementableEvent is meant to allow designers to add some front-end stuff when C++ needs it. C++ is great with hardcore logic, Bp is great with dynamic asset references (eg, effects, sounds, materials, etc)
answered Dec 06 '18 at 03:14 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here