For each loops run infinite, cant understand why

Hello,

I am doing a game where in our world, when the main character overlaps on a tile or platform, the flipbook changes and the tile/platform is ‘transformed’

I am trying to make a completion bar for the world, so the player can try and track how much of the world they have transformed. In my HUD, I am trying to do the function that would update the percent bar.

Basically the problem is in the for each loop. I just want to see if the variable of the platform for transformed = true, and if so, increment my NumberTransformed counter. For some reason, it just increments it infinitely forever. I’m not sure if I have to incrememt the array index, or if ue4 does it automatically, that’s why on the bottom part of the blueprint (it looks like the same class as the other one but its actually another one, the names look the same) I have it increment the index. It didn’t help solve my problem.

Can anyone offer any guidance?

Don’t know for sure if this is the thing that you are doing wrong, but in your second foreach loop you add to the index, you don’t need to that since it is a foreach loop

ahh, ok thats what i thought initially but it wanst sure so I added it. I’ll take it out

what it looks like now

It’s hard for me to see the nodes in the PNG snapshot because the resolution is low. But as you are using a “For Each Loop”, altering the “index” the way you have it, won’t make a difference.

The For Each loop is going to loop once for each element in the array, if you are altering the number of elements in the array in the code after the for each loop (i.e. adding to the end of the array), that could cause some headaches but you are not doing that. Because the array coming into the loop is coming out of the GetActors node.

.

Sorry about the quality. Is this better?

I understand about the index, that I don’t need to increment it myself. As far as my loop body though, do you see anything in it that would make NumberTransformed increment infinitetly?

chuckles, if you could double the size one more time, would be good! My eyes are old, sorry about that, I just can’t see what classes you are getting the actors for.

But I would put in print string, after each GetActor node, to dump out the length of each array being returned, There is actually nothing in the flow of execution to cause a infinite loop. As the code for the top loop is essentially identical to the code for the bottom loop, and you have the return node in order to return to the caller.

You may wish to look at the calling function, to see, if it is calling more this inner routine more than you expect.

.

I put in a print string to see what NumberTransformed is, and I just realized my problem. If i transform 1 platform, it just keeps incrementing it by 1, because it just recounts the same platform as being transformed. I need a way to make it take all of the transformed platforms out of the array, so it only considers platforms that are not transformed and have not been counted for…

Depending on which array you are referring too, you have two GetActor nodes. You really don’t have a way to take an element out of an array that is meaningful.

The top loop gets it’s own array, then so does the bottom loop. Hence you have what is called “information loss”. In that the bottom loop cannot know what the top loop is doing, all the bottom loop knows is that that that the top loop finished executing (because of the completed pin), plus the “total transformable” and the “number transformed”, but there is no information conveyed as to what is transformed.

As a hack, you might consider, making a new local array, that holds all the elements that were transformed. Then using it to filter out, on the lower loop, if they are getting the same set of Actors, or it they are connected in some fashion.

.