Hey guys. So I've heard you can save variables to a separate location in order to prevent you loosing them when shutting down minecraft. I've been wanting to learn how to do this for some time as most the programs I've written are at least 3/4 dedicated to trying to figure out their last task.
I'm about to try to write a chunk-loading program that'll need 2 variables to be saved; F and lastTask. I'd want a function something along the lines of writeF and getF, and ditto for lastTask. One function would be to write the variable to an existing file, or creating the file if necessary, and the other would pull the info and set it back as a variable within the main program. Could someone give me an example of how this would be done? I've found snippets of info on the web, but not enough to really comprehend how to do this. (I'm not a programmer.)
Also, if anyone has time, could you explain to me how to save all the variables I need to 1 location rather than separate ones for separate variables, and then to read them and set them as a variable within my main program again?
Thanks in advance!
5 replies to this topic
#1
Posted 24 February 2013 - 10:25 PM
#2
Posted 24 February 2013 - 10:29 PM
#3
Posted 24 February 2013 - 10:35 PM
Saving the variables function
Loading the variables and setting them
EDIT -- Ninja'd
local function fwrite()
local file = assert(io.open("yoursavedfilename", "w"))
file:write(F.."\n"..lastTask)
file:close()
end
Loading the variables and setting them
local function getTable(path)
if fs.exists(path) then
local file = io.open(path, "r")
local lines = {}
local i = 1
local line = file:read("*l")
while line ~= nil do
lines[i] = line
line = file:read("*l")
i = i + 1
end
file:close()
return lines
end
return {}
end
local storedVariables = getTable("yoursavedfilename")
local F = storedVariables[1]
local lastTask = storedVariables[2]
EDIT -- Ninja'd
Edited by NDFJay, 24 February 2013 - 10:35 PM.
#4
Posted 24 February 2013 - 11:13 PM
Thank you, I appreciate it. I'm still a little unclear though. This is what I've got so far from this:
I'm probably missing some key basic knowledge or something.
The part I'm confused by is:
Could you sum up what is going on please? (I know, baby steps here. I'm sorry.)
local function getTable(path) --Creates the funcion getTable, sets an argument for path
if fs.exists(path) then --Checks if there's an existing file
local file = io.open(path, "r") --Opens the file to be read
local lines = {} -- creates the table lines
local i = 1 --sets the variable i to 1
local line = file:read("*l") -- creates the varaible line. Not sure what it does. What is the ("*1") for?
while line ~= nil do -- while variable line isn't nil do
lines[i] = line --Not entirely sure what this is doing.
line = file:read("*l") --Again, I'm lost on this one.
i = i + 1 -- i+1
end --ends the loop
file:close() --closes the file
return lines --returns a boolean if lines has data in it?
end --ends the if loop
return {} --no idea
end -- ends the function
local storedVariables = getTable("yoursavedfilename") --creates storedVariables, give it the previously attained data
local F = storedVariables[1] --gets the first entry to the table
local lastTask = storedVariables[2] --gets the second entry to the table
I'm probably missing some key basic knowledge or something.
The part I'm confused by is:
local line = file:read("*l")
while line ~= nil do
lines[i] = line
line = file:read("*l")
i = i + 1
end
Could you sum up what is going on please? (I know, baby steps here. I'm sorry.)
#5
Posted 24 February 2013 - 11:23 PM
ksbd, on 24 February 2013 - 11:13 PM, said:
I'm probably missing some key basic knowledge or something.
The part I'm confused by is:
local line = file:read("*l")
while line ~= nil do
lines[i] = line
line = file:read("*l")
i = i + 1
end
Could you sum up what is going on please? (I know, baby steps here. I'm sorry.)
that bit of code basically reads each line in the file, if the line isnt empty (if theres text in it) then it adds the line into the table with a new index
#6
Posted 25 February 2013 - 04:36 AM
Okay, thank you very much. This will make everything SO much easier.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











