Skip to content

Commit a502dee

Browse files
authored
Merge pull request #11372 from Calinou/gdscript-basics-function-parameter-defaults
Document function parameter defaults in GDScript reference
2 parents c757300 + 2481589 commit a502dee

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ here's an example of how GDScript looks.
6565
var v3 = Vector3(1, 2, 3)
6666

6767

68-
# Functions.
69-
func some_function(param1, param2, param3):
68+
# Function, with a default value for the last parameter.
69+
func some_function(param1, param2, param3 = 123):
7070
const local_const = 5
7171

7272
if param1 < local_const:
@@ -1411,6 +1411,20 @@ function's first argument, unlike Python).
14111411

14121412
A function can ``return`` at any point. The default return value is ``null``.
14131413

1414+
By default, all function parameters are required. You can make one or more
1415+
parameters at the end optional by assigning a default value to them:
1416+
1417+
::
1418+
1419+
# Since the last two parameters are optional, all these calls are valid:
1420+
# - my_function(1)
1421+
# - my_function(1, 20)
1422+
# - my_function(1, 20, 100)
1423+
func my_function(a_required, b_optional = 10, c_optional = 42):
1424+
print(a_required)
1425+
print(b_optional)
1426+
print(c_optional)
1427+
14141428
If a function contains only one line of code, it can be written on one line:
14151429

14161430
::
@@ -2229,7 +2243,7 @@ abstract class:
22292243
an abstract class to a node. If you attempt to do so, the engine will print
22302244
an error when running the scene:
22312245

2232-
::
2246+
.. code-block:: none
22332247
22342248
Cannot set object script. Script '<path to script>' should not be abstract.
22352249
@@ -2351,7 +2365,7 @@ This is better explained through examples. Consider this scenario:
23512365
var message = null
23522366

23532367

2354-
func _init(e=null):
2368+
func _init(e = null):
23552369
entity = e
23562370

23572371

@@ -2363,7 +2377,7 @@ This is better explained through examples. Consider this scenario:
23632377
extends "state.gd"
23642378

23652379

2366-
func _init(e=null, m=null):
2380+
func _init(e = null, m = null):
23672381
super(e)
23682382
# Do something with 'e'.
23692383
message = m
@@ -2382,10 +2396,10 @@ There are a few things to keep in mind here:
23822396

23832397
::
23842398

2385-
# idle.gd
2399+
# idle.gd
23862400

2387-
func _init():
2388-
super(5)
2401+
func _init():
2402+
super(5)
23892403

23902404
Static constructor
23912405
~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)