# ForEach Loop losing data in array

**URL:** <https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247>\
**Category:** Audio\
**Tags:** possible-bug, vr-use-focus, unreal-engine, Array, loop, Blueprint, question\
**Created:** [August 23, 2018, 12:16am UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247 "2018-08-23T00:16:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 12:16am UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/1 "2018-08-23T00:16:06Z")

</div>

I have an issue where when I try to use a for each loop, the information in the array I call the loop on, disappears. I have checked that the data is in the array. See GIF for example. (The values turn to null as soon as it’s accessed)

![251332-unrealproblem-2.gif](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/d/c/0dc1b337ce1acb9c5d882b3e7590579d5f0dd260.gif)

I have also posted a picture of how I add values to the array. It just an event that I call from another BP. ![alt text](https://d1ap1mz92jnks1.cloudfront.net/storage/temp/251331-helpfourm2.png)

Any Suggestions? I’m still kinda new to unreal, but I don’t fully know why this is happening. I had also tried using sets, and that also didn’t work.

Thank you for any and all help.

---

<div class="post-metadata">

**Author:** ![Nebula\_Games\_Inc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nebula\_Games\_Inc](https://forums.unrealengine.com/u/Nebula_Games_Inc)\
**Post date:** [August 23, 2018, 3:01am UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/2 "2018-08-23T03:01:22Z")

</div>

Does the second print string ever fire? Also what is before all of this in the flow of execution?

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [August 23, 2018, 6:56am UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/3 "2018-08-23T06:56:10Z")

</div>

Your screenshots are not informative enough. All I can see is the ForEach Loop prints your string N times depending on the array length. What happens to the array elements is unclear.  
  
Why do you think something gets lost? What happens to the array elements - maybe there are some further conditions in the execution sequence?  
  
If you could provide more detailed screenshots that would be helpful.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 2:19pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/4 "2018-08-23T14:19:54Z")

</div>

The Second string never fires, before this is just a lot of code to get an actor that is the attach parent’s parent.I will post more details.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 2:23pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/5 "2018-08-23T14:23:38Z")

</div>

See that’s just it, the array never gets printed. It only prints the first string, but not the second. I don’t understand why the references get lost, seeing I don’t clear them anywhere.

---

<div class="post-metadata">

**Author:** ![Nebula\_Games\_Inc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nebula\_Games\_Inc](https://forums.unrealengine.com/u/Nebula_Games_Inc)\
**Post date:** [August 23, 2018, 2:31pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/6 "2018-08-23T14:31:28Z")

</div>

You must be clearing the array somehow. Does the completed pin fire? Sometimes blueprint nodes are just “bad” deleting the non-working ones and replacing them sometimes fixes problems that have no other explanation. Also try printing a string for the array length just prior to the loop and see if it has anything in it.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 2:52pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/7 "2018-08-23T14:52:54Z")

</div>

Added some additional Screenshots. Warning, it is a mess.

 ![251382-unrealproblem-3.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/e/d/3ed49d7c4a5db3701a40dd950ed8ee09adbb8f15.jpeg)

 ![251383-unrealproblem-4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/2/1/22179028c3e9ffe9e2c3792cc8b137afb762209e.png)

This is a majority of the BP. But I’m just trying to figure out why the “for each” isn’t running

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [August 23, 2018, 3:00pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/8 "2018-08-23T15:00:29Z")

</div>

1st screenshot, bottom center. Shouldn’t that ADD node be hooked up to AttachToActor?

---

<div class="post-metadata">

**Author:** ![Nebula\_Games\_Inc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nebula\_Games\_Inc](https://forums.unrealengine.com/u/Nebula_Games_Inc)\
**Post date:** [August 23, 2018, 3:03pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/9 "2018-08-23T15:03:42Z")

</div>

Umm…I just took a couple minutes to try and figure out that appropriately named “mess” of blueprint you have there and I am sorry I have no clue what you are trying to do. But after looking at this, I can tell you with 100% certainty the least of your issues is why the “For Each Loop” isn’t firing. You are running a million things off tick…one of which is the “For Each Loop”, I can’t think of any situation (maybe there is 1 out there but I doubt this is it) where you would want a “For Each Loop” to run on tick, that is pretty absurd. You have kind of mitigated that effect somewhat by using a ton of “Do Once” nodes with resets all over the place that I can’t really follow.You also have a bunch of booleans regulating flow through this massive thing here but I can only see where one of those booleans is being “set” which makes it even harder to follow exactly what you are trying to accomplish. My advice, write down, step by step the “GOAL/MECHANIC” desired at the end of all of this and we will work on re-doing this entire set-up because I don’t think this is something you should event attempt to salvage in its current state.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 3:06pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/10 "2018-08-23T15:06:50Z")

</div>

So I just tested printing the string length. If you look at the Top of the 1st new screenshot. I added a print string before the branch (After posting the screenshot). And it prints the length. But if it’s after the branch, it prints 0.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 3:13pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/11 "2018-08-23T15:13:55Z")

</div>

Solved…  
In the setting of the array, I needed to set “Instance Editable” to be true.  
Sigh…  
Thank you all for your help.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 3:16pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/12 "2018-08-23T15:16:46Z")

</div>

Thank you, I’m sorry that you had to try to read it. I’m trying to emulate a motor, and this is just a wire end. I’m still super new to unreal programming, but thank you for helping me.  
I did just solve it, it was a simple checkbox in the setting of the variable.

---

<div class="post-metadata">

**Author:** ![Nebula\_Games\_Inc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nebula\_Games\_Inc](https://forums.unrealengine.com/u/Nebula_Games_Inc)\
**Post date:** [August 23, 2018, 3:20pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/13 "2018-08-23T15:20:23Z")

</div>

I mean I am glad you fixed it, BUT I caution you, that set-up is bound to give you problems down the line. In general “tick + loop” is never a good idea. I would switch this over to “events, timers, functions etc”. Tick is a heavy node to use for something like this and probably only needs to be used for smooth motion where you really do need to update the location of an object every frame.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ff40c3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ff40c3e](https://forums.unrealengine.com/u/anonymous_user_5ff40c3e)\
**Post date:** [August 23, 2018, 3:35pm UTC](https://forums.unrealengine.com/t/foreach-loop-losing-data-in-array/432247/14 "2018-08-23T15:35:34Z")

</div>

Thank you, Events are a place that I struggle with, so I will take your advice on my next project. I would try to change things if I didn’t have a week left on this 3-month project. Thank you again!
