Single save/load script fails to work for all scenes per its name.

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

func _process(delta):
    if get_tree().current_scene.name == "TRACK_1":
        best_file = "user://best_1.txt" 
    if get_tree().current_scene.name == "TRACK_2":
        best_file = "user://best_2.txt" 
    #and so on for all 10 tracks...

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):
        file.open(best_file, File.READ)
        newBest = file.get_var()
        file.close()
else:
    newBest = newBest

Basically, I want to use a single script for all the scenes and let it find out what scene it is and pick path accordingly. I thought the above written architecture would do the job, but it returns the following error: Invalid type in function "file_exists" in base "_File". Cannot convert argument 1 from Nil to String.

Though when I make separate scripts with corresponding var best_file = "user://best_n.txt", one for each, it works perfectly.

Is there any way to make one script work in all scenes? It should check what scene it is and give the corresponding path.

Thank you for your time!

:bust_in_silhouette: Reply From: Inces

It looks like You want to use export variables here.

What happens in your script is that best_file is nulkl at the start and it becomes string in process(). You didn’t show it, but You call load function before process() can run, so an error occures, that File with path null can’t be accessed.

when You introduce new variable You can do it like this :

export var newBest : String

Thanks to this, You can manually type desired path for each copy of this scene in editor, without making separate scripts for those scenes. One script, different variables. This way newBest will not be null on init() and You can refer to this variable anytime without null error.

Is this it, or did I take it too simply ?

Thank you for the reply! That’s how I ended up doing it:

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()

As you mentioned I set the variable to be string by adding = "" and also I added an array of strings (paths) to choose from and converted them to str. Works like a charm!

Suleymanov | 2021-04-10 21:03

I see
If You don’t want to do so many repetitions in code You can do something like this :

func ready():
       best_file = "user://best_" + get_tree().current_scene.name.right(0) + ".txt"

This line will find number in your current scene name and build a path using this number.

Inces | 2021-04-10 21:10

Hey, that’s a very smart and short way of doing it! I’ll try that. Thanks!

Suleymanov | 2021-04-10 21:20

I used your short method and it worked well, just wanted to thank you again! The only part I changed was ...name.right(6), instead of zero (it happened to crop from left to right). But the idea itself and its simplicity is 5 star!! That eliminated var path and the rows of unnecessary ifs. Hats off!

Suleymanov | 2021-04-11 11:39