This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

How do I store a built in function (such as <) in a variable?

The GDScript documentation about referencing functions suggests:

var comparator = funcref(self, "<")
comparator.call_func(1, 2)

This produces the error:

Invalid call. Nonexistent function 'call_func' in base 'FuncRef'.

The result I expect in the example above is true.

in Engine by (23 points)

2 Answers

+2 votes
Best answer

The character < is an operator, so there is no way to store it in a variable. You can write a static function which returns its result though:

static func lt(x, y):
    return x < y

var comparator = funcref(self, "lt")
comparator.call_func(1, 2)

Thanks to bojidar_bg for their suggestions on IRC.

by (23 points)
+1 vote

Unlike C++ operators aren't themselves functions you can call. Funcref will only store methods of an instance. It's basically a form of that you can pack into a var and courier off to another method, for example, for the _notification() of one object packing a method call into a message queue to courier off to another object's _process() method or _draw() method.

You could also store a Funcref along with some parameters into a dictionary to cobble up a currying (baking parameters to a function call) mechanism. Actually that's an idea for an object I should make to share in Projects.

by (45 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.