Advertisement

[SOLVED] How to pass global function as callback?

Started by November 12, 2018 06:38 PM
4 comments, last by WitchLord 6 years ago

Following the example from the docs, this works:


array<int> arr = {3,2,1};
arr.sort(function(a,b) { return a < b; });

I would expect the following to work:


bool less(int a, int b) { return a < b; }
...
array<int> arr = {3,2,1};
arr.sort(less);

but instead it errors:


ERROR : No matching signatures to 'array::sort(::less)'
INFORMATION : Candidates are:
INFORMATION : void array::sort(less&in, uint startAt = 0, uint count = uint ( - 1 ))

How do I pass array.sort an existing function instead of an anonymous function? I'm using Angelscript 2.32.0

The comparison function must take the arguments by reference.

Try this:


bool less(const int &in a, const int &in b) {return a < b;}

I'll update the documentation to give an example with a global function too for clarity.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

Okay, now how do I get this to work?


bool weaker(const Info@ &in a, const Info@ &in b) { return a.hp < b.hp; }
array<const Info@> attackableEnemiesByHp() {
    array<const Info@> attackable_enemies = attackableEnemies();
    
    attackable_enemies.sort(weaker); // ERROR No matching signatures
      
    return attackable_enemies;
}

Info is a class representing an entity in the game.

Unfortunately you've hit a previously unknown problem. I'm working on a fix for it, but for now you can work around it with the following:


attackable_enemies.sort(function(a,b) { return weaker(a,b); });

 

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've fixed this now in revision 2561.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement