# Do you cache a pure "get" functions result if using it on tick/input axis?

**URL:** <https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [September 10, 2022, 10:36pm UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985 "2022-09-10T22:36:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BIGTIMEMASTER](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bigtimemaster/32/710746_2.png) [@BIGTIMEMASTER](https://forums.unrealengine.com/u/BIGTIMEMASTER)\
**Post date:** [September 10, 2022, 10:36pm UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985/1 "2022-09-10T22:36:13Z")

</div>

Example: I will deliver an input axis event to the currently controlled pawn. Is it any difference if I cached a reference to the pawn, like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/4/c/84c886f67b6fee4345178a1d4397843503cddf09.png)

compared to:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/e/7/4e70e3dcad760cd9f7b7cee70dcaf34afc77b326.png)

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [September 10, 2022, 11:50pm UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985/2 "2022-09-10T23:50:25Z")

</div>

Depending on the situation and the function called, yes it can make a difference, sometimes quite big. In general when it comes to blueprints there is always a small overhead when executing nodes, but most times it’s negligible.

On another note, pure functions get called **once for every connection of their output pins** , which is important and can make a difference in the result of your function.  
Take the following example:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/f/7/cf7ae4abcd4216b5b6ff73d49283c4425134e6b6.png)

This function will not give you the result you might expect, since the `Random Float In Range` node will get executed twice, once for the comparison and the branch, and once for the print string. And since it generates a random number every time it’s run, a different number than the one used in the comparison will be printed. So in this case you need to cache the result like so:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/3/a/83a85430556058558318da7bc7c26bf494bcad9d.png)

So if you take that into consideration, especially when it comes to some pure functions like for example:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/7/1/471679133e94e6ad6c9cd8e7e4108e95d61d1760.png)  
which could potentially be expensive or slow, then it makes sense to cache the result, if you are using it multiple times and it doesn’t change in between.

However, in cases like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/7/a/b7a7d398695634459997ae99cc194ca989be5d43.png)  
where you need to access a value that is changed and you need the latest value, then caching won’t help you.  
In this example however, it would be more efficient to do this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/6/4/26418a7e59486724018935462ffc53375d20e664.png)  
than this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/5/3/d539b2ca64d1869e88880245640cb60adfbd099c.png)  
especially because it would have to calculate a vector length twice, which uses a square root, which is a generally expensive function for computers, even though the vector doesn’t change between the `Set Speed` and `Set Is Moving` nodes.

In your specific example, the `Get Controlled Pawn` function is this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/c/2/ec230d92e49eada1c0345b33128d3320b1a6e3df.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/3/f/73fd37c435c79650bb35754bbf8873f0200df96f.png)  
so it’s basically just a getter for the internal variable “Pawn”, so the only price you’re paying is the small overhead of blueprint nodes. But still, be mindful when using pure functions like that, and of course if you are planning to reuse something it’s always better performance wise to cache it, but at the same time you shouldn’t stress too much about it unless it’s the result of an expensive call

---

<div class="post-metadata">

**Author:** ![BIGTIMEMASTER](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bigtimemaster/32/710746_2.png) [@BIGTIMEMASTER](https://forums.unrealengine.com/u/BIGTIMEMASTER)\
**Post date:** [September 11, 2022, 12:09am UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985/3 "2022-09-11T00:09:36Z")

</div>

@zeaf , just the sort of explanation I was hoping for + more. Very helpful, thanks a lot.

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [September 11, 2022, 9:09am UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985/4 "2022-09-11T09:09:54Z")

</div>

zeaf’s explanation should be in the official documentation for blueprints.

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [September 11, 2022, 5:18pm UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985/5 "2022-09-11T17:18:52Z")

</div>

This drove me crazy for a long time!

Blueprint scripting _looks_ like a dataflow, functional graph, but it’s actually a graph encoding of a procedural program. The same “pure” node going to two different execution nodes, will be evaluated twice, meaning the same “pure” reference may return different values to the different readers.

This really needs to be put front and center in the blueprint reference documentation, at least for programmers who aren’t new to programming.

(I still get confused sometimes!)

Or, to put it in terms a functional programmer might want: Each white “exec” node is a separate monadic bind operation, and “pure” only holds within each such operation. And even there, it’s not actually pure – it can get evaluated multiple times, which matters if the internal implementation actually mutates something, like incrementing a counter. So, a “pure” blueprint node is less like `pure` and more like `unsafePerformIO` 😃

---

<div class="post-metadata">

**Author:** ![BIGTIMEMASTER](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bigtimemaster/32/710746_2.png) [@BIGTIMEMASTER](https://forums.unrealengine.com/u/BIGTIMEMASTER)\
**Post date:** [September 11, 2022, 5:38pm UTC](https://forums.unrealengine.com/t/do-you-cache-a-pure-get-functions-result-if-using-it-on-tick-input-axis/645985/6 "2022-09-11T17:38:38Z")

</div>

[Unreal Directive](https://www.unrealdirective.com/)

i saw this recently, it has a nice collection of little tips. Seems like the intent is to startup a new wiki, and it looks like they have some megagrant support.

Might be worth trying to add a “blueprint pure functions, beware!” tip about this. I’m happy to transpose what I learned here but probably better if a real programmer who can explain more correctly did so.

Of course, it does belong in docs, but I dunno if that even gets more eyes than some of our community resources
