Jump to content




load array element with loadstring


  • You cannot reply to this topic
2 replies to this topic

#1 unobtanium

  • Members
  • 505 posts

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 by UNOBTANIUM, 24 February 2014 - 07:53 PM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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

#3 unobtanium

  • Members
  • 505 posts

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 by UNOBTANIUM, 24 February 2014 - 08:10 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users