0 votes

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!

Godot version 3.2.3
in Engine by (322 points)

1 Answer

0 votes
Best answer

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!

by (322 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.