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?

in Engine by (92 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://.

by (3,298 points)
selected by

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 Frequently asked questions and 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 [email protected] with your username.