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

load array element with loadstring

Started by unobtanium, 24 February 2014 - 06:52 PM
unobtanium #1
Posted 24 February 2014 - 07:52 PM
Hello,

i am gambling again and tried to make my save file as nice and easy as possible, but it wont work :(/>

I am building strings like these:

site[1][2] ={"graphPillar",1,1,100,40,0,1337,1,1,false,"NONE","NONE",{},{}}

And save these into a file and load them later on with:


local site = {}
for i=1, 198 do
site[i] = {}
end
-- necessary??

function load()
  if not fs.exists("openPeripheralClient") then return end
  local file = fs.open("openPeripheralClient","r")
	 while true do
	  local line = file.readLine()
	  if line == "END" then
		break
	  else
		assert(loadstring(tostring(line))) -- HERE SHOULD THE MAGIC HAPPEN
	  end
	end
  file.close()
end

This doesnt throw a error (at least) but it doesn't create the element like shown above.
I know its not the best solution (or maybe it doesnt even work), but i dont go for security or anything else.

Thank you
unobtanium
Edited on 24 February 2014 - 06:53 PM
theoriginalbit #2
Posted 24 February 2014 - 07:56 PM
okay so if you're making use of Tables, and wanting to save the Table as a String and then load it back from a string, why don't you just use textutils.serialize and textutils.unserialize
unobtanium #3
Posted 24 February 2014 - 07:59 PM
Can i go like textutils.serialize(site) and take the whole thingy into the file and load it with
site = textutils.unserialize(file.readLine))
?

edit: Okay it does… i feel dumb now

Thanks for your help^^
Edited on 24 February 2014 - 07:10 PM