File doesn't exist paradox-why is it pulling the default value?

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

So I’ve been working on implementing a leaderboard (silentwolf) using a simple Global LineEdit scene. With my current set up, when the player enters their name and runs a race, it still enters the default value that exists in the “if not file.fileexists” section of my load_PlayerName. This is true for the first race and any race beyond…but once you close out the window and open it again, it begins displaying the locally stored value on the leaderboard. This shows things are working on the back end, but it would seem that no matter what I do when I open up the game as if for the first time, it won’t recognize my saved text until after I close out.

I have to use func _ready to load_PlayerName() otherwise when you open a new game you’re prompted to enter a new name despite having saved data that should be used instead. With this current setup, a saved script means you’ll bypass the name entry screen immediately. I’ve tried setting it up with a signal but then suddenly my name becomes invalid on the backend.

extends LineEdit

const filepath = "user://PlayerName.data"
var PlayerName = get_text()

func _ready():
    load_PlayerName()
    pass

func load_PlayerName():
    var file = File.new()
    file.open(filepath, File.READ)
    PlayerName = file.get_var()
    file.close()
    if not file.file_exists(filepath):
	    PlayerName = "error"
	    return
    if file.file_exists(filepath):
	    PlayerName = get_text()
	    GlobalLineEdit.visible = false
	    get_tree().change_scene("res://Scenes_GUI/Difficulty.tscn")
    pass

func save_PlayerName():
    var file = File.new()
    file.open(filepath, File.WRITE)
    file.store_var(PlayerName)
    file.close()
    pass

func _on_LineEdit_text_entered(new_text):
    PlayerName = get_text()
    save_PlayerName()
    GlobalLineEdit.visible = false
    get_tree().change_scene("res://Scenes_GUI/Difficulty.tscn")

Any thoughts on how to get around this? I’ve tried everything I can think of but just can’t get it to behave the way it’s intended to.

I don’t know if you know this but you don’t need to have pass at the end of every function, What pass does is it skips that line without calling errors. That’s why the default script has it because since theirs no code after func _example it would throw an error, bur pass tells it to ignore it.

Also try having the code that checks if the file exists, run before it tries to open the file.

So this

func load_PlayerName():
var file = File.new()
file.open(filepath, File.READ)
PlayerName = file.get_var()
file.close()
if not file.file_exists(filepath):
PlayerName = “error”
return
if file.file_exists(filepath):
PlayerName = get_text()
GlobalLineEdit.visible = false
get_tree().change_scene(“res://Scenes_GUI/Difficulty.tscn”)
pass

Would be this

func load_PlayerName():
var file = File.new()

if not file.file_exists(filepath):
    PlayerName = "error"
else:
    file.open(filepath, File.READ)
    PlayerName = file.get_var()
    file.close()
    GlobalLineEdit.visible = false
    get_tree().change_scene("res://Scenes_GUI/Difficulty.tscn")

Their shouldn’t be any problems with that, assuming thats the part that was breaking.

Merlin1846 | 2020-11-17 19:19