playMaker

Author Topic: Sorting an Array List Based on Distance to Game Object  (Read 6060 times)

mweyna

  • Full Member
  • ***
  • Posts: 242
Sorting an Array List Based on Distance to Game Object
« on: April 06, 2015, 02:09:30 AM »
Does any action that sorts an array list based on a defined object starting point exist? Something that would arrange, A,B,C,D to D,B,C,A if that's the closest order to my starting object.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sorting an Array List Based on Distance to Game Object
« Reply #1 on: April 07, 2015, 03:15:10 AM »
Hi,

 It's on the todo:

https://trello.com/c/jPAktQDt/77-arraymaker-update

 also, there is a sample showing ( Samples/ArrayList/DistanceSorting.unity) you how to do this manually, so it can be done.

 Bye,

 Jean

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Sorting an Array List Based on Distance to Game Object
« Reply #2 on: April 07, 2015, 03:53:13 AM »
Good to know, I'll check it out and added my vote.

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Sorting an Array List Based on Distance to Game Object
« Reply #3 on: March 26, 2016, 07:02:33 AM »
This action has since been created, but I am having a small trouble with it.
Array List Sort Game Object By Distance
https://darkhitori.snipt.net/arraysortgameobjectbydistance/

When sorting the game objects in the list, it does not seem to sort the one in index 0. Also the ones after that are ALWAYS sorted correctly.
Not sure it's a bug, because it does NOT happen EVERY time. I can't explain why it leaves the one object that is surely further away and is in index 0, but sometimes it sorts it correctly.

What I am doing:
I have a bunch of objects in a line in the scene and they are also named in alphabetical order. (A closest, B farther, etc.)
(This also helps in debugging, as I can instantly see if the sorting happened correctly or not.)
I randomly select 10 objects and place them into the array list. At this point it has 10 game objects in it in random order.
Then I use the "Array List Sort Game Object By Distance" action and observe that:
1. sometimes the one in index 0 is left there and all after that are sorted nicely,
2. other times it is all ok, (all sorted nicely in order),
3. and even rarely another object that is not the closest gets put into index 0.

The one thing in common in the results is that in all cases index 1-9 are always sorted properly.

Any idea?
Is there an error in the script that could cause such erratic behavior?

Thanks in advance,
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Sorting an Array List Based on Distance to Game Object
« Reply #4 on: March 26, 2016, 07:12:54 AM »
Looking at it a bunch of time I am pretty sure there is some problem with the sorting algorithm.
I see the pattern:
1. If the closest object is already in index 0 before the sorting then it sorts them all properly. - no problem
2. But if there are 2 or more farther objects (farther than any other object in the array) in index 0 and 1 before the sorting then the sorting won't be error free. The one left or put into index 0 will be one that should be farther down the line.

The order of index 1-9 will always be sorted properly.
The only problem is that in most cases there is one left in index 0 that should be farther down the line.

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Sorting an Array List Based on Distance to Game Object
« Reply #5 on: March 26, 2016, 07:30:19 AM »
:-)
An idea just popped into my head: what if I sorted the array list TWICE!?

And it works: the first one sorts all elements except messes up index 0, but then the 2nd sort solves that and puts it into it's proper place.
:-)

Br,
szomaza

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sorting an Array List Based on Distance to Game Object
« Reply #6 on: March 28, 2016, 02:59:19 AM »
Hi,

 maybe you are sorting too soon.

 when you move an object, you need to wait one frame before the position read by script is actually the one that was set.

So try to wait one frame before sorting, you may get it working this way.

Bye,

 Jean

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Sorting an Array List Based on Distance to Game Object
« Reply #7 on: April 01, 2016, 01:41:06 AM »
Hi Jean,

No, that is not the problem.
Just tried waiting 1 second before the sort and the 1st sort fails the first item ~80% of the times, then the 2nd sort fixes that.

By the way I am not moving items around. They are stationary and loaded from a scene with Load Scene action.

Br,
szomaza