Jump to content
  • Advertisement
Dinkleberg

[SOLVED] How to pass global function as callback?

Recommended Posts

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

Edited by Dinkleberg

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Edited by Dinkleberg

Share this post


Link to post
Share on other sites

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); });

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!