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
in Engine by (655 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.

by (564 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.
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 webmaster@godotengine.org with your username.