GDScripted created button doesn't change scene

Godot Version

4.4.1.stable

Question

My game creates buttons with gdscript and my intention is to change to a different scene once the button is pressed. However, when testing my game, nothing happens when I click on the button for some reason. Code below:

extends Node2D

var cities_array = [
	"Los Angeles",
	"San Fransisco",
	"San Diego"
]

var cities_location_array = [
	Vector2(1635, 2930),
	Vector2(1525, 2848),
	Vector2(1649, 2948)
]

const CITY = preload("res://sprites/city.png")

# Called when the node enters the scene tree for the first time.
func _ready():
	populate_cities()

func populate_cities():
	for i in cities_array.size():
		var btn = Button.new()
		btn.icon = CITY
		btn.text = str(cities_array[i])
		btn.flat = true
		btn.alignment = HORIZONTAL_ALIGNMENT_LEFT
		btn.position = cities_location_array[i]
		btn.pressed.connect(cargo_scene)
		add_child(btn)

func cargo_scene():
	get_tree().change_scene_to_file("res://scripts/loading_cargo.gd")

I’m not entirely sure what the script is attempting to do here. If you want to switch to a different scene, shouldn’t the path passed to change_scene_to_file lead to a .tscn instead of a script file?

1 Like

…ok now I feel like an idiot, lmao. I’ll try that and report back.

Ok, that worked, lmao. My mistake.

1 Like