Posted 20 October 2016 - 02:10 AM
Save function: http://pastebin.com/mdud8uTW
Whole program: http://pastebin.com/73wJD4ra
I'm trying to make a function that has the program rewrite itself to save information, in this case I'm saving it to a separate file for testing purposes. I've made something like this before that worked perfectly fine, but now it's being weird. Here's the rundown of what happens:
Edit: This is what the save function originally looked like:
local file = fs.open("speedrun", "r")
local content = file.readAll()
file.close()
local a = string.find(content, "local listStart")
content = string.sub(content, a)
local file = fs.open("testfile", "w")
file.writeLine("local game = "..textutils.serialize(game))
file.write(content)
file.close()
end
– The load function did not exist at this point, nor was it necessary for anything other than testing.I should also point out that I don't get an error. The program functions perfectly fine. It just doesn't write to files properly. It's exactly what I explained above, no more, no less.
Whole program: http://pastebin.com/73wJD4ra
I'm trying to make a function that has the program rewrite itself to save information, in this case I'm saving it to a separate file for testing purposes. I've made something like this before that worked perfectly fine, but now it's being weird. Here's the rundown of what happens:
- The programs reads its own code which is kept as a string in the 'content' variable.
- It deletes the information about the 'game' variable from 'content' in order to write new information about it.
- It writes the new data from the 'game' table, which takes 6 lines if it's empty.
- Then it's supposed to write the remaining code from 'content'.
- What actually happens is it deletes all the contents in the 'testfile' when it writes the 24th or 25th line (depending on how many lines it wrote from 'game'), including what it already wrote to the file.
- I've confirmed that the information in 'content' exists, it just deletes everything in the file after it writes a certain number of lines.
- All the commented out stuff is just from me trying to figure this out.
- This glitch doesn't seem to happen if I have it write 'game' by itself. This is after testing it with 30 items in 'game.name' (it automatically writes one line per item), but for some reason it doesn't want to work with the rest of it.
Edit: This is what the save function originally looked like:
Spoiler
function saveData()local file = fs.open("speedrun", "r")
local content = file.readAll()
file.close()
local a = string.find(content, "local listStart")
content = string.sub(content, a)
local file = fs.open("testfile", "w")
file.writeLine("local game = "..textutils.serialize(game))
file.write(content)
file.close()
end
– The load function did not exist at this point, nor was it necessary for anything other than testing.
Edited on 20 October 2016 - 02:54 AM