playMaker

Author Topic: Sorting Arrays`  (Read 1635 times)

adrian

  • Junior Playmaker
  • **
  • Posts: 52
Sorting Arrays`
« on: February 25, 2019, 02:57:09 PM »
Hello!

I am working with a selection array in playmaker for an RTS system. The units get added one by one to an array as gameobjects for use elsewhere.

The "sort" function does not do anything, I think it's only for strings and ints, the question is, how would I go about sorting my array based on the name of the objects?

I guess I could get name, build new arrays, sort, find objects and rebuild but this seems all little over the top. Am I missing something or is there another way to sort a gameobject array by name?

Thank you for reading!

adrian

  • Junior Playmaker
  • **
  • Posts: 52
Re: Sorting Arrays`
« Reply #1 on: February 25, 2019, 03:08:39 PM »
Further to that, when trying to use sort on gameobjects in an array I get the following error, any ideas:

Code: [Select]
ArgumentException: does not implement right interface
System.Collections.Generic.Comparer`1+DefaultComparer[System.Object].Compare (System.Object x, System.Object y) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Comparer.cs:86)
System.Array.compare[Object] (System.Object value1, System.Object value2, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1745)
System.Array.qsort[Object,Object] (System.Object[] keys, System.Object[] items, Int32 low0, Int32 high0, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1722)
System.Array.Sort[Object,Object] (System.Object[] keys, System.Object[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1675)
Rethrow as InvalidOperationException: The comparer threw an exception.
System.Array.Sort[Object,Object] (System.Object[] keys, System.Object[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1678)
System.Array.Sort[Object] (System.Object[] array, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1623)
System.Collections.Generic.List`1[System.Object].Sort () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:568)
HutongGames.PlayMaker.Actions.ArraySort.OnEnter () (at Assets/PlayMaker/Actions/Array/ArraySort.cs:25)
HutongGames.PlayMaker.FsmState.ActivateActions (Int32 startIndex) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:205)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:175)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2768)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2715)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2643)
HutongGames.PlayMaker.Fsm.DoTransition (HutongGames.PlayMaker.FsmTransition transition, Boolean isGlobal) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2682)
HutongGames.PlayMaker.Fsm.ProcessEvent (HutongGames.PlayMaker.FsmEvent fsmEvent, HutongGames.PlayMaker.FsmEventData eventData) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2253)
HutongGames.PlayMaker.Fsm.BroadcastEvent (HutongGames.PlayMaker.FsmEvent fsmEvent, Boolean excludeSelf) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2521)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2402)
HutongGames.PlayMaker.Actions.SendEvent.OnEnter () (at Assets/PlayMaker/Actions/StateMachine/SendEvent.cs:42)
HutongGames.PlayMaker.FsmState.ActivateActions (Int32 startIndex) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:205)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:175)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2768)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2715)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2643)
HutongGames.PlayMaker.Fsm.UpdateState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2784)
HutongGames.PlayMaker.Fsm.Update () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:1995)
PlayMakerFSM.Update () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:579)
« Last Edit: February 25, 2019, 03:15:54 PM by adrian »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sorting Arrays`
« Reply #2 on: February 26, 2019, 03:18:09 AM »
Hi,

 you can't sort GameObjects like that, what would be the property that is used to sort them? name, position, rotation, distance from a point or object, property from one of its component?

so if you want to sort by name, you need to save the names of your gameobjects in a second array, sort that array, and use the indexes of the name array to get to the right GameObject in your first array.

does that make sense?

Bye,

 Jean

adrian

  • Junior Playmaker
  • **
  • Posts: 52
Re: Sorting Arrays`
« Reply #3 on: February 27, 2019, 01:56:02 PM »
Hi Jean,

Thank you for your response, that does make sense as an idea but I am unsure how to implement. For context, I am trying to sort a array of units which add themselves via an event to the main array when rect transform selected (rts). I use "get next" when issuing orders to all units in array but want consistent ordering. How would I create a new ordered array? Would I have to use a find function from the name (string) and re-add all units to a new ordered array?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sorting Arrays`
« Reply #4 on: February 28, 2019, 01:50:56 AM »
Hi,

 then you would use GetNext on the array filled up with names, and get to the gameobject unit you want to call using that name.

Have a go and if you struggle with this, I'll do a quick sample.

Bye,

 Jean