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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
KoalaJohnson
Prole
Posts: 2
Joined: Tue Jun 23, 2020 3:14 pm

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

Post 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:
Attachments
Asteroids in Lua.love
(140.49 KiB) Downloaded 99 times
Last edited by KoalaJohnson on Wed Jun 24, 2020 6:03 pm, edited 1 time in total.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Errors regarding file io despite working with VS Code

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Errors regarding file io despite working with VS Code

Post by zorg »

And you can test the above post's theory with love.filesystem.getWorkingDirectory.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
KoalaJohnson
Prole
Posts: 2
Joined: Tue Jun 23, 2020 3:14 pm

Re: Errors regarding file io despite working with VS Code

Post by KoalaJohnson »

Thank you for this, I am fairly new to love2D. I will see into love.filesystem.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 136 guests