Menu
CoddyTech

Your Own Functions

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

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

Wrap the robot at the right and bottom edges with your own function.

  1. Under _process(), write func wrap_around(): holding the if for x past 640, plus one that sets y past 360 back to 0.
  2. In _process(), call wrap_around() after the robot moves, in place of the if.

Press Play: the robot drifts down and right, and always comes back on the screen. Then Check.

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