Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How do I sort an array by distance?

Hello, I'm creating a third person shooter game and I want my companions to follow me in a V Formation. I have gotten the formation already but the function that I used to sort the companion array by distance is not working well. Because the formation is symmetrical, when the player continues to move in the same direction, the two companions beside it maintain the same distance from it, hence the function tends to resort the array and make the two companions swap sides.

I have tried sorting the array only once so that the index assigned to each actor is the same throughout but this is not the ideal result I was going for as it is a very inflexible and inefficient way.

I'm currently following this blueprint from @Benni.Lodge. (Thank you!)

alt text

How can I program the sorting array such that when two values are the same, the formation will still be consistent?

I am still new to UE4 and have been struggling for days for my project. Hope to hear from all of you soon. Thank you so much for your help!

alt text

Product Version: UE 4.16
formation.png (316.3 kB)
more ▼

asked Nov 28 '17 at 03:12 AM in Blueprint Scripting

avatar image

18 1 2 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Here we go. You are looking for a sort algorithm available in the TArray class, called Sort. Click here to see more information about C++ Arrays in UE4

You will need to create a Blueprint Function Library if you want to use it in Blueprints. First of all, go to UE, right click in any content folder and create a new C++ class derived from Blueprint Function Library.

I made mine and named it as StaticFunctions. When it is successfully created and compiled, go to your .h file and do this declaration: alt text

Then go to your .cpp file, and create the definition: alt text

Go to UE and click in Compile button beside the Play button. Once the code has compiled, all you need to do is call OrderByDistance in any blueprint you wish to use it. alt text

ueans0.png (13.3 kB)
ueans1.png (27.2 kB)
ueans2.png (63.1 kB)
more ▼

answered Nov 28 '17 at 11:23 AM

avatar image

315 3 7 11

avatar image emilyong Nov 29 '17 at 06:12 AM

What about pure blueprint scripting? I have tried this method but it doesn't work :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

If you want it to be BP only, this setup should work. If you want to order them from closest to farthest, change the Max of Float Array function for the Min version.

alt text

more ▼

answered Aug 10 '19 at 09:56 AM

avatar image

277 15 18 21

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question