Jump to content

Trouble creating/writing to file.


Netherwulf
 Share

Go to solution Solved by Netherwulf,

Recommended Posts

Greetings!

I'm hitting a wall trying to create a game state file that can be opened and read back. Here is my code.
 

filePath = FileSystem:GetAppDataPath() .. "gamestate.txt"


function Script:UpdatePhysics()
	
	if window:KeyHit(Key.S) then
		local stream = FileSystem:WriteFile(filePath) 
		if (stream==nil) then Debug:Error("Failed to write file.") end

		stream:WriteInt(GameData)
		stream:Release()
	end

end

When I run the program and press the key to create and save the file, the debug error is fired and the program ends. I can't tell if the problem lies in my code or somewhere else. 

Link to comment
Share on other sites

I worked on this and it runs ok in this code order:

DISPLAY was the variable I wanted to save written.

Quote

        local path = FileSystem:GetAppDataPath().."/MyFile.txt" 
        FileSystem:CreateFile(path) 
        local stream = FileSystem:OpenFile(path) 

        if (stream) then
                stream:Seek(0) 
                stream:WriteLine(DISPLAY) 
 --               stream:Seek(0)   -- debug
 --               System:Print(stream:ReadLine()) 
                stream:Release() 
        end
 

 

 

 

Link to comment
Share on other sites

7 hours ago, Josh said:

Your second code shows a "/" is inserted, and the first one does not.

The path being in the proper format was the first thing I looked into when I started having trouble. That doesn't seem to be the issue. 

Furthermore, I tried using the FileSystem:CreateFile() and FileSystem:OpenFile() commands that Marcousik suggested, but that doesn't work for me either. My local "stream" variable continues to return a nil value.  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...