Bound Checks

2025/12/27

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
Assets
All else
Copyright 2016-2026, GDQuest
Created
2026/02/16
Updated
2025/12/27

Bound Checks

A "bound check" or "boundary check" is when you check if an index is within the bounds of an array.

For example, in the case of the following Array:

var characters := ["Donald", "Scrooge", "Goofy"]

The following indices are valid:

characters[0]
characters[1]
characters[2]

However, the following are not:

characters[3]
characters[4]
characters[1000]

Trying to access those indices will not give you errors in the editor, because Godot cannot infer, or guess, how many items are in the array. But it will give an error at runtime.

Checking if the index we are using is actually valid, like so, is called a "bound check":

func select_character(character_index := 0) -> void:
	if character_index < 0:
		push_error("Index is invalid: it is lower than 0")
	elif character_index >= characters.size():
		push_error("Index is invalid: It is higher than the amount of characters")
	else:
		var selected_character: String = characters[character_index]
		print("Selected character: " + selected_character)
Become an Indie Gamedev with GDQuest!

Don't stop here. Step-by-step tutorials are fun but they only take you so far.

Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!