Page 1 of 1

[Solved] Errors regarding file io despite working with VS Code

Posted: Tue Jun 23, 2020 3:27 pm
by KoalaJohnson
I am receiving errors from love2D when drag and dropping my folder containing my project onto the program. It is about file io and closing an opened file for reading. When I run my project in Visual Studio Code it runs fine, but when going the more traditional way with the love2D application directly it gives me this error: [code]Error main.lua:34: bad argument #1 to 'close' (FILE* expected, got nil)

Traceback

[C]: in function 'close'
main.lua:34: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
[/code]

In my main.lua file I have these lines of code:

Code: Select all

fileHighScore = io.open("score.txt", "r")
io.input(fileHighScore) 
and then within love.load I wrote this:

Code: Select all

scoreString = io.read()
highscore = tonumber(scoreString)
io.close(fileHighScore) 
I do not understand why the fileHighscore is returning nil when ran directly by love2D but not when ran through VS Code

I included my project zipped up into a .love file:

Re: Errors regarding file io despite working with VS Code

Posted: Wed Jun 24, 2020 2:10 am
by ReFreezed
io.open() depends on the current working directory which is probably not what you want. You should be using the functions provided by love.filesystem.

Re: Errors regarding file io despite working with VS Code

Posted: Wed Jun 24, 2020 7:02 am
by zorg
And you can test the above post's theory with love.filesystem.getWorkingDirectory.

Re: Errors regarding file io despite working with VS Code

Posted: Wed Jun 24, 2020 6:03 pm
by KoalaJohnson
Thank you for this, I am fairly new to love2D. I will see into love.filesystem.