Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | bluemage |
I’ve tried both node and control as the extends. Can anyone tell my why it seems file_exists doesn’t seem to be there at all.
extends Node
onready var file = File.new()
func load_data(url) -> Dictionary:
if url == null: return {}
if !file.file_exists(url): return {}
file.open(url, File.READ)
var data:Dictionary = {}
data = parse_json(file.get_as_text())
file.close()
return data
func write_data(url:String, dict:Dictionary):
if url == null: return
file.open(url, File.WRITE)
file.store_line(to_json(dict))
file.close()
more info… I was originally tryiing to pass in a .bin file as the url. I don’t know if that matters. The .bin file isn’t seen by godot’s filesystem. I’ve tried changing it to a .txt and a .dat nothing seems to take with 3.1. But the error seems to be with file not seeing file_exists anywhere not the filestring passed into it. Seems like if it was seeing file_exists it would just say false for not being able to see the file.
bluemage | 2019-06-16 03:07
working code
extends Control
#onready var file = File.new()
func load_data(url) -> Dictionary:
var file = File.new()
if url == null: return {}
if !file.file_exists(url): return {}
file.open(url, File.READ)
var data:Dictionary = {}
data = parse_json(file.get_as_text())
file.close()
return data
func write_data(url:String, dict:Dictionary):
var file = File.new()
if url == null: return
file.open(url, File.WRITE)
file.store_line(to_json(dict))
file.close()
bluemage | 2019-06-16 04:00