+1 vote

My game runs perfectly fine when run in the editor, but the exported version can't read the JSON file.

The relevant code:

func load_dict(name):
  var dict := {}
  var fp = "res://maps/%s.json" % name
  dir_contents("res://maps")
  var file = File.new()
  if not file.file_exists(fp):
    print("File not found: ", fp )
  else:
    print('File found: ', fp)
  file.open(fp, file.READ)
  var text = file.get_as_text()
  dict = parse_json(text)
  file.close()

      return dict

func dir_contents(path):
    var dir = Directory.new()
    if dir.open(path) == OK:
        dir.list_dir_begin()
        var file_name = dir.get_next()
        while file_name != "":
            if dir.current_is_dir():
                print("Found directory: " + file_name)
            else:
                print("Found file: " + file_name)
            file_name = dir.get_next()
    else:
        print("An error occurred when trying to access the path.")

export settings

Export filters:

*.json

error message

Found file: phandalin.json.import
File not found: res://maps/phandalin.json
ERROR: File must be opened before use.
   At: core/bind/core_bind.cpp:2126
ERROR: call: Error parsing JSON at line 0:
   At: modules/gdscript/gdscript_functions.cpp:1268
SCRIPT ERROR: load_dict: Trying to assign value of type 'Nil' to a variable of type 'Dictionary'.
          At: res://scripts/world.gdc:175

No such errors appear when running the game from the editor.

The function dir_contents is for debugging purposes and isn't used anywhere else.

The line res://scripts/world.gdc:175 refers to dict = parse_json(text)

Any help would be very appreciated. Thank you in advance!

Godot version v3.3.2.stable.official
in Engine by (13 points)

I'm especially confused that the .json.import is exported but the original .json is not.

I believe you have to read res://maps/%s.json.import (I had the same problem once).
I'm not sure I understand why though......

Edit: you have to use the ResourceLoader, see the relevant doc (litterally the first part of the page)!

Please log in or register to answer this question.

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.