0 votes

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.

info.txt:

name=A mod.
desc=A mod for testing mod menu.
Godot version Godot_v3.4.2-stable_win64.exe, the project is GLES2.
in Engine by (12 points)

1 Answer

0 votes

I think the problem is with this line:

var inforaw = f.open(mil, File.READ).get_as_text()

The File open() command returns an Error object. In this case, it was probably the message OK, which equals 0.

by (2,394 points)

Oh, what should I use instead to get the raw text then?

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()

Alright, I'll test that.

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()

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]
# ...
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.