How to call an out of bounds index on an array without an error

Godot Version

Godot v4.1.3

Question

  • the scene starts containing an instance of fctry_1
  • the root node of every level scene is called ‘room’
  • the code:
@export var skip_to_level:bool
@export var skip_destination:String# either set the level's filename (fctry_1) or its number (0)
var level:Array = ["res://levels/fctry_1.tscn","res://levels/fctry_2.tscn","etc"]
var current_level:int = 0
func _ready()->void:
	if skip_to_level == true and skip_destination:
		if ResourceLoader.exists("res://levels/"+skip_destination+".tscn"):
			current_level = level.find(skip_destination)
			print(level.find(skip_destination))
			add_child(ResourceLoader.load("res://levels/"+skip_destination+".tscn").instantiate())
		elif int(skip_destination) == 0:
			print("Looks like you've either tried to skip to the first level or inputted an invalid non-integer value. Let's just remove the level out of sheer spite");skip_to_level = false
		elif ResourceLoader.exists(level[int(skip_destination)]):
			current_level = int(skip_destination)
		else: print("The level you have tried to skip to does not exist. Input a different name or integer or turn off skip_to_level.");skip_to_level = false
		get_node("room").queue_free()
		if skip_to_level:add_child(ResourceLoader.load(level[current_level]).instantiate());print("Skipped to level "+level[current_level])
	add_child(ResourceLoader.load("res://lynca1.tscn").instantiate())

Inputting an invalid value (4 for example) as skip_destination gives an “Out of bounds get index ‘4’ (on base: ‘Array’)” error, which is a valid error message but is there a way to get rid of it - or better, check if an array contains that index without triggering the error

use skip_destination.is_valid_int()

@export var skip_to_level: bool
@export var skip_destination: String  # either set the level's filename (fctry_1) or its number (0)
var level: Array = ["res://levels/fctry_1.tscn", "res://levels/fctry_2.tscn", "etc"]
var current_level: int = 0

func _ready() -> void:
	if skip_to_level && skip_destination:
		if ResourceLoader.exists("res://levels/" + skip_destination + ".tscn"):
			current_level = level.find(skip_destination)
			print(level.find(skip_destination))
			add_child(ResourceLoader.load("res://levels/" + skip_destination + ".tscn").instantiate())
		elif skip_destination.is_valid_int():  # Check if skip_destination is a valid integer
			var destination_index: int = int(skip_destination)
			if destination_index >= 0 && destination_index < level.size():  # Check if index is within bounds
				current_level = destination_index
			else:
				print("The level index you've entered is out of bounds. Please enter a valid index.")
				skip_to_level = false
		else:
			print("The level you've tried to skip to does not exist. Input a different name or integer or turn off skip_to_level.")
			skip_to_level = false
			
		get_node("room").queue_free()
		if skip_to_level:
			add_child(ResourceLoader.load(level[current_level]).instantiate())
			print("Skipped to level " + level[current_level])
	add_child(ResourceLoader.load("res://lynca1.tscn").instantiate())
1 Like