In GDScript, super()
is used to call a parent function. For example:
class_name Pokemon
var name := ""
func attack() -> void:
print(name + " attacked!")
A subclass can then use super()
to call the parent attack
:
class_name Pikachu extends Pokemon
func attack() -> void:
print(name + " used Thunder Shock")!
super()
This would result in "... attacked!"
appearing after "... user Thunder Shock!"
.
super
can also pass arguments. This is especially useful when overriding _init():
class_name Pokemon
var name := ""
func _init(initial_name: String) -> void:
name = initial_name
The subclass then can use super()
to fill those initial values:
class_name Pikachu extends Pokemon
func _init() -> void:
super("Pikachu")
Note that contrary to every other method, _init()
does not require the same arguments between parents and children classes when overriding it.See Also
Related terms in the Glossary