[SOLVED] os.rename function causing crash

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
DGM
Prole
Posts: 14
Joined: Tue Mar 20, 2018 6:42 am

[SOLVED] os.rename function causing crash

Post by DGM » Thu Sep 27, 2018 1:06 pm

I want to write a utility that creates some files and directories but places them into the same folder as the program rather than into the save directory. After some research I figured the easy way to do this was to create what I wanted in the save folder and then call os.rename to move it where I needed it. But the program I wrote to test the process isn't working.

Here's my code:

Code: Select all

require("os")

function love.load()
	fileName = "Test.txt"
	saveDir = love.filesystem.getSaveDirectory( )
	sourceDir = love.filesystem.getSourceBaseDirectory( )
	oldPath = saveDir.."/"..fileName
	newPath = sourceDir.."/"..fileName
end

function love.draw()
	love.graphics.print(oldPath, 100, 100)
	love.graphics.print(newPath, 100, 200)	
end

os.rename(oldPath, newPath)
When I comment out the os.rename line at the end I get the expected result (the source and destination paths are correctly shown). But the os.rename line itself gives the following error:

Code: Select all

Error

main.lua:17: bad argument #1 to 'rename' (string expected, got nil)


Traceback

[C]: in function 'rename'
main.lua:17: in main chunk
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
Before you ask: yes, there is a Test.txt file waiting in the correct place.

Any idea what I'm doing wrong?
Last edited by DGM on Thu Sep 27, 2018 2:38 pm, edited 1 time in total.

grump
Party member
Posts: 576
Joined: Sat Jul 22, 2017 7:43 pm

Re: os.rename function causing crash

Post by grump » Thu Sep 27, 2018 1:53 pm

You're using oldPath and newPath before anything has been assigned to them. LÖVE will read your main.lua file, execute top level code, and only after that love.load will be called.

DGM
Prole
Posts: 14
Joined: Tue Mar 20, 2018 6:42 am

[SOLVED] os.rename function causing crash

Post by DGM » Thu Sep 27, 2018 2:37 pm

[smacks forehead]

Right. Moving the string declarations out of the load function fixed it. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests