This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
The_King_JAZ's profile picture

LOTS of problems with fs API

Started by The_King_JAZ, 21 July 2014 - 06:07 AM
The_King_JAZ #1
Posted 21 July 2014 - 08:07 AM
So I am making a text-based adventure game, and I am trying to use the fs API to save information to separate files so the player can save/load games. I've done this before, and it has always worked. But for some reason, whenever I try to use it with this program, the program ends (without reporting an error), and I can't delete the file or the directory I tried saving to, but I can edit the file just fine.

If anyone can help, that would be great. If you need the program, you can find it here: http://pastebin.com/3xQTJjMV Everything works except the save function and the load function.

I am usually able to get programs to save to other files, so this is really confusing me.
hilburn #2
Posted 21 July 2014 - 08:36 AM
Ummm… interesting
The only thing that I can think of looking at it is that you have a fairly large number of details being loaded and saved, but the save function goes lines 267->305 = 38 variables, while the load function goes from 171 to 207 = 36 variables, this would lead to "room" probably being aligned with an incorrect variable so when you call if room=="room1" then, it returns false, and there is no "else" on that statement so the function ends without calling another function so the program ends. Not sure why the saveq is producing the same results, aybe try sticking a load of print("text")s in there to see exactly where it is breaking
I would suggest creating a table eg: game_state={player_name="hilburn", gender=male…quest1="Pick of Destiny"} which would let you save and load it in just one line of code, make adding and removing variables nice and easy, and prevent this kind of thing happening
I can't see anything that would prevent you deleting the files and directories, was that manually or through CC?
Edited on 21 July 2014 - 06:42 AM
Bomb Bloke #3
Posted 21 July 2014 - 10:05 AM
Generally, you can't delete files that are locked by an open handle. This means that if you open a file at some point then fail to close it, it'll stay "opened" until you find a way to kill the process maintaining the handle. In the case of locks held by ComputerCraft scripts, generally rebooting the ComputerCraft computer will be sufficient.

I notice that you attempt to load files from saveFiles/file_1, saveFiles/file_2, etc but attempt to save them to saveFiles/file1, saveFiles/file2, etc.

Stuff like this:

local playerName = ""
local playerGender = ""
local kingdomName = ""
local str = 0
local stam = 0
local level = 0
local xp = 0
local xpCap = 0
local armorHType = "N/A"
local armorCType = "N/A"
local armorLType = "N/A"
local armorFType = "N/A"
local armorH = 0
local armorC = 0
local armorL = 0
local armorF = 0

… can be entered as this:

local playerName,playerGender,kingdomName,str,stam,level,xp,xpCap,armorHType,armorCType,armorLType,armorFType,armorH,armorC,armorL,armorF = "","","",0,0,0,0,0,"N/A","N/A","N/A","N/A",0,0,0,0

Though table form is, as hilburn points out, even more convenient.
The_King_JAZ #4
Posted 21 July 2014 - 06:17 PM
I notice that you attempt to load files from saveFiles/file_1, saveFiles/file_2, etc but attempt to save them to saveFiles/file1, saveFiles/file2, etc.

Yeah, I stopped worrying about the load function, and thought that maybe it would be easier to get rid of the underscore, so I created a copy of the directories and used the names file1, file2 and file3 instead. I just forgot to change the load function.

I guess I'll have to look into using tables. I've never been good with tables, but I guess it's worth a shot. :lol:/>