Wiiplay123

Persistent Tables

Oct 4th, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.74 KB | None | 0 0
  1. local filePath = ".persistence"
  2. function store(sName, stuff)
  3.     local tempTable = {}
  4.     if fs.exists(filePath) then
  5.         local handle = fs.open(filePath, "r")
  6.         local stuff = handle.readAll()
  7.         handle.close()
  8.         tempTable = textutils.unserialize(stuff)
  9.     end
  10.     tempTable[sName] = stuff -- tempTable[sName]: "I'm stuff."
  11.     local handle = fs.open(filePath, "w")
  12.     handle.write(textutils.serialize(stuff))
  13.     handle.close()
  14. end
  15.  
  16. function pull(sName)
  17.     local tempTable = {}
  18.     if fs.exists(filePath) then
  19.         local handle = fs.open(filePath, "r")
  20.         local stuff = handle.readAll()
  21.         handle.close()
  22.         tempTable = textutils.unserialize(stuff)
  23.     end
  24.     return tempTable
  25. end
  26. persistent = {}
  27. setmetatable(persistent,{
  28. __index = pull, __newindex = store
  29. })
Advertisement
Add Comment
Please, Sign In to add comment