I cannot run the program because of this error: ' cannot call method 'get_size' on a null value '

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By symeber
extends Sprite2D

var gameWidth: int = get_window().get_size().x
var gameHeight: int = get_window().get_size().y
var spriteWidth: int = get_texture().get_width()
var spriteHeight: int = get_texture().get_height()

@export var currentSpeed: float = 100.0
@export var acceleration: float = 300.0
@export var maxSpeed: float = 1500.0

var halfSpriteHeight: int = spriteHeight / 2
var lowerLimit: int = gameHeight + halfSpriteHeight


func _enter_tree():
	positionTopCenter()

func _physics_process(delta: float) -> void:
	moveDown(delta)
	if(currentSpeed < maxSpeed):
		addAcceleration(delta)
	
	if(position.y > lowerLimit):
		resetFall()
		pass

func addAcceleration(delta: float) -> void:
	currentSpeed += delta * acceleration

func moveRight(delta: float) -> void:
	position.x += currentSpeed * delta

func moveLeft(delta: float) -> void:
	position.x -= currentSpeed * delta

func moveUp(delta: float) -> void:
	position.y -= currentSpeed * delta

func moveDown(delta: float) -> void:
	position.y += currentSpeed * delta

func resetFall() -> void:
	position.y = 0 - halfSpriteHeight







func positionTopCenter() -> void:
	self.position.x = gameWidth / 2
	self.position.y = spriteHeight / 2

func positionBottomCenter() -> void:
	self.position.x = gameWidth / 2
	self.position.y = gameHeight - (spriteHeight / 2)

func positionLeftCenter() -> void:
	self.position.x = spriteWidth / 2
	self.position.y = gameHeight / 2

func positionRightCenter() -> void:
	self.position.x = gameWidth - (spriteWidth / 2)
	self.position.y = gameHeight / 2

func positionMiddle() -> void:
	self.position.x = gameWidth / 2
	self.position.y = gameHeight / 2
:bust_in_silhouette: Reply From: jgodfrey

That get_window() call requires that the node be part of the scene tree. That code is likely being run before the node gets added to the tree. Changing this:

var gameWidth: int = get_window().get_size().x

to this should fix it:

@onready var gameWidth: int = get_window().get_size().x

(for both the x and yreferences)