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.
+2 votes

Currying is the process of baking some or all arguments to a function call for later use. You can then call a curried function, supplying missing parameters, and the actual call includes the stored arguments, combined with the ones provided when the curried function is called.

We have to abuse PackedScene (and ResourceLoader) to get instances of the Curry object, as will be seen in the SpiceRack test script.

  • Create a new project. script:

extends Node

#  Currying bakes arguments into a function call for later use
#  You can curry some or all of a method's paramters, than
#  provide the remainder at the time of the call.
#  One particular use of curry in GDScript is to work around
#  call_deferred's 5-argument limitation.

var curry_call=[]
var curry_args=[]

func call_me(arglist):
    if curry_call.size()<2:
        var ob=curry_call[0]
        var me=curry_call[1]
        var args=[]
        # add the curried arguments
        for arg in curry_args:
        # add the provided arguments to the curried ones
        for arg in arglist:
        # now do the appropriate

func curry(ob,me,args):
    for arg in args:

func _ready():
  • Create a Node, call it Curry, extend it (set script) to
  • Save as Curry.tscn script:

extends Node

onready var Curry=load("Curry.tscn")

func testfunc(a="",b="",c="",d=""):
    print("a=",a,", b=",b,", c=",c,", d=",d)

func somefunc(curried):

func _ready():

    var curry1=Curry.instance()

    var curry2=Curry.instance()


  • Create a Node, call it SpiceRack, extend it (set script) to
  • Save as SpiceRack.tscn
  • In project settings, set default scene to SpiceRack.tscn


Edit: Removed the inelegant wall of if's.

in Engine by (45 points)
edited by

Instead of a bunch of ifs, you could use callv which expects an array of arguments.

Indeed, that works perfectly.

@vnen Using callv would lift the argument count limit (thus removing the need for the min() clause) too, wouldn't it?

Yes, with callv you don't need to limit the argument count.

Please log in or register to answer this question.

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.