ksbd

Testing Saving Variables

Feb 24th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.86 KB | None | 0 0
  1. --This is a test
  2.  
  3. --Variables:
  4.  
  5. var1 = 0
  6. var2 = 0
  7.  
  8. varStorage = {}
  9.  
  10. --Functions:
  11.  
  12. function storeVariables()
  13.     varStorage.var1 = var1
  14.     varStorage.var2 = var2
  15.     --The above stores the variables in the table
  16.    
  17.     local output = textutils.serialize(varStorage)
  18.     --This converts the table to a string
  19.  
  20.     local handle = assert(fs.open("vars", "w"), "Couldn't save vars")
  21.     handle.write(output)
  22.     handle.close()
  23. end
  24.  
  25. function getVariables()
  26.     local handle = assert(fs.open("vars", "r"), "Couldn't load vars")
  27.     local input = handle.readAll()
  28.     handle.close()
  29.  
  30.     local destination = textutils.unserialize(input)
  31.     var1 = destination.var1
  32.     var2 = destination.var2
  33. end
  34.  
  35. --Main program:
  36.  
  37.  
  38. getVariables()
  39. print(var1) print(var2)
  40.  
  41. term.write("Enter var1: ") var1 = io.read()
  42. term.write("Enter var2: ") var2 = io.read()
  43. storeVariables()
  44. print(var1) print(var2)
Advertisement
Add Comment
Please, Sign In to add comment