+1 vote

I want to print "Hello" if I don't set an argument. And how is it possible functions not to have necessarily an argument/all possible agruments so it would be ignored then?

extends Node

func _on_Button_pressed():
say_something("bye")

func say_something(string)
print(string)
asked May 20 in Engine by MaaaxiKing (291 points)

1 Answer

+4 votes
Best answer

You can specify default values for arguments like:

func say_something(string = "Hello"):
    print(string)

With that...

say() # will print "Hello"
say("Bye") # will print "Bye"
answered May 20 by jgodfrey (5,248 points)
selected May 20 by MaaaxiKing

Thank you very much!

Is it a bug if this doesn't work: func say_something(string:String="Hello",a)
Seems that if you have set a default to a paramater before another parameter without a default, you have to set a default to the parameter after the first parameter which has a default.
Error message: Default parameter expected.
func say_something(a,string:String="Hello") works

No, that's not a bug. Arguments with default values must be at the end of the function's argument list. That's a fairly common restriction in a number of languages.

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.