extends Control
var path = "res://data.json"
func readJSON(jsonFilePath):
var file = FileAccess.open(jsonFilePath, FileAccess.READ)
var content = file.get_as_text()
var finish = JSON.parse_string(content)
return finish
func _ready():
print(readJSON(path))
When I first ran it - the function returned a null value as there were invalid characters in the json file, being the quote marks - not sure if those quote marks are present in your json file, or what, but replacing them with regular quotes seemed to fix the issue for me
The only issue I found in your above code was trying to access FD_Stars in your Node1 script. The variable is local to the _ready function in the ChartLoader autoload, so it is only ever present in the ready function.
I changed it to be defined globally in the autoload script (at the top of the script), and the logic from Node1 is working and successfully retrieving and printing the information out (see end of post)
ChartLoader.gd
extends Node2D
var bpm: float
var SongScrollSpeed: float
var SongChart: Array
var FD_Star
func readJSON(json_file_path):
var file = FileAccess.open(json_file_path, FileAccess.READ)
var content = file.get_as_text()
var json = JSON.new()
var finish = json.parse_string(content)
return finish
func _ready() -> void:
# Replaced your local declaration of `FD_Stars` with a global one `FD_Star`
# so that it can be accessed by the Node1 script
FD_Star = readJSON("res://Assets/FNFFD-Stars/FD-ChartL1.json")
Node1.gd
extends Node2D
var bpm: float
var SongScrollSpeed: float
var SongChart: Array
func _ready() -> void:
# Editing post: just to add, you can reference any autoload scripts
# by referring to the node name - in this case "ChartLoader", rather
# than getting a node reference to it using the "root/ChartLoader" path
var jsonfile = ChartLoader.FD_Star
bpm = jsonfile["bpm"]
SongScrollSpeed = jsonfile["SongScrollSpeed"]
SongChart = jsonfile["SongChart"]
print(jsonfile)
print(bpm, SongScrollSpeed, SongChart)
I really can’t think of anything else that would be going wrong, it’s working for me. Are you sure you’re running the right scene and printing the file contents before checking the output?
I think your SongChart array doesn’t have any elements in, try printing SongChart out on the line before and see if it has things in, then we can go from there
"func _ready() → void:
# Editing post: just to add, you can reference any autoload scripts
# by referring to the node name - in this case “ChartLoader”, rather
# than getting a node reference to it using the “root/ChartLoader” path
var jsonfile = ChartLoader.FD_Star
bpm = jsonfile[“bpm”]
SongScrollSpeed = jsonfile[“SongScrollSpeed”]
SongChart = jsonfile[“SongChart”]
Vulkan: Did not create swapchain successfully. Error code: VK_ERROR_SURFACE_LOST_KHR
Godot Engine v4.2.1.stable.official.b09f793f5 - https://godotengine.org
Vulkan API 1.1.117 - Forward Mobile - Using Vulkan Device #0: ARM - Mali-G57