Jump to content




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


4 replies to this topic

#1 JustIan12

  • Members
  • 57 posts

Posted 03 September 2017 - 06: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 by JustIan122, 03 September 2017 - 06:44 PM.


#2 Lupus590

  • Members
  • 2,027 posts
  • LocationUK

Posted 03 September 2017 - 07:23 PM

View PostJustIan122, on 03 September 2017 - 06:42 PM, said:

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 by Lupus590, 03 September 2017 - 07:28 PM.


#3 JustIan12

  • Members
  • 57 posts

Posted 03 September 2017 - 11:36 PM

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

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 04 September 2017 - 12: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 by KingofGamesYami, 04 September 2017 - 12:57 AM.


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 04 September 2017 - 03: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.

View PostJustIan122, on 03 September 2017 - 11:36 PM, said:

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")






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users