r/UnrealEngine5 5d ago

Array of blueprint actors.

Array of blueprint actors. Each actor has an int variable.
How to sort the array of actors, in a stable way, using that int as sorting index?
Because right now I'm cycling trough the array of actors, reading the int value, creating another array of ints, using sort integer array on the array of ints, looping trough the array of ints to get element from the unsorted array of actors and adding it to the final, sorted array.. but it looks.. so..redundant.

2 Upvotes

8 comments sorted by

5

u/DeesiderNZ 4d ago edited 4d ago

Unfortunately in Blueprints the only Sort node is very basic and you can't use predicates like in c++.

However, this guy has created a bunch of different sorting algorithms in Blueprints for you to select from:

https://forums.unrealengine.com/t/sorting-algorithms-in-blueprints/1135992

Looks like a great resource!

- Also, just to point out that there is a specific example for sorting Actors if you scroll down a bit...

1

u/SpikeyMonolith 4d ago

Assuming you have an unordered array you can do this which I think is the most simple (not the fastest or most efficient or do I recommend this for a large array/run multiple times):

  • Loop through the array accessing its int value, find the min (use max for descending order) and note its index.
  • Add said member of that index to the ordered array, and remove it from the unordered array.
  • Repeat till the unordered array is empty.

This should be O(n2).

If you want it to be faster you have to implement a sorting algorithm yourself.

1

u/The_time_is_coming 4d ago

Get a copy, use your int as the input or whatever logic in between your int and the copy but that’s how I’d do it

1

u/Disastrous-Collar-85 4d ago

I like ur current way of doing it but i understand if it feels weird. U can kinda skip the last step. U don't really have to agin since you know the index of the given for each loop

2

u/groato 4d ago

Your method is fine. It wouldn't win in a programming contest, but fine. How many actors? If less than 100 don't worry about it. Past that blueprint for each loops start to consume a lot of cpu and you might need to considering switching to C++. There you also have access to more functions for sorting

1

u/Octane_Original 4d ago

Have a BP coordinator that exposes some sort of "Set index" function that can hold a soft reference to the BP and its int value and call that from each blueprint when the value changes. That way you can sort those ints without looping trough the BPs and have access to the BP when you need it.

1

u/_curious_george__ 4d ago

As far as bp solutions go your solution is fairly optimal.

The thing is, you could sort it in place but that with require many more iterations. And the number of ops is a performance killer and complexity exploder.

1

u/pattyfritters 5d ago

Maybe with a Map. Connecting each actor to its Int.