In a piece of code I'm writing, I'm using an anonymous function like so:
// first populate the object array "allitems"
// bag is an object
allitems = filter(all_inventory(bag), (:
(!($1->query_property("no steal")) && $1->get())
:) );
// I calculate the value of tmp here, shouldn't be important
allitems = sort_array(allitems,
function (object a, object b, tmp) {
int aw = a->query_weight();
int bw = b->query_weight();
mapping acv = a->query_curr_value();
mapping bcv = b->query_curr_value();
int agv = (acv && acv["gold"] ? acv["gold"] : 0);
int bgv = (bcv && bcv["gold"] ? bcv["gold"] : 0);
aw -= agv;
bw -= bgv;
aw -= bw;
aw = aw + random(tmp) - random(tmp);
if (aw < 0) return -1;
else if (aw > 0) return 1;
else return 0;
},
tmp);
When I try to update this file, the mud segfaults. MudOS v22.2b14. Odd things: It doesn't matter whether the file successfully updates or not. It segfaulted both with syntax errors and without. Also, it sometimes takes a few heart_beats before it crashes.
I have gotten it to work by taking the anonymous function and making it a separate function, then calling it, like so:
int sortitems(object, object, int);
allitems = sort_array(allitems, (: "sortitems" :), tmp);
int sortitems(object a, object b, int tmp) {
// exact same contents as the anonymous function posted above
}
Has anyone else had this sort of thing happen?? I use anonymous functions fairly often, does anyone have any tips on how to avoid this thing in the future??