Sorting an array by distance from player?

Hey all.

I’m attempting to sort an array of HISMs by distance to the player and then use the newly sorted array to stream in levels.

That way when levels are procedurally streamed in. It would stream the closest ones first.

Something about this seems to be inherently flawed as I merely pulled the code from this thread.

I modified it slightly to use get instance transform instead but now everything returns false every single time no matter what, causing everything to spawn at world 0,0,0.

If anyone has some insight on how to do this in a better way I’d love to hear it. Thank you.

Can you use this and modify it?:

1 Like

Hey, I appreciate you taking the time to throw that together for me.

So I modified it for my uses and it doesn’t seem to work for the same reason the last one doesn’t. I’m sure your function does what it’s supposed to but it’s a problem on my end.

I spawn a large grid of “level stream markers” at begin play. The player has a large collision sphere around himself. When the sphere overlaps a marker, spawn a level. When it leaves overlap, despawn.

The markers are HISMs to cutdown on performance. Due to this they have their own instances and they’re a bit wonky to work with sometimes.

The problem I am having is when the trace overlaps a marker I add it to an arra- but! To get it’s transform I also have to save the int of the hit result(So I can keep track of what instance is hit).

So the problem with these is that when I rearrange the array to sort by distance I am losing record of what instance id it is. So then my function spawns the “sorted” list using instance 0, 1, 2, 3, etc, etc… When really this could be level instance 999324384… Or whatever.

And to be totally honest I’m not sure how I would make the function reference these specific instances in the “unsorted” order then re-reference them and add them to a new sorted array as well, or some sort of way to keep the HISM ref 1:1 for the int ref.

Any Ideas? Thank you for your help thus far.

So, are both arrays the same length?
image

Or are they not equal?
image

1 Like

Here’s a quick example I made, I might think of something better later let me know if this works for you.
I used the dictionary so we only calculate the distance once. The array is filled with actors but it should work with other object types.

2 Likes

Both would be the same length. It does a trace and for every hit instance adds to an array, and then add its instance id to an array.

€dit: I don’t think you need to cast here. You can right-click on the Transform of the GetInstanceTransform node and select SplitStructPin.

1 Like

Your second loop first index should be arrayIndex+1 to avoid comparing the same item against itself.

1 Like

Hey, funny enough I literally handled this in my game today as well. There’s no “answer” on this post so I decided I’ll throw what I did together. I can walk you through exactly what I’m doing as well.

To summarize it, every few seconds this event receives a list of selectable objects, calculates their distances, and sorts the array in order of closest to furthest. Then a UI element grabs these and displays them in a box.

The array I’m sorting is “Stations”. It contains an array of actor objects.

I first loop through each station. I take each station, and compare that station among every other station beyond it (the 2nd for loop takes the index of that element, and then the ending index, to loop between those two).

During this second loop, I use these functions to check distance: Get Actor Location (to get both my pawn’s location, and the station’s location), and Distance (Vector) which does an automatic calculation in distance between the two actors. I do this both with the foreach element, and the for loop element.

Then after getting these two distances, I check if the for loop element is closer to me (less than) than the for each loop element. I run that into a branch and then lastly input a Swap.

To check, you can run another quick for loop against your array after completed and just Print String the array.

2 Likes

It seems like we used the same concept overall, and also similar to L1z4rD89 you should start the second loop with previous loop index + 1 so you don’t compare the same element with itself and do redundant swapping.

2 Likes

Yeah I looked back at it and noticed the same thing a little bit ago. Will change that, thanks.

2 Likes

This ended up being my solution, thanks so much for everyone who replied but I also edited it to add +1 of the fore each loop index to the for loop as Kaidoom15 pointed out.

Thank you for your help man. Much appreciated.

I know its been two years but I just wanted to thank you for this! Had been banging my head on this issue for a little while, the swap node is what I was missing!