@@ -1572,6 +1572,62 @@ Lambda functions cannot be declared static.
1572
1572
1573
1573
See also `Static variables `_ and `Static constructor `_.
1574
1574
1575
+ Variadic functions
1576
+ ~~~~~~~~~~~~~~~~~~
1577
+
1578
+ A variadic function is a function that can take a variable number of arguments.
1579
+ Since Godot 4.5, GDScript supports variadic functions. To declare a variadic function,
1580
+ you need to use the *rest parameter *, which collects all the excess arguments into an array.
1581
+
1582
+ ::
1583
+
1584
+ func my_func(a, b = 0, ...args):
1585
+ prints(a, b, args)
1586
+
1587
+ func _ready():
1588
+ my_func(1) # 1 0 []
1589
+ my_func(1, 2) # 1 2 []
1590
+ my_func(1, 2, 3) # 1 2 [3]
1591
+ my_func(1, 2, 3, 4) # 1 2 [3, 4]
1592
+ my_func(1, 2, 3, 4, 5) # 1 2 [3, 4, 5]
1593
+
1594
+ A function can have at most one rest parameter, which must be the last one in the parameter list.
1595
+ The rest parameter cannot have a default value. Static and lambda functions can also be variadic.
1596
+
1597
+ Static typing works for variadic functions too. However, typed arrays are currently not supported
1598
+ as a static type of the rest parameter:
1599
+
1600
+ ::
1601
+
1602
+ # You cannot specify `...values: Array[int]`.
1603
+ func sum(...values: Array) -> int:
1604
+ var result := 0
1605
+ for value in values:
1606
+ assert(value is int)
1607
+ result += value
1608
+ return result
1609
+
1610
+ .. note ::
1611
+
1612
+ Although you can declare functions as variadic using the rest parameter, unpacking parameters
1613
+ when calling a function using *spread syntax * that exists in some languages (JavaScript, PHP)
1614
+ is currently not supported in GDScript. However, you can use ``callv() `` to call a function
1615
+ with an array of arguments:
1616
+
1617
+ ::
1618
+
1619
+ func log_data(...values):
1620
+ # ...
1621
+
1622
+ func other_func(...args):
1623
+ #log_data(...args) # This won't work.
1624
+ log_data.callv(args) # This will work.
1625
+
1626
+ Abstract functions
1627
+ ~~~~~~~~~~~~~~~~~~
1628
+
1629
+ See `Abstract classes and methods `_.
1630
+
1575
1631
Statements and control flow
1576
1632
---------------------------
1577
1633
@@ -2047,7 +2103,7 @@ If you want to use ``extends`` too, you can keep both on the same line:
2047
2103
Named classes are globally registered, which means they become available to use
2048
2104
in other scripts without the need to ``load `` or ``preload `` them:
2049
2105
2050
- .. code-block :: gdscript
2106
+ ::
2051
2107
2052
2108
var player
2053
2109
@@ -2070,30 +2126,45 @@ in other scripts without the need to ``load`` or ``preload`` them:
2070
2126
2071
2127
.. _doc_gdscript_basics_abstract_class :
2072
2128
2073
- Registering abstract classes
2129
+ Abstract classes and methods
2074
2130
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2075
2131
2076
- Since Godot 4.5, you can register abstract classes using the ``abstract `` keyword.
2077
- An abstract class is a class that cannot be instantiated directly. Instead, it
2078
- is meant to be subclassed by other classes. Attempting to instantiate
2132
+ Since Godot 4.5, you can define abstract classes and methods using
2133
+ the ``@abstract `` annotation.
2134
+
2135
+ An abstract class is a class that cannot be instantiated directly.
2136
+ Instead, it is meant to be inherited by other classes. Attempting to instantiate
2079
2137
an abstract class will result in an error.
2080
2138
2139
+ An abstract method is a method that has no implementation. Therefore, a newline
2140
+ or a semicolon is expected after the function header. This defines a contract that
2141
+ inheriting classes must conform to, because the method signature must be compatible
2142
+ when overriding.
2143
+
2144
+ Inheriting classes must either provide implementations for all abstract methods,
2145
+ or the inheriting class must be marked as abstract. If a class has at least one
2146
+ abstract method (either its own or an unimplemented inherited one),
2147
+ then it must also be marked as abstract. However, the reverse is not true:
2148
+ an abstract class is allowed to have no abstract methods.
2149
+
2150
+ .. tip ::
2151
+
2152
+ If you want to declare a method as optional to be overridden, you should use
2153
+ a non-abstract method and provide a default implementation.
2154
+
2081
2155
For example, you could have an abstract class called ``Shape `` that defines
2082
- a method called ``draw() ``. You can then create subclasses like ``Circle ``
2156
+ an abstract method called ``draw() ``. You can then create subclasses like ``Circle ``
2083
2157
and ``Square `` that implement the ``draw() `` method in their own way.
2084
2158
This allows you to define a common *interface * for all shapes without
2085
2159
having to implement all the details in the abstract class itself:
2086
2160
2087
- .. code-block :: gdscript
2161
+ ::
2088
2162
2089
- abstract class Shape:
2090
- func draw():
2091
- # It is possible for subclasses to call the parent class method using `super()`.
2092
- # In this example, we won't use `super()` to call the parent class method,
2093
- # so we can leave this method empty.
2094
- pass
2163
+ @abstract class Shape:
2164
+ @abstract func draw()
2095
2165
2096
2166
# This is a concrete (non-abstract) subclass of Shape.
2167
+ # You **must** implement all abstract methods in concrete classes.
2097
2168
class Circle extends Shape:
2098
2169
func draw():
2099
2170
print("Drawing a circle.")
@@ -2102,16 +2173,17 @@ having to implement all the details in the abstract class itself:
2102
2173
func draw():
2103
2174
print("Drawing a square.")
2104
2175
2105
- Both subclasses and classes created using ``class_name `` can be abstract. This
2106
- example creates two abstract classes, one of which is a subclass of another
2176
+ Both inner classes and classes created using ``class_name `` can be abstract.
2177
+ This example creates two abstract classes, one of which is a subclass of another
2107
2178
abstract class:
2108
2179
2109
- .. code-block :: gdscript
2180
+ ::
2110
2181
2111
- abstract class_name AbstractClass
2182
+ @abstract
2183
+ class_name AbstractClass
2112
2184
extends Node
2113
2185
2114
- abstract class AbstractSubClass:
2186
+ @ abstract class AbstractSubClass:
2115
2187
func _ready():
2116
2188
pass
2117
2189
@@ -2132,6 +2204,14 @@ abstract class:
2132
2204
2133
2205
Cannot set object script. Script '<path to script>' should not be abstract.
2134
2206
2207
+ Unnamed classes can also be defined as abstract, the ``@abstract `` annotation
2208
+ must precede ``extends ``:
2209
+
2210
+ ::
2211
+
2212
+ @abstract
2213
+ extends Node
2214
+
2135
2215
Inheritance
2136
2216
~~~~~~~~~~~
2137
2217
0 commit comments