How to call a function without necessarily requiring arguments

Godot Version

Godot v4.3

Question

So I made a test function to have a Controi node trigger a level change

func _process(delta:float)->void:
	mousepos = get_viewport().get_mouse_position()
	if Input.is_action_just_pressed("click"): get_parent().changelevel()

and

# Changes the current level to either the next one or the one corresponding to the int entered into the function
func changelevel(dest:int)->void:
	if str(dest).is_valid_int(): current_level = dest
	else: current_level = current_level+1
	get_node("room").free()
	add_child(ResourceLoader.load(level[current_level]).instantiate())
	print("Changed to level["+str(current_level)+"] = "+level[current_level])

(all level scenes are called β€˜room’)

You can use default values for the parameters. More info here GDScript reference β€” Godot Engine (stable) documentation in English

func changelevel(dest:int = -1)->void:
	if dest == -1:
		current_level = current_level + 1
	else:
		current_level = dest
	#...
2 Likes