Download

Trying to add integer values to array runs infinitely, can anyone help?

First of all I’m very new to Unreal and programming/scripting in general, so please excuse my ugly node structures.

What I’m trying to do, is set up a tile that is 9x9 in size, and for any given tile, get a list of 8 tiles around it (up, down, left, right, then each diagonal direction)
The tiles are set up in an array of 81, so that tile on the right side of any tile would be i+9, tile above would be i+1, so on and so forth.

So here’s a function I have set up to retrieve indices of 8 tiles around a given tile: [IMG2=JSON]{“data-align”:“none”,“data-size”:“full”,“src”:“https://forums.unrealengine.com/core/image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==”}[/IMG2]

[ATTACH=JSON]{“data-align”:“left”,“data-size”:“full”,“data-tempid”:“temp_184793_1582876975847_617”,“title”:“20200228_165338.png”}[/ATTACH]

To add some more information, this is how this function is being used:

Now, for some reason this “Search Nearby Indices” function runs infinitely, not stopping after adding 8 items as intended. Instead, it keeps adding the same 8 items to the list over and over in an infinite loop–can someone help me find out what the problem is? Thank you!

For some reason, I’m unable to see the image of the function that retrieves the indices. Is it the same for you?

I can see the images I posted, but maybe I did something wrong? I’m attaching the images with the reply to see if that works!

here’s how the function is used; just trying to get Print String to work for now, which still freezes the unreal editor :frowning:

Alright, I can see them now. The “Search Nearby Indices” connected to your for loop is a pure function. And while they allow you to not have execution pins, they come with the disadvantage that everytime you need its output, the function is called again. So in this case, what I’m guessing is happening is that for each iteration of the For Each Loop, the function is called again thus making your array larger and larger, and thus the loop essentially becomes infinite causing the editor to freeze.

You can solve this by not making it a pure function. Just untick the “Pure” attribute on the function details panel and it should work fine.

Now the function will only be called once before entering the For Each Loop and the same output array will be used for each iteration of the loop.

You saved so much of my time!! I never would have guessed this myself if I had not asked. Thank you so much! Works perfectly now.
I just thought pure and impure functions are like getters and setters but I guess there is a bigger difference than just that. Thank you again!

Happy to help! :slight_smile: Pure functions are useful when you have a single output parameter and don’t intend to use it more than once (exception being if you store it as a local variable inside a function).

If you have multiple output parameters that are all going to be used, or have a single output that will be used repeatedly, then the function will be called every single time you use any of the outputs. In such cases, it might be better to use impure functions since you can use the output parameters multiple times but the function will be executed only once. If you’re interested in learning more about them, this is a good read: Pure & impure functions. The concept of pure and impure nodes… | by Morva Kristóf | Advanced Blueprint scripting in Unreal Engine | Medium

Awesome, will check it out. Thanks again :slight_smile: