- Home /
Sort an ArrayList of game objects by another script variable
So here the trick im trying to do, I am building a RPG-type game, And i have all the battle characters stored in a list. I want to sort this list so that the character with the highest turn speed(TSpeed number is at the top of the list(for simplicity's sake) to designate turns. Problem: How do i sort the ArrayList by the variable that is stored in another script? Here is the script that contains the Arraylist
using UnityEngine;
using System.Collections;
public class BattleSystem : MonoBehaviour {
GameObject[] Enemies;
GameObject[] Players;
ArrayList Combatants = new ArrayList();
float starttimer, currenttimer;
bool isPlayer= false;
int curCombatantIndex;
GameObject curCombatantObj;
// Use this for initialization
void Start () {
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
Players = GameObject.FindGameObjectsWithTag("Player");
foreach ( GameObject enemy in Enemies)
{
Combatants.Add(enemy);
}
foreach ( GameObject player in Players)
{
Combatants.Add(player);
}
enter code here
For the sake of refences, all of the charcters have the same script attached to them that holds their stats. Heres that script: using UnityEngine; using System.Collections;
public class PlayerStats : MonoBehaviour {
public float Health= 10.0f;
public float TSpeed= 10.0f;
public float AP= 10.0f;
public float PhysDef= 10.0f;
public float MagDef= 10.0f;
public float PhysATK= 10.0f;
public float MagATK= 10.0f;
public float PAL= 10.0f;
public float EXP= 10.0f;
}
I have heard of the IComparer function, but i can't really find anything that helps me out so far. Any help is appreciated Also, keep it C# pls
Answer by FL · Jan 27, 2013 at 12:53 AM
I use the second code in item 2 of http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html . Use
Array.Sort(putYouArrayHere,(IComparer)new sortPlayerStatsSpeed());
Put in your BattleSystem class:
private class sortPlayerStatsSpeed: IComparer{
int IComparer.Compare(object a, object b){
PlayerStats p1=((GameObject)a).GetComponent<OtherScript>();
PlayerStats p2=((GameObject)a).GetComponent<OtherScript>();
if (p1.TSpeed > p2.TSpeed)
return 1;
else if (p1.TSpeed < p2.TSpeed)
return -1;
else
return 0;
}
}
Thank you so much! I made two edits of this so for the reference you have it
PlayerStats p1=((GameObject)a).GetComponent<OtherScript>();
PlayerStats p2=((GameObject)b).GetComponent<OtherScript>();
and i used hits call
Combatants.Sort((IComparer)new sortPlayerStatsSpeed());
the other threw an error at me. XD
Try: Array.Sort(Combatants,(IComparer)new sortPlayerStatsSpeed());
Answer by mstultz · Jan 27, 2013 at 04:25 AM
You know, instead of using an ArrayList, you could use a List< PlayerStats > and then use System.Linq to sort by specific attributes (via OrderBy or OrderByDescending). For example you could change the commented lines of your original question:
//ArrayList Combatants = new ArrayList(); becomes:
List< PlayerStats > Combatants = new List< PlayerStats >();
// Then...
//Combatants.Add(enemy); becomes:
Combatants.Add( enemy.GetComponent< PlayerStats >() );
//Combatants.Add(player); becomes:
Combatants.Add( player.GetComponent< PlayerStats >() );
// Finally...
var sortedCombatantsAscending = Combatants.OrderBy( x => x.TSpeed ); // for 1, 2, 3
var sortedCombatantsDescending = Combatants.OrderByDescending( x => x.TSpeed ); // for 3, 2, 1
I'm not sure about Java, but it definitely works for C#; I tested out the sorting code before submitting my answer. Just be sure to include the 'using System.Linq;' statement.