95 posts
Location
A CPU some where in Bolton, UK
Posted 12 April 2014 - 10:29 PM
Im working on a project and it has a function for severs I.E different computers..
by default its set to false so…
Serverside = false
on the settings page it will have a option to set Server Mode On, i know i could do
if input == "Server Mode On" then
Serverside = true
end
But then you would have to enable it everytime the computer reboots
i have looked into "fs" and "io" but i dont get it…
how would i get it to chage to true without the user editing the program or doing the if statment?
if anyone can help i would be very thankful.
Edited on 12 April 2014 - 08:53 PM
147 posts
Location
My Computer
Posted 12 April 2014 - 10:45 PM
First off, Serverside and ServerSide are different. Change your code to:
if input == "Server Mode On" then
h = fs.open("thing", "w")
h.write("true")
h.close()
Serverside = true
end
And in your startup:
h = fs.open("thing", "r")
b = h.readAll()
h.close()
if b == "true" then
Serverside = true
end
95 posts
Location
A CPU some where in Bolton, UK
Posted 12 April 2014 - 10:54 PM
First off, Serverside and ServerSide are different. Change your code to:
if input == "Server Mode On" then
h = fs.open("thing", "w")
h.write("true")
h.close()
Serverside = true
end
And in your startup:
h = fs.open("thing", "r")
b = h.readAll()
h.close()
if b == "true" then
Serverside = true
end
Please could you explane what this is doing and my main program will be named startup so will i have to change this?
and wouldnt i have to tell it where in the code i want it changing to true?
Edited on 12 April 2014 - 08:59 PM
147 posts
Location
My Computer
Posted 12 April 2014 - 11:16 PM
Put the code I said should be in your startup at the top of your code in startup.
h = fs.open("thing", "w") --Tells the code where to open the file, and w means to overwrite everything already in it.
h.write("true") -- Writes "true" to the file "thing", so when the code runs, it can recognize that server mode is on.
h.close() -- Closes the file
And:
h = fs.open("thing", "r") --Opens the file we wrote "true" in. r means it opens it as read only.
b = h.readAll() --B now is everything that is in the file
h.close() --Again, closes the file.
if b == "true" then --If the person had previously wanted server mode on, it wrote true to the file, and now it is checking if it did or not
Serverside = true --Turns on server mode
end -- Close statement
If you need more just ask
95 posts
Location
A CPU some where in Bolton, UK
Posted 13 April 2014 - 03:46 PM
Put the code I said should be in your startup at the top of your code in startup.
h = fs.open("thing", "w") --Tells the code where to open the file, and w means to overwrite everything already in it.
h.write("true") -- Writes "true" to the file "thing", so when the code runs, it can recognize that server mode is on.
h.close() -- Closes the file
And:
h = fs.open("thing", "r") --Opens the file we wrote "true" in. r means it opens it as read only.
b = h.readAll() --B now is everything that is in the file
h.close() --Again, closes the file.
if b == "true" then --If the person had previously wanted server mode on, it wrote true to the file, and now it is checking if it did or not
Serverside = true --Turns on server mode
end -- Close statement
If you need more just ask
so how would i implement this into a 2000 line program becouse if i do the
fs.open("thing", "w")
then it would wipe all 2000 lines?
570 posts
Posted 13 April 2014 - 06:08 PM
so how would i implement this into a 2000 line program becouse if i do the
fs.open("thing", "w")
then it would wipe all 2000 lines?
Your program won't write to itself. It will create a new file called "thing" and write to that (unless your program's name is "thing"). fs stands for filesystem. It's used to copy files, move files, delete files, make directories, create/modify files etc. While a program can modify itself, that would be considered very bad program design.
8543 posts
Posted 13 April 2014 - 09:22 PM
There's nothing wrong with self-modifying code, as long as you're careful about it. You'd need to first read the entire file into a table of lines or some such, so you could modify that table and then write it back out into the same file name. Just be careful about it and it would be fine. Of course, it's nearly always easier to just use a separate file for saving settings in.