Append() doesn't seem to work

Godot Version

4.5.stable

Question

I am trying to append an array named “destination_cities” with keys from a different dictionary, but it is not appending for some reason. Code below:

func create_airship_icon(cargo_amount: int, city_origin: String):
	var airship = Button.new()
	
	#Spawns new airship button in World Map
	airship.icon = load("res://sprites/Airship_icon.png")
	airship.flat = true
	var origin_vector = Vector2(map_to_local(WorldMap.cities[city_origin][0]))
	airship.position = origin_vector
	add_child(airship)
	
	#Adds cities within the range of the airship form origion city, while eliminating the origin city
	for key in WorldMap.active_cities:
		var distance = origin_vector.distance_to(map_to_local(WorldMap.cities[key][0]))
		if distance <= 20:
			WorldMap.destination_cities.append(key) #<---Keys are not being added
	WorldMap.destination_cities.erase(city_origin)
	print(WorldMap.destination_cities)
	
	var destination = WorldMap.destination_cities.pick_random() #<--- Can't take value from an empty array
	var destination_vector = Vector2(map_to_local(WorldMap.cities[destination][0]))
	var final_distance = origin_vector.distance_to(destination_vector)

The follwing code is found in the WorldMap autoload:

var cities = {
	"Los Angeles": [Vector2(78, 165), 1000000, 2000000, 1],
	"San Fransisco": [Vector2(73, 161), 1000000, 1000000, 1],
	"San Diego": [Vector2(80, 166), 1000000, 1000000, 0],
	"Chicago": [Vector2(123, 152), 1000000, 1000000, 0],
	"Houston": [Vector2(113, 169), 1000000, 1000000, 0],
	"San Antonio": [Vector2(108, 170), 1000000, 1000000, 0],
	"Dallas": [Vector2(110, 166), 1000000, 1000000, 0],
	"New York": [Vector2(142, 156), 1000000, 1000000, 0],
	"Seattle": [Vector2(73, 143), 1000000, 500000, 0],
	"Vancouver": [Vector2(71, 139), 1000000, 500000, 0],
	"Anchorage": [Vector2(31, 113), 1000000, 500000, 0],
	"Detroit": [Vector2(131, 151), 1000000, 500000, 0],
	"Washington DC": [Vector2(140, 160), 1000000, 500000, 0],
	"Mexico City": [Vector2(108, 187), 1000000, 500000, 0],
	"Rio de Janeiro": [Vector2(187, 239), 1000000, 500000, 0],
	"London": [Vector2(251, 135), 1000000, 500000, 0],
	"Paris": [Vector2(254, 142), 1000000, 500000, 0],
	"Berlin": [Vector2(270, 134), 1000000, 500000, 0],
	"Rome": [Vector2(270, 153), 1000000, 500000, 0],
	"Seoul": [Vector2(437, 160), 1000000, 500000, 0],
	"Tokyo": [Vector2(456, 163), 1000000, 500000, 0],
	"Beijing": [Vector2(423, 156), 1000000, 500000, 0],
	"Taipei": [Vector2(430, 177), 1000000, 500000, 0]
}

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

@export var destination_cities: Array

Put a print statement inside the loop

Tried it. It does print the expected text. Also, I have “print(WorldMap.destination_cities)” and it shows an empty array, so I don’t see what the purpose of your comment is.

The purpose is to show you the basics of debugging your code.

Did you put it inside if distance <= 20: block?

I placed “print(“1”)” inside the loop statement and it does print. But the array still doesn’t get appended.

Let’s see the code and the exact printout.

You are aware that there’s no chance that append() magically just “doesn’t work”. It’s much more likely that your code execution doesn’t reach it the way you’re expecting it. Don’t print 1. Print key and distance before entering the if block, as well as inside the if block. So you can see the value of relevant variables each step of the loop.

Ah, ya see, telling me to “Print key and distance before entering the if block, as well as inside the if block.” at your first comment would’ve been much more productive then simply stating “Put a print statement inside the loop” since it’s a much more descriptive comment! The updated code is below:

	for key in WorldMap.active_cities:
		var distance = origin_vector.distance_to(map_to_local(WorldMap.cities[key][0]))
		print(key)
		print(distance)
		if distance <= 20:
			WorldMap.destination_cities.append(key) #<---Keys are not being added
			print(key)
			print(distance)
	WorldMap.destination_cities.erase(city_origin)
	print(WorldMap.destination_cities)

Output is the following:

Los Angeles
0.0
Los Angeles
0.0
San Fransisco
102.449989318848
San Diego
35.7770881652832
[]

So the “if” statement is imposing a distance limit that is too low. Will fix that. Thanks for the help!

1 Like

That’d minimize your involvement, which is bad for learning :wink:

Besides I didn’t know the nature of your bug. Debugging is an incremental process. You first attack it with one innocuous print. If that doesn’t give you a clue, you attack it with couple more prints. You escalate by adding prints until your code is full of them tracking every step of the way… until you’ve figured out where the problem is.

Here’s a useful hint. Since printing is essential for basic debugging, you can use format strings to neatly format the printout:

print("BEFORE IF: key = %s, distance = %f"%[key, distance]
if distance <= 20:
	print("INSIDE IF: key = %s, distance = %f"%[key, distance]
1 Like