Posted 20 December 2015 - 06:02 PM
After several days of fighting with this program and referencing this website (along with the api computercraft info pages) I have finally
produced a backbone application of what will be a much larger program.
Yes, I have tested this code, and it works.
Yes, there are better ways of handling the separate functions.
The nature of the program is rather simple, It asks if you want to insert a new computer into the table, it then saves that table to a file. It will recall the file and print off each computer with it's corresponding ID.
I am using minecraft 1.7.10 w/ Forge and computercraft 1.75 (craftOS 1.7)
produced a backbone application of what will be a much larger program.
Yes, I have tested this code, and it works.
Yes, there are better ways of handling the separate functions.
The nature of the program is rather simple, It asks if you want to insert a new computer into the table, it then saves that table to a file. It will recall the file and print off each computer with it's corresponding ID.
I am using minecraft 1.7.10 w/ Forge and computercraft 1.75 (craftOS 1.7)
–SAVING and LOADING functions
function saveTofile(mytable,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(mytable))
file.close()
end
function loadFromfile(name)
local file = fs.open(name, "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
–Input the new computer!
function input()
local i = 1
local nmcompsid = {}
nmcompsid = loadFromfile("IDcompNM")
for m = 1, #nmcompsid do
i = i+1
end
while true do
term.clear()
term.setCursorPos(1,1)
write("Comp ID: ")
compid = read()
compid = tonumber(compid)
if compid == 0 then
saveTofile(nmcompsid,"IDcompNM")
break
end
write("Comp Name: ")
compnm = read()
compnm = tostring(compnm)
table.insert(nmcompsid, {compid, compnm})
term.clear()
term.setCursorPos(1,1)
print("computer just entered")
print(nmcompsid[2], " is id ", nmcompsid[1])
i=i+1
os.sleep(3.0)
end
end
–Print the entire table
function printdb()
mytable = {}
mytable = loadFromfile("IDcompNM")
for i = 1, #mytable do
write(mytable[2])
write(" is id: ")
print(mytable[1])
end
os.sleep(3.0)
end
–This is the main program
local fail = true
while fail do
term.clear()
term.setCursorPos(1,1)
write("Input new Computer Y/N? ")
YN = read()
YN = tostring(YN)
if YN == "Y" or YN == "y" or YN == "N" or YN == "n" then
fail = false
if YN == "Y" or YN == "y" then
input()
end
printdb()
end
end