CodeBlanks Logo Icon CodeBlanks

extends and variables

Learn to derive a class with extends Node and define variables.

The extends keyword

In GDScript, a script file typically begins with the extends statement. This keyword designates which Godot class the script inherits from.

extends Node
# This script now inherits all capabilities of a Node object

For instance, writing extends CharacterBody2D for a 2D character inherits built-in movement functions from that class.

Declaring variables with var

In GDScript, variables are declared using the var keyword. A variable can be directly assigned an initial value and modified throughout gameplay.

extends Node

var health = 100
var player_name = "Archer"
var is_alive = true

Variables store runtime data such as character health, ammo counts, or inventory items in memory.

Practice Print character variables

Assign "Hero" to name and "Knight" to role. Print Player: Hero | Role: Knight to the console.

Starter code

Player.cs
extends Node

func _ready():
    # name ve role degiskenlerini tanimlayip yazdir
    pass

Sample solution

Player.cs
extends Node

func _ready():
    var name = "Hero"
    var role = "Knight"
    print("Player: " + name + " | Role: " + role)

Expected output

Player: Hero | Role: Knight

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which keyword in GDScript specifies which Godot class a script inherits from?

  • inherits
  • import
  • package
  • extends Correct answer

Explanation: extends is the inheritance keyword allowing GDScript classes to adopt capabilities from a superclass.

2 Declare speed variable

Question
____ speed = 250
Answer
var speed = 250

Explanation: In GDScript, the 'var' keyword is used to declare variables.

3 Inheritance and variable terms

Term Meaning
extends Statement to inherit from a class
var Keyword to declare a new variable
Node Base class for scene tree elements
health = 100 Assigning initial value to variable

All lessons in Beginner