I’m attempting to write a Race script (which has been written in Javascript), which needs a sorted list of RaceStat objects, a custom class I wrote. The simplest way to do this is through Array.Sort(), which requires that <> operators be overridden (I assume).
So in order to accomplish that goal, I wrote RaceStats in C#. However, no matter what I do, Race.js refuses to accept RaceStats as a valid type. Is there some inter-language issue happening here? (If so, the fact that I have always been able to use Mathfx.cs functions from Javascript would now confuse me.)
To my knowledge Javascript does not have the ability to override operators; is this correct?
Is it possible to use C# classes within Javascript?
Thanks freyr. The operator methods do (seem to) work; I compare the RaceStats of the most recent run to that of the best run without any errors.
Unfortunately, when I try to sort an array of them, it throws:
Cannot cast from source to destination type
I’ve overridden all the operators that would be needed for sorting, I think. Are there any more that need to be overloaded? Or is Array.Sort just broken for nonstandard types?
Race.js snippet:
var orderedStats = new Array();
orderedStats.Add(lastRunStats);
for (o=0; o < opponentNames.length; o++) {
if (o < opponentLapTimes.length) {
opponentStats[o] = GenerateRandomStats(opponentNames[o], opponentLapTimes[o], opponentLapVariation);
orderedStats.Add(opponentStats[o]);
}
}
orderedStats.Sort();//error here
RaceStats.js snippet
static function op_Equality (x : RaceStats, y : RaceStats) {
return (x.raceTime == y.raceTime);
}
static function op_LessThan (x : RaceStats, y : RaceStats) {
return (x.raceTime == y.raceTime);
}
static function op_LessThanOrEqual (x : RaceStats, y : RaceStats) {
return (x.raceTime == y.raceTime);
}
static function op_GreaterThan (x : RaceStats, y : RaceStats) {
return (x.raceTime == y.raceTime);
}
static function op_GreaterThanOrEqual (x : RaceStats, y : RaceStats) {
return (x.raceTime == y.raceTime);
}
Actually implementing the comparison operators will not help you. For Sort to work, you need to make your class implement the IComparable interface, which consists of a single method, CompareTo:
class RaceStats extends IComparable {
var raceTime : float;
// ...
function CompareTo(obj: Object) : int {
var other :MyClass = obj; // Typecast to own class
return raceTime.CompareTo(other.raceTime);
}
}
If you for some reason can’t make your custom class implement IComparable (f.ex. if it’s a MonoBehaviour), an alternative is to create a helper class that implements IComparer and pass an instance of it to the Sort function.
class RaceStatsComparer extends IComparer {
function Compare(x : Object, y : Object) : int {
x.raceTime.CompareTo(y.raceTime);
}
}
...
myArray.Sort(new RaceStatsComparer ());