19. JSON, configuration and file operations
func run() -> Array:
var text: String = JSON.stringify({"music": 0.5, "name": "Ada"})
var parser := JSON.new()
if parser.parse(text) != OK:
return []
var data: Dictionary = parser.data
return [data["music"], data["name"]]JSON.parse_string is convenient; JSON.new().parse gives an error code and diagnostics. JSON does not preserve every Godot value type. Encode vectors as arrays or dictionaries, and reconstruct them explicitly when loading.
func run() -> float:
var configuration := ConfigFile.new()
configuration.set_value("audio", "music", 0.5)
var saved: Error = configuration.save("user://gdscript_tutorial.cfg")
if saved != OK:
return -1.0
var loaded := ConfigFile.new()
if loaded.load("user://gdscript_tutorial.cfg") != OK:
return -1.0
return float(loaded.get_value("audio", "music", 0.7))DirAccess can create directories, list entries, rename and remove files. Check Error results. Perform test edits inside your own user-data tutorial folder. Deleting a custom note file is different from deleting a PCA save slot; use the save-widget/save-system route for game saves.
For an imported image, model or other engine resource, ResourceLoader is usually the correct export-aware reader. An original source file may be converted during export and unavailable as plain bytes.
