Menu
CoddyTech

Variables on a Node

Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 13 of 27.

A variable made at the top of a script, outside any function, belongs to the node. It keeps its value from frame to frame, and every function of the script can use it:

extends Node2D

var speed = 150

func _process(delta):
	position.x += speed * delta

Because it lasts, a node variable can change over time. Add to speed on every frame and the node goes faster and faster:

func _process(delta):
	speed += 50 * delta

A var made inside a function is made again on every call and forgotten at the end.

challenge icon

Challenge

Easy

The script has a variable speed that starts at 0. Make the robot speed up:

  • On every frame, add 200 every second to speed.
  • Move the robot right at speed pixels a second.

After one second it moves at 200 pixels a second, after two at 400.

Try it yourself

extends Node2D

var speed = 0

func _process(delta):
	position.x += 100 * delta
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Nodes and Scenes

Practice on your own: Godot playground