# A sample for calling a UFunction with reflection?

**URL:** <https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416>\
**Category:** Feedback & Requests\
**Tags:** unreal-engine, documentation, UE4, question\
**Created:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416 "2014-03-11T03:50:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/1 "2014-03-11T03:50:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/2 "2014-03-11T03:50:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/3 "2014-03-11T03:50:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/4 "2014-03-11T03:50:19Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/5 "2014-03-11T03:50:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/6 "2014-03-11T03:50:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/7 "2014-03-11T03:50:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/8 "2014-03-11T03:50:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/9 "2014-03-11T03:50:22Z")

</div>

Geez, is there anything the TFieldIterator can’t do? Thanks Mike!

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/10 "2014-03-11T03:50:22Z")

</div>

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.

link text

Cheers,  
Michael Noland

---

<div class="post-metadata">

**Author:** ![ue4-archive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ue4-archive](https://forums.unrealengine.com/u/ue4-archive)\
**Post date:** [March 11, 2014, 3:50am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/11 "2014-03-11T03:50:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BenVlodgi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benvlodgi/32/1194567_2.png) [@BenVlodgi](https://forums.unrealengine.com/u/BenVlodgi)\
**Post date:** [May 7, 2022, 5:56pm UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/12 "2022-05-07T17:56:28Z")

</div>

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.

- [Original Post](https://web.archive.org/web/20141213141825/https://answers.unrealengine.com/questions/7732/a-sample-for-calling-a-ufunction-with-reflection.html)
- [Linked Text](https://web.archive.org/web/20140828135232/https://answers.unrealengine.com/storage/attachments/904-callfunctionbynamewitharguments.txt)

Another useful link on this topic is here:

- [UE4 Reflection Overview](https://ikrima.dev/ue4guide/engine-programming/uobject-reflection/uobject-reflection/)

---

<div class="post-metadata">

**Author:** ![MikeSeese](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mikeseese/32/1022829_2.png) [@MikeSeese](https://forums.unrealengine.com/u/MikeSeese)\
**Post date:** [June 14, 2022, 11:47pm UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/13 "2022-06-14T23:47:29Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![ezuvella](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ezuvella](https://forums.unrealengine.com/u/ezuvella)\
**Post date:** [June 17, 2022, 4:30pm UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/14 "2022-06-17T16:30:46Z")

</div>

I will also try these links.

---

<div class="post-metadata">

**Author:** ![RemRemRemRe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/remremremre/32/974428_2.png) [@RemRemRemRe](https://forums.unrealengine.com/u/RemRemRemRe)\
**Post date:** [December 8, 2024, 6:56am UTC](https://forums.unrealengine.com/t/a-sample-for-calling-a-ufunction-with-reflection/276416/15 "2024-12-08T06:56:51Z")

</div>

I made one of these things recently and with editor support, check the code bellow for more information:

> <https://github.com/RemRemRemRe/RemEditorUtilities/blob/main/Source/RemCommonEditor/Private/Details/RemReflectedFunctionCallDataDetails.cpp#L86>

> <https://github.com/RemRemRemRe/RemCommon/blob/main/Source/RemCommon/Private/Struct/RemReflectedFunctionCallData.cpp#L38>

`RemReflectedFunctionCallData` which support calling a static or member`UFUNCTION`, 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.
