I’d wager that the reason he’s jumping between tiles and not in order is because “Get All Actors of Class” doesn’t necessarily get them in the order you want. I had a hunch this would be an issue at the beginning, but didn’t want to overload you. You’ll need to sort these tiles, but there is no one-size-fits-all approach to it, unfortunately. It would be easiest to pass the array into a C++ blueprint function library and sort based on a predicate of the tile’s name, but I assume that C++ isn’t your forte.
Let’s assume that you want tiles to be sorted left-to-right as they are on-screen. Assuming your camera is facing down the X axis, Y-left should be a lower number than Y-right.
When you get the tiles, put them into a temporary array (as a local variable in a function). Do a foreach on the array and get the Y coordinate of each, placing it into a local float array.
After the previous foreach is completed, use the while loop. The while loop will run a block of code for as long as the condition plugged into it is true. While the local float array’s length is greater than 0 (meaning it still has entries), Get the minimum number of the float array and put it into another sorted float array, then remove the entry from the previous float array. This will continue until the original float array’s entries are totally removed, placed into the sorted float array in order.
After that’s completed, we’ll run another while loop, this time checking that the length of the sorted float array is greater than 0. Inside each while loop body, we’ll run another foreach loop on the array of tiles, but this time we want to use “foreach loop with break.” This will allow us to break out of the foreach loop when we find what we’re looking for so we’re not wasting time iterating over the rest of the entries in the array.
This foreach loop will get the Y coordinate of the tile and check if it is nearly equal to the first entry of the sorted float array (since they are in order). If it is nearly equal, we know that this tile is the first of the tiles going from left to right. We can then place this tile into a permanent array of tiles for his level and they’ll be sorted based on their Y positions going from left to right. After placing each tile in the array, we remove the first entry of the sorted float array. We remove the first because we want to go through them in order. then we break the current foreach loop.
Nearly equal is used instead of equal because floats are imprecise. They can be slightly different between ticks just because they’re meant to be approximations of numbers, not exact numbers. Changes are usually minute, within a trillionth of deviation, but it can still be different and therefore, not equal.
The while loop will then evaluate the length of the sorted array again and if necessary repeat the process until the sorted float array is empty. In the end you should have your tile array sorted by Y-coordinate from least to greatest, and if your scene is set up to corroborate, from left to right.
Unreal really needs some array sorting nodes. I’ll add them to my blueprint function library and submit a pull request so they get added to the engine.