I've discovered a rather odd bug when using array.sort() with a comparison function. If I call an API function from within a comparison function I get the following error...
Code: Select all
dbg: [ai] [1] [S] Your script made an error: parameter 0 has an invalid type 'table' ; expected: 'instance'
Code: Select all
array.sort(function (a,b) {
local aval = AITown.GetPopulation(a.id);
local bval = AITown.GetPopulation(b.id);
if(aval > bval) return 1
else if(aval < bval) return -1
return 0;
});
Code: Select all
array.sort(function (a,b) {
local aval = AITown.GetPopulation.call(a, a.id);
local bval = AITown.GetPopulation.call(b, b.id);
if(aval > bval) return 1
else if(aval < bval) return -1
return 0;
});
This is in r15379. And yes I know that the above could sort of be done with an AIList valuator, but I need to sort objects not integers. The actual comparison function I'm using is more complex than this. I have just boiled it down to an example that demonstrates the bug neatly.
Just thought you'd like to know. Thanks for your attention.

----------------
Edit: by the way, if anyone is scratching their heads like I was, trying to figure out how to sort an array of objects by a variable that is not in scope, squirrel provides a thing called free variables...
Code: Select all
local cargo = ......
array.sort(function (a,b):(cargo) {
return ...
}