Level Selection System - Comparing current scene filename with string in Array returns False

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lawric_

Hi, when working on my level selection system i was trying to make unvisited levels locked, for that i used a array where each item is array with the level path and a bool.

var level_list = [
["res://Scenes/levels/Level1.tscn", true],
["res://Scenes/levels/Level2.tscn", false],
["res://Scenes/levels/Level3.tscn", false],
["res://Scenes/levels/Level4.tscn", false],
["res://Scenes/levels/Level5.tscn", false],
["res://Scenes/levels/Level6.tscn", false],
["res://Scenes/levels/Level7.tscn", false],
["res://Scenes/levels/Level8.tscn", false],
["res://Scenes/levels/Level9.tscn", false],
["res://Scenes/levels/Level10.tscn", false],
["res://Scenes/levels/Level11.tscn", false],
["res://Scenes/levels/Level12.tscn", false],
["res://Scenes/levels/Level13.tscn", false],
["res://Scenes/levels/Level14.tscn", false],
["res://Scenes/levels/Level15.tscn", false],
["res://Scenes/levels/Level16.tscn", false],
["res://Scenes/levels/Level17.tscn", false],
["res://Scenes/levels/Level18.tscn", false],
["res://Scenes/levels/Level19.tscn", false],
["res://Scenes/levels/Level20.tscn", false],
]

When a scene is entered i run a function that checks if the current scene file path is in this list, if so, update the corresponding bool.

func update_level_list():
	var current_level_filepath = current_level.get_filename()
	for level in level_list:
		if current_level_filepath == level[0]:
			level[1] = true

For some reason that comparison returns false, even though when i print both values they are the same in the output window.

Unsure what current_level.get_filename() is here but it must differ from what is in the list in some way (or the list is incorrect).

The following works:

# consider populating the list auto-magically using a directory object
var level_list = [
    ["res://Scenes/levels/Level1.tscn", true],
    ["res://Test.tscn", false],
]

# scene name is 'res://Test.tscn'
var current_level_filepath = get_tree().current_scene.filename
for level in level_list:
    if current_level_filepath == level[0]:
        print("matched")

spaceyjase | 2022-01-07 09:44

:bust_in_silhouette: Reply From: Inces

Problem lies with iteration reference to original array. Using line like this :

level[1] = true

You only change the reference ( var level indtroduced during iteration ), while original array inside array(level_list) does not change. Iterating through arrrays is a bit pain, but it shhould look like this :

func update_level_list():
    var current_level_filepath = current_level.get_filename()
    for level in range(level_list.size()-1):
        if current_level_filepath == level_list[level][0]:
           level_list[level][1] = true

That makes perfect sense, thank you!

lawric_ | 2022-01-07 10:11