Menu
CoddyTech

Variables on a Node

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

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

Make the robot start still and speed up.

  1. In _process(), add a new line speed += 200 * delta. It adds to speed on every frame, not to the position. Keep var speed = 0 at the top as it is.
  2. Move the robot by speed: replace position.x += 100 * delta with position.x += speed * delta.

Press Play: the robot creeps off, then goes faster and faster. Then Check.

Try it yourself

extends Node2D

var speed = 0

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

All lessons in Nodes and Scenes

Practice on your own: Godot playground