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

[Save & Loading] Ask A Pro [Season 982]

Started by JustIan12, 03 September 2017 - 05:42 PM
JustIan12 #1
Posted 03 September 2017 - 07:42 PM
So, I am having issues when loading the current values of a table into the 'RAM' of the computer I currently assume that a table inside a program cannot be edited and saved without loading it externally so, I set it so that there are two functions and by default the program has an empty white list.

whitelist = {}
function save(whitelist,WL)
local file = fs.open("WL","w")
file.write(textutils.serialize(whitelist))
file.close()
end

function load(WL)
local file = fs.open("WL","r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

These two functions are to save and load any changes to the white list within the program. to an external program called WL I figured that after I saved a name to a white list it would have no effect as I did not update or load the permanent white list into the white list "RAM" and therefore save another empty file.

term.clear()
term.setCursorPos(2,2)
  write("Insert Player IGN > ")

local IGN = read()

table.insert(whitelist,IGN)

save(whitelist,WL)

term.clear()
term.setCursorPos(2,2)
write(IGN.." has been whitelisted.")
sleep(2)

term.clear()

for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.3)
end

So what I am asking is how do I use the load function to load the serialised table from the external file into the whitelist variable within the program to create a working table add and remove system.
Edited on 03 September 2017 - 05:44 PM
Lupus590 #2
Posted 03 September 2017 - 08:23 PM
whitelist = {}
function save(whitelist,WL)
local file = fs.open("WL","w")
file.write(textutils.serialize(whitelist))
file.close()
end

function load(WL)
local file = fs.open("WL","r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

This code will work but could be improved. You have an unused variable, I have corrected it below with the original line as a comment.

whitelist = {}
function save(whitelist,WL)
local file = fs.open(WL,"w") --# local file = fs.open("WL","w")
file.write(textutils.serialize(whitelist))
file.close()
end

function load(WL)
local file = fs.open(WL,"r") --# local file = fs.open("WL","r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

The chage will allow you to specify the file path when you call your save and load functions. Also you may want to make the functions local, I have not done this.

I will be looking at the rest of your post in a moment.

Answering you actual question, using your load function (or my edited version) you would do this.

whitelist = load(filePath)
Edited on 03 September 2017 - 06:28 PM
JustIan12 #3
Posted 04 September 2017 - 12:36 AM
Lupus, the variables are not unused the API requires it to be in string format. I did make the functions local, I also changed one of the functions which had the same name as the API load() to pull() and finally your answer does not work XD because it does not unserialise the file before reassigning it to the table. I could also be wrong but I get the error when I run it with the load command

Please help again lol XD

- Ian

-- Whitelist
whitelist = {}
whitelist = load("WL")
-- TestSaveFunction

local function save(whitelist,WL)
local file = fs.open("WL","w")
file.write(textutils.serialize(whitelist))
file.close()
end

-- TestLoadFunction
local function pull(WL)
local file = fs.open("WL","r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
-- Add Player Code
term.clear()
term.setCursorPos(2,2)
write("Insert Player IGN > ")
local IGN = read()

table.insert(whitelist,IGN)

save(whitelist,WL)

term.clear()
term.setCursorPos(2,2)
write(IGN.." has been whitelisted.")
sleep(2)

term.clear()

for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.3)
end

test:35: bad arguement: table expected, got nil

PS: the file with the table in WL exists aswell
KingofGamesYami #4
Posted 04 September 2017 - 01:54 AM
On line 3, you call the 'load' function which is a built-in function which prepares a string containing a program for execution. The function returns nil, as "WL" is not valid code. You assign whitelist to this value, ending up with a nil value error on line 25, where you call table.insert.
Edited on 03 September 2017 - 11:57 PM
Bomb Bloke #5
Posted 04 September 2017 - 04:38 AM
That is to say: you neglected to actually include the code that defines your own "load" function, so calling "load" calls Lua's default version (which is for something completely different to what you're wanting to do here).

You do define a similar function called "pull", but you do so after the point where you'd want to call it, and you never actually call it using that name anyway - you're calling "load" instead.

Lupus, the variables are not unused the API requires it to be in string format.

On this point - you don't specifically have to hard-code a string (eg "WL") into your fs.open() parameter list. If you define a variable (eg WL) and assign a string to that, then said variable can be used instead.

Eg, this:

local WL = "WL"
local file = fs.open(WL, "w")

… gets you the same effect as this:

local file = fs.open("WL", "w")