스크립트를 처음 들어가보면 extends 라는 단어가 보인다.

#include 같은건가 싶어 찾아보았는데 상속이더라

Sprite2D를 Extends 하면 Sprite2D를 상속받는 자식 클래스를 만드는 것이다.

 

Every GDScript file is implicitly a class. The extends keyword defines the class this script inherits or extends.

 

https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#inheritance

 

Inheritance

A class (stored as a file) can inherit from:

  • A global class.
  • Another class file.
  • An inner class inside another class file.

Multiple inheritance is not allowed.

Inheritance uses the extends keyword:

# Inherit/extend a globally available class.
extends SomeClass

# Inherit/extend a named class file.
extends "somefile.gd"

# Inherit/extend an inner class in another file.
extends "somefile.gd".SomeInnerClass
 

If inheritance is not explicitly defined, the class will default to inheriting RefCounted.

To check if a given instance inherits from a given class, the is keyword can be used:

# Cache the enemy class.
const Enemy = preload("enemy.gd")

# [...]

# Use 'is' to check inheritance.
if entity is Enemy:
	entity.apply_damage()

To call a function in a super class (i.e. one extend-ed in your current class), use the super keyword:

super(args)

This is especially useful because functions in extending classes replace functions with the same name in their super classes. If you still want to call them, you can use super:

func some_func(x):
	super(x) # Calls the same function on the super class.

If you need to call a different function from the super class, you can specify the function name with the attribute operator:

func overriding():
	return 0 # This overrides the method in the base class.

func dont_override():
	return super.overriding() # This calls the method as defined in the base class.

 

 

'Godot4' 카테고리의 다른 글

고도엔진과 Your first 2D game  (0) 2023.11.23
GDScript  (0) 2023.11.23
signals  (0) 2023.11.22
inputs  (1) 2023.11.22
Godot Basics  (0) 2023.11.22