I'm trying to make a basic load up scrip where the computer checks where the modem are placed as well as the Password-Server ID, and then after that write it to a file. So far i haven't got any problems making the so called "Preference File" and writing the different values to it, but the problem is that i can't find a way to read the values from the file correctly.
Here is the part working excellent:
function CreateFiles()
if fs.exists("info") then
print("FILE: INFO EXISTS")
end
for i=1, #rs.getSides() do
if peripheral.getType(rs.getSides()[i]) == "modem" then
print("---------------------------------------------------")
print("Modem found on "..rs.getSides()[i].." side!")
print("---------------------------------------------------")
side = rs.getSides()[i]
sleep(1)
end
end
if side == 0 then
print("No modem attached! Please place modem on any side!")
sleep(1)
print("Recheck in 10 sek!")
print("---------------------------------------------------")
sleep(10)
os.reboot()
else
print("Writing Modem-side to file...")
sleep(1)
print("---------------------------------------------------")
end
rednet.open(side)
rednet.broadcast("Server?")
local senderId, message, distance = rednet.receive(1)
print("[FROM COMPUTER, ID: "..os.getComputerID().."]: Server?")
sleep(1)
-- This part bellow is just to define the server response! --
local senderId = 23
local message = "Server Here"
-- Regular code once again --
if message == "Server Here" then
ServerID = senderId
else
sleep(1)
print("---------------------------------------------------")
print("No response from Password Server!")
sleep(2)
print("Reboot in 5")
print("---------------------------------------------------")
sleep(5)
os.reboot()
end
print("[FROM SERVER, ID: "..senderId.."]: "..message.."...")
sleep(1)
print("---------------------------------------------------")
print("Writing Server ID to file...")
print("---------------------------------------------------")
sleep(2.5)
f = fs.open("info", "w")
f.write("ModemSide="..side.."\nServerID="..ServerID)
f.close()
print("Main program may now start!")
print("---------------------------------------------------")
sleep(2)
end --[[ END CREATEFILES ]]--
As you can see the graphical look of the script is so far very basic, but it was just to see how the computer responded to the code. ( Don't judge! ;)/>/> )
Now to the part I can't get to work:
function ReadFiles()
local file = io.open("info")
if fs.exists("info") == false then
print("Pref file not found! ")
print("ERROR: 372")
end
ServerID = string.gsub(file:read(), "ServerID=")
print(ServerID)
ServerID = tonumber(ServerID)
if ServerID == 0 then
print("ServerID not valid!")
end
ModemSide = string.gsub(file:read(), "ModemSide=")
print(ModemSide)
end
Now if you look on the two print parts (They are just there in purpose of debugging), print(ServerID) will print: ServerID="id" and print(ModemSide) will print: ModemSide="side" ("id" & "side" are of curse depending on the defined values of the computer) Why wont it just print "id" & "side"?? The question is how can I make a function that can read the ServerID and ModemSide from the preference file so that I just get "id" & "side" as a variable?
If anyone could help me with this I would be grateful!
//BBAAYYEERR