Topic was automatically imported from the old Question2Answer platform.
Asked By
TJ20201
I’m creating a mod system for a game, and so far it’s worked until I tried to get all mods in the user:// directory. I can’t preload it or anything as part of the path URL to get to it is unknown until checking.
while true:
var file = dirhandle.get_next()
if file == "":
break
elif not file.begins_with("."):
var f = File.new()
var mil = "user://mods/"+file+"/info.txt"
if f.file_exists(mil):
var inforaw = f.open(mil, File.READ).get_as_text()
var modinfo = {}
for line in str(inforaw).split("\n"):
for item in line.split("="):
modinfo[item[0]] = item[1]
mods[file] = modinfo
f.close()
What this should do is get the file’s name and desc content items, but instead the inforaw is read as 0.
var inforaw = f.open(mil, File.READ).get_as_text()
The Fileopen() command returns an Error object. In this case, it was probably the message OK, which equals 0.
Oh, what should I use instead to get the raw text then?
TJ20201 | 2022-01-24 17:52
Try rewriting it like this:
var f = File.new()
var mil = "user://mods/"+file+"/info.txt"
if f.file_exists(mil):
var result = f.open(mil, File.READ)
if result != OK:
push_error("Couldn't open file.")
return
var inforaw = f.get_as_text()
var modinfo = {}
for line in str(inforaw).split("\n"):
for item in line.split("="):
modinfo[item[0]] = item[1]
mods[file] = modinfo
f.close()
Ertain | 2022-01-24 18:12
Alright, I’ll test that.
TJ20201 | 2022-01-24 18:13
The file reading works, but the info doesn’t.
It returns
modName:{A: , d:e, n:a}
When it should return
modName:{name:A mod, desc:A mod for testing mod menu}
(this is what it looks like in print()
TJ20201 | 2022-01-24 18:19
I think this code will work:
# ...
var modinfo = {}
for line in str(inforaw).split("\n"):
var lines: PoolStringArray = line.split("=")
modinfo[lines[0]] = lines[1]
# ...