Array of strings (paths) to choose from to save and load data?

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

I want a single script to detect what scene it is attached to and choose a string (path) from variable array to use to save and load data. Here is what I have so far:

onready var newBest = 0.0
onready var best_file = ["user://best_1.txt", "user://best_2.txt", "user://best_3.txt"]

func _ready():
    if get_tree().current_scene.name == "TRACK_1":
        best_file[0]
    if get_tree().current_scene.name == "TRACK_2":
        best_file[1]
    if get_tree().current_scene.name == "TRACK_3":
        best_file[2]
    _load_best()

func _save_best():
    var file = File.new()
    file.open(best_file, File.WRITE)
    file.store_var(newBest)
    file.close()

func _load_best():
    var file = File.new()
    if file.file_exists(best_file):    #error points to this line
        file.open(best_file, File.READ)
        newBest = file.get_var()
        file.close()
    else:
        newBest = newBest

It returns error Invalid type in function 'file_exists' in base '_File'. Cannot convert argument 1 from Array to String.

When I make separate scripts for each scene with individual variable of path, then it works perfectly well. But I’d love to simplify it by having only one script attached to all scenes and let it detect what scene it is and give corresponding path to save and load accordingly. Any suggestions?

Thank you for your time!

:bust_in_silhouette: Reply From: Suleymanov

SOLVED! In case somebody is interested how, here it is:

var newBest = 0.0
var path = ["user://best_1.txt", "user://best_2.txt", "user://best_3.txt"]
var best_file = ""

func _ready():
    if get_tree().current_scene.name == "TRACK_1":
        best_file = str(path[0])
    if get_tree().current_scene.name == "TRACK_2":
        best_file = str(path[1])
    if get_tree().current_scene.name == "TRACK_3":
        best_file = str(path[2])
    _load_best()

Love GODOT!