0 votes

The problem:
I autoload a script (globals.gd) to access global variables in my scenes. Inside this script I have a set of commands for opening a file (save.sav). However, accessing the file prints null, whereas doing the same thing from a scene returns the file contents:

extends Node

func _ready():
    file = File.new()
    if file.open("res://save.save", File.READ_WRITE) != 0:
        print("Error opening file")
        print("opened file")

    var d = file.get_var()

What am I missing?

asked Mar 30, 2019 in Engine by marcorexo (85 points)

1 Answer

+1 vote
Best answer

I can say for sure that opening a file read/write from res:// will fail on some platforms and installations.

Try: file.open("res://somefilename.txt, file.READ)
And save files only in user://

The reason: res:// is for program resources and is often located in locations where the operating system does not allow writing.

It is better to discover sooner than later that you should not write to res:// unless you code some plugin/addon which is used from within the godot IDE during development.

I can attest that godot allows writing to user:// also from singletons because I personally use functions in my global.gd to write savegames to user://.

answered Mar 30, 2019 by wombatstampede (3,193 points)
selected Mar 30, 2019 by marcorexo

Thanks wombatstampede - appreciate your time

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.