Self Keyword

2026/04/07

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
MIT
Game Assets
2016-2026, GDQuest© - All rights reserved
All else
2016-2026, GDQuest© - All rights reserved
Created
2026/06/06
Updated
2026/04/07

Self Keyword

The self keyword references the current object instance. For example, to reference the position property of a Node2D, we may write:

extends Node2D

func _ready() -> void:
  self.position = Vector2(200, 200)

This is equivalent to writing:

func _ready() -> void:
  get_node(".").position = Vector2(200, 200)

Or even:

func _ready() -> void:
  get_node(get_path()).position = Vector2(200, 200)

All those snippets allow to reference the node itself.

Of course, most times, you don't need to do this at all. Simply writing the property name position without any prefix accomplishes the same:

extends Node2D

func _ready() -> void:
  position = Vector2(200, 200)

So you rarely need to use self.

When to use `self`?

There are two instances where self is mandatory:

Shadowing

"shadowing" is when a new variable named like the class variable prevents accessing it. Let's say you have a function that sets the position, like so:

func change_position(position: Vector2) -> void:
  # ...

How would you be able, in the function body, to differentiate between the property position and the parameter position?

func change_position(position: Vector2) -> void:
  position = position

Writing position would only reassign the parameter to itself, because Godot will only use the last occurence of a name. To fix this, you'd use self:

func change_position(position: Vector2) -> void:
  self.position = position

Of course, it'd be better in this case to change the parameter name. You can read more about this in the scope article.

Reference for other functions

The second, and arguably more essential reason to use self is when you need to reference the node, mostly for tweens.

Let's say you want to tween the current node; the Tween.tween_property() method needs a node to act on. When trying to animate a different node, you might write:

var tween := create_tween()
tween.tween_property($Sprite, "position", Vector2(100, 200), 1.0)

But if you need to tween the current object itself, you'll need to use self:

var tween := create_tween()
tween.tween_property(self, "position", Vector2(100, 200), 1.0)

You might also imagine needing to pass the reference to other nodes.

For example, owner is a property set by the editor automatically when creating nodes, but it's empty for nodes you create programmatically. If you wanted to set it, you may do:

func _ready() -> void:
  var button := Button.new()
  button.owner = self
  add_child(owner)

Dynamic properties

Sometimes, you may want to call a property dynamically. In the example below, we want to run take_damage_[element_name](), where element_name is fire, water, or banana:

func _take_damage_fire(amount: int) -> void:
  # ...

func _take damage_water(amount: int) -> void:
  # ...

func _take_damage_banana(amount: int) -> void:
  # ...

func take_damage(damage_type: String, amount: int) -> void:
  self["_take_damage_"+damage_type].call(amount)

We can't construct the string that designates the methods and call them without using self.

Become an Indie Gamedev with GDQuest!

Don't stop here. Step-by-step tutorials are fun but they only take you so far.

Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!

Site in BETA!found a bug?