system.pathForFile question

the following code is supposed to do this:

check for folder named “save”
if not, make folder, restart
if so, check for .txt file named “lastPlayed.txt” inside of folder
if so, read it
if not, create the .txt file, store os.time() inside of it

the only trouble i am having is when it checks to see if the file exists. It says it does exist, but when trying to read it, it figures out there is no .txt file. I am confused.

function startUp()		
	local saveFolder = lfs.chdir(system.pathForFile("save", system.DocumentsDirectory))
	
	if saveFolder then
		print("found save")
		
		local lastPlayedtxtPath = system.pathForFile("save/lastPlayed.txt", system.DocumentsDirectory)
		
		if lastPlayedtxtPath then
			print("found lastPlayedtxt, reading...")	

			io.input(lastPlayedtxtPath) 
			
			print(io.read("*a"))
			io.input():close()	
			
		else
			print("no lastPlayedtxt")
			local lastPlayedtxtPath = system.pathForFile("save/lastPlayed.txt", system.DocumentsDirectory)
						
			local lastPlayedtxt, errorCode = io.open(lastPlayedtxtPath, "w+")
			
			if lastPlayedtxt then
				print("created new lastPlayedtxt")
				
				lastPlayedtxt:write(tostring(os.time()))
				
				io.close(lastPlayedtxt)
				print("restarting")
				startUp()
			else
				print("error: ".. errorCode)
			end
		end
	else
		print("no save")
		
		local docsPath = system.pathForFile("", system.DocumentsDirectory)
		local changeDir = lfs.chdir(docsPath)
		
		local saveFolderPath
		local saveFolderName = "save"
		
		if changeDir then
			lfs.mkdir(saveFolderName)
			saveFolderPath = lfs.currentdir().."/"..saveFolderName
		end
		
		print("made save, restarting...")
		startUp()
	end
end

now, when run when the there is no save file, the console says this:

no save
made save, restarting…
found save
found lastPlayedtxt, reading…
ERROR: Runtime error

error is: Documents\save/lastPlayed.txt: No such file or directory

obviously there will be no .txt file if there was no save folder to begin with, so why does system.pathForFile return true?

If you could share a sample project I may be able to help better but…

Is it possible that it creates the file at save/lastPlayed.txt but you are looking at it under Documents\save\lastPlayed.txt?

or could be related to using forward slash in the file path?

Well, if i change the slash it comes up as Documents\savelastPlayed.txt: No such file or directory
as if there was no slash.

I tried

local lastPlayedtxtPath = lfs.chdir(system.pathForFile("save/lastPlayed.txt", system.DocumentsDirectory))

which goes into an infinite loop where it says it cant find the file then creates it, restarts, and does the same thing (crashes). However, now the file is there, when i change it back to not using lua file system, it reads the file just fine.

I am so confused.

I think I am using system.pathForFile wrong or something. It is returning true no matter what?

nevermind, I fixed it. You have to use io.open() to check if the file is there.

1 Like

SSK2 comes with a complete files library that works in the sandbox and outside of it (assuming you have permissions):
https://roaminggamer.github.io/RGDocs/pages/SSK2/

However, there is a way easier way to do what you want, also provided by SSK2:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/persist/

ssk.persist.setDefault( "lastPlayed.json", 'playtime', os.time() )

local playtime = ssk.persist.get( "lastPlayed.json", 'playtime' )

ssk.persist.set( "lastPlayed.json", 'playtime', os.time() )

Run 1
‘playtime’ will contain the current os.time().

Run 2 … N
‘playtime’ will contain the os.time() of the prior run.

I also provide (in SSK) a table save/load extension if you want to use that:
https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/#saving-loading-tables