file:readLine makes my simulator crash

Hey everyone! I'm working on a delimited text file based dialogue system, so my plan for this was to utilize the file reading api. However, when I run the code below, the simulator crashes upon the first call to readline (right at local line = pdFile:readLine())

I'm unfortunately not getting any errors in the console or anything. I'm not sure if something is going wrong with my VS code extension but a lot of times if I call functions that don't don't exist (spelling mistakes) the simulator freezes up with no errors.

I am on a Macbook pro (apple silicon).

function playdate.loadDialogueFromCsv(csv) 

    local chunks = {}
    local path = "strings/" .. GLOBAL_LANGUAGE_CODE .. "/" .. csv
    local pdFile = playdate.file.open(path, playdate.file.kFileRead)

    if (pdFile == nil) then
        error("The game installation appears to be corrupt.  Please re-install.", 1)
    end

    -- load the first line and ignore it, as it is the csv header.
    local line = pdFile:readLine()

    repeat
        line = pdFile:readLine()

        local parts = string.split(line, "|")

        local chunk = {
            avatarId = parts[1],
            title = parts[2],
            text = parts[3]
        }

        table.insert(chunks, chunk)

    until line == nil

    pdFile:close()
    return chunks

end

The text file i'm reading looks like this:

avatar_id|title|text
test|Test man|Hello everyone,this is some test text
test|Test 2|This is more tesst text
test|Test 3|"*What!?* **this is more text text**

Things I have tried:

  • Tried CRLF / LF ending styles
  • Tried different delimiters
  • called playdate.file.exists(path) to confirm the file exists
  • using different file extensions

Thanks!

I think you've opened the file for writing when you did

playdate.file.open(path, playdate.file.kFileWrite)

Shouldn't this be kFileRead ?

Ah, i actually tried both as well, i just forgot to flip it back when i posted this. I"ll update the post, good catch :slight_smile:

ah, Stephen beat me to it. :slight_smile: To match the rest of the API we need to add a second return value with an error description when the first one is nil. Filing that now!

On observation I made is that the function readLine seems to be nil. Is that function name wrong?
Screenshot 2023-03-17 at 2.48.25 PM

it's lower case readline(), and Lua is case sensitive

ahhhh that's embarassing, that was totally it :expressionless: Now I'm not getting the crash anymore but the result from readline is nil.

Here's the updated code:

function playdate.loadDialogueFromCsv(csv) 

    local chunks = {}
    local path = "strings/" .. GLOBAL_LANGUAGE_CODE .. "/" .. csv
    local pdFile = playdate.file.open(path)

    if (pdFile == nil) then
        error("The game installation appears to be corrupt.  Please re-install.", 1)
    end

    -- load the first line and ignore it, as it is the csv header.
    local line = pdFile:readline()

    repeat
        line = pdFile:readline()

        if (line ~= nil) then
            
            local parts = string.split(line, "|")
            
            local chunk = {
                avatarId = parts[1],
                title = parts[2],
                text = parts[3]
            }
            
            table.insert(chunks, chunk)
        end

    until line == nil

    pdFile:close()
    return chunks

end

Okay I think i found the issue...sort of.

In my above code I had the dialogue text loaded with a subfolder for language code. So, for example, that would be strings/en/blah.txt. Interestingly enough the call to playdate.file.exists(path) returns true for this, and the returned playdate.file isn't nil... yet when i load the file and try to read the lines they're nil.

So I did some experimenting with the file structure and found that this only happens when the sub directory is 'en'. If i rename that folder to anything else it load the lines just fine. I apparently chose the forbidden name :slight_smile:

I'm not sure if this is a quirk in the build structure or a in the file reader thing, but just thought i'd toss this info out there in case anyone is confused (or dumb like me).

TL;DR don't add directories named as language codes...I guess?

EDIT: I was unable to replicate the behavior in an isolated codebase so idk anymore :frowning: