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
EasyThe 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 (xpast 640 goes back to 0) and at the bottom edge (ypast 360 goes back to 0). - Call it in
_process(), after the robot moves, in place of theifthat 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Nodes and Scenes
1Your First Godot Game
What Is Godot?Ready Runs OncePrint in GodotThe Scene TreeSet a PositionMove a Node Once3Values You Can Tune
Variables on a NodeConstantsExport VariablesVector2 MathWrap Around the ScreenYour Own FunctionsPractice on your own: Godot playground