0 votes

I had never wondered anything about this, but how could I skip arguments when calling a function?

For example:

func _ready():
    my_function(4, b, 6) # How to skip 'b' argument?

func my_function(a = 1, b = 2, c = 3):
    return a + b + c
asked Dec 14, 2019 in Engine by JulioYagami (631 points)

1 Answer

+1 vote

I don't think that is currently possible in GdScript. This is usually made possible by named arguments which is a functionality in many programming languages but not yet supported in GdScript. I believe this pull request is the one you should follow to track when it gets implemented/merged.

You can do a manual implementation of it if you really want, the way to do it is to have your function receive a dictionary as a parameter and you check if they were passed or not to decide if you want to use the default or not. But it might get annoying with having to build a dictionary every time you want to call the function.

answered Dec 15, 2019 by tastyshrimp (556 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 How to use this Q&A? before posting your first questions.