I’m wading into the unreal reflection system - trying to call a UFunction on an Actor. Is there a quick sample anyone has handy you could post that shows how to call a UFunction correctly? My attempts so far have crashed and burned.
In what context? (gameplay code, your own editor module, etc.)
There should be plenty of examples in the ExampleGame code, but I don’t think there is anything special about it in general. You just call the function like you would any other as far as I know. Can you post an example of what you are trying to do?
Hey Jeff!
I’m doing this in my editor module. I’m using the TFieldIterator to iterate over the UFuntions in a class and I’m currently trying to call one of them with CallFuntion. All of the calls in ExampleGame are compiled function calls, I’m trying to determine at runtime everything nessesary to call these functions.
Away from PC or I’d post a code snippit of where I’m at.
Ah, that makes sense. Probably need Mike, Zane, or Mr. Noland to answer that one, but I am sure a code snippet of where you are at would still be helpful.
Alright, I’ve made some progress. CallFunction seems like the wrong way to go about it (at least for the simple case), ProcessEvent appears to do the trick. Not sure what I need to provide the parameters param if I actually wanted to provide a set of parameters. Would love for Mike or Zane to clear that up.
The below code is just a reference, (I’m not actually looping like this and blindly calling all functions).
TFieldIterator funcIt(actor->GetClass(), false);
while(funcIt)
{
UFunction* function = *funcIt;
actor->ProcessEvent(function, NULL);
++funcIt;
}
Hmm and I need to figure out how to determine what parameters a UFunction takes in the signature.
ProcessEvent is the correct function to call. Of course you would never usually iterate over an actor and call all of its functions though. So assuming you know something about the function that you want to call, there might be a better way to go about it than by manually packing up the arguments to marshal.
A good example might be a class that allows you to specify a UFunction (by name) to be called when a value changes. You would find the function and validate that its signature matches what you expect, and then marshal the arguments and pass them along to ProcessEvent(). But in that case there is probably a better way – instead, you could expose a dynamic delegate that allows the user to bind (in a type safe way) to a method on their class, and you can invoke it in code using normal delegate “broadcast” syntax.
Obviously I am holding back details here to keep my response brief, but I can elaborate more if needed. It would help if you gave an example of what you’re trying to accomplish. 99% of the time, programmers using UE4 will never need to call ProcessEvent().
Hope that helps!
–Mike
Yup, is the only way to determine what the parameters are in a UFunction to compare it with a known signature with IsSignatureCompatibleWith?
I figured out I can pass in parameters by putting them into a struct and passing that in.
Thanks Mike!
You can actually enumerate all of the parameters by using a TFieldIterator and checking for fields with the CPF_Parm PropertyFlags set. They will enumerate in the order of the arguments on the function. This is a pretty advanced feature though. I can give you more detail if needed.
Geez, is there anything the TFieldIterator can’t do? Thanks Mike!
Here is an example from the engine for how to call a function with arbitrary argument parsing.
Note: This already exists, so if your parameters are already in text to begin with, then you might be better off just calling it directly, instead of duplicating it / writing similar code.
Cheers,
Michael Noland
Awesome, thanks Mike. +1
I may end up using the function in the end to do the actual call. For my simulated events plugin I wanted to be able to offer the capability to call void events as well as events that take a single primitive value, so I needed to be able to filter it to that set.
I can provide some menu selections that make ints, floats and bools, quick selections and hope that covers 95% of use cases, but we’ll see. Gota prototype it and see what sticks.
Thankfully I was able to find this post in the wayback machine which includes a link to this original Linked Text which didn’t get migrated.
Another useful link on this topic is here:
I just wanted to add for ultimate posterity’s sake: you can find the “linked text” example in Engine/Source/Runtime/CoreUObject/Private/UObject/ScriptCore.cpp
under the function UObject::CallFunctionByNameWithArguments
I will also try these links.
I made one of these things recently and with editor support, check the code bellow for more information:
RemReflectedFunctionCallData
which support calling a static or memberUFUNCTION
, with almost any parameters
(except parameter of delegate type (or more) for now because of limitation of FInstancedPropertyBag
), and has return value
.
I’m searching for populating default value
for these parameters ATM.