Menu
CoddyTech

Your Own Functions

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

Godot calls _ready() and _process() by itself. You can also write your own functions, to give a few lines a name:

func jump_to_start():
	position = Vector2(50, 250)

A function runs only when code calls it, by writing its name with brackets: jump_to_start(). Call it from _process() and it runs on every frame. Call it from _ready() and it runs once.

A good name says what the function does. Then _process() reads like a list of steps: move, then wrap.

challenge icon

Challenge

Easy

The robot moves down and to the right, and wraps only at the right edge.

  • Write a function wrap_around() that wraps at the right edge (x past 640 goes back to 0) and at the bottom edge (y past 360 goes back to 0).
  • Call it in _process(), after the robot moves, in place of the if that is there.

Try it yourself

extends Node2D

var velocity = Vector2(200, 100)

func _process(delta):
	position += velocity * delta
	if position.x > 640:
		position.x = 0
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