# Setting a ForEachLoop to return a certain value if only one variable out of an array is true

**URL:** <https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213>\
**Category:** Blueprint\
**Tags:** foreachloop, unreal-engine, UE4, Blueprint, question\
**Created:** [December 28, 2018, 7:17am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213 "2018-12-28T07:17:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noodle\_Games](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noodle\_Games](https://forums.unrealengine.com/u/Noodle_Games)\
**Post date:** [December 28, 2018, 7:17am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213/1 "2018-12-28T07:17:58Z")

</div>

This one is a real brainbuster for me, but I need to figure out a way to determine if just one of the desired variable from the GetAllActorsOfClass node returns true, then set the boolean value for the entire loop to be true. Is there some magic node to do this BS? Thanks.

 ![263485-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/c/8/dc8a5ea147306c95d6b7654c29a7ba39d4ebbbf4.png)

---

<div class="post-metadata">

**Author:** ![GarnerP57](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GarnerP57](https://forums.unrealengine.com/u/GarnerP57)\
**Post date:** [December 28, 2018, 7:51am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213/2 "2018-12-28T07:51:18Z")

</div>

Make it into a function and have the function return true Boolean if it is true and return false on Completed otherwise.

Note that using GetAllActorsOfClass on Event Tick in a Blueprint is probably one of the worst things you can do to slow down the performance. Whatever it is you are checking for most likely don’t need to happen on tick even if you think it does and if the results don’t change very often you should store them in a variable instead of getting them all the time.

---

<div class="post-metadata">

**Author:** ![Evigmae](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/evigmae/32/1187349_2.png) [@Evigmae](https://forums.unrealengine.com/u/Evigmae)\
**Post date:** [December 28, 2018, 7:55am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213/3 "2018-12-28T07:55:28Z")

</div>

You would do it like this:

 ![263470-0.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/5/5/65563c00b353175e87fc2d9eea82b1ac0e4b9e4a.png)

The ForEachLoopWithBreak lets you stop the loop on its tracks.

So if any returns true, the loops stops with the bool set as **true**. otherwise the loop will run until the end with the bool set as **false**.

In any case, once the loop stops, you’ll have the return bool to branch off from.

---

<div class="post-metadata">

**Author:** ![Evigmae](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/evigmae/32/1187349_2.png) [@Evigmae](https://forums.unrealengine.com/u/Evigmae)\
**Post date:** [December 28, 2018, 7:58am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213/4 "2018-12-28T07:58:30Z")

</div>

I like your method!

 ![263491-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/b/6/8b6c6ac700bc3200a1de0c8cbfffa78c5e9f7c1d.png)

it’s very elegant.

---

<div class="post-metadata">

**Author:** ![Noodle\_Games](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noodle\_Games](https://forums.unrealengine.com/u/Noodle_Games)\
**Post date:** [December 28, 2018, 8:02am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213/5 "2018-12-28T08:02:04Z")

</div>

Yeah I see where you’re going with that. I’m using getallactors on event tick but only every 5 seconds, so it doesn’t have a huge performance hit.

---

<div class="post-metadata">

**Author:** ![Noodle\_Games](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noodle\_Games](https://forums.unrealengine.com/u/Noodle_Games)\
**Post date:** [December 28, 2018, 8:06am UTC](https://forums.unrealengine.com/t/setting-a-foreachloop-to-return-a-certain-value-if-only-one-variable-out-of-an-array-is-true/440213/6 "2018-12-28T08:06:20Z")

</div>

That worked like a hot-■■■■ thank you so much! I woulda never gotten that on my own.
