http://pastebin.com/nEak0u3U
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Menu Needs
Started by bigbaddevil6, 27 December 2012 - 05:43 AMPosted 27 December 2012 - 06:43 AM
I have been working on a menu for what will be a control center for all my machines. I need to have either to have a way to saves the state of the menus so it doesn't reset when the computer restarts I'm assuming its a fs API but i have no idea how to use them. I also need the have a function that passes a variable but every one that I've tried writing doesn't pass the variable when i enter it. Otherwise ill have about 70 if statements… so if anyone wants to help or give ideas and suggestions would be most helpful. The code is in the link below.
http://pastebin.com/nEak0u3U
http://pastebin.com/nEak0u3U
Posted 27 December 2012 - 06:50 AM
It's easy to use the fs API:
Writing:
file = fs.open("file", "r")
line_1 = file.readLine() -- read the first line
line_2 = file.readLine() -- read the second line
line_3 = file.readLine() -- read the third line
line_all = file.readAll() -- read all the text in the file
file.close() -- always close the file
Writing:
file = fs.open("file", "w")
file.write("blah blah")
file.close() -- always close the file
Posted 27 December 2012 - 07:08 AM
so would i assign each redstone state i want to keep to a line to be stored and when i run the program i have it readAll() at the beginning?
Posted 27 December 2012 - 08:38 AM
Yeah in the beginning of the code, just read the state, and when your code is done, save it to the file. Or save it everytime it updates
Posted 27 December 2012 - 01:43 PM
sorry for possibly sounding dumb but im not sure how to call the right variables this is pretty much what i need to save
function Gr()
if n == 1 then print("On") redstone.setOutput("back", true) sleep(2) os.reboot()
elseif n==2 then print("Off") redstone.setOutput("back", false) sleep(2) os.reboot()
elseif n==3 the os.reboot()
end
end
i need to save those redstone and im not sure how to write the string into the file or how to call it so that when it loads it sets all the outputs again
function Gr()
if n == 1 then print("On") redstone.setOutput("back", true) sleep(2) os.reboot()
elseif n==2 then print("Off") redstone.setOutput("back", false) sleep(2) os.reboot()
elseif n==3 the os.reboot()
end
end
i need to save those redstone and im not sure how to write the string into the file or how to call it so that when it loads it sets all the outputs again
Posted 27 December 2012 - 03:43 PM
You just put the local identifier of the passed value in the parentheses of the function definition, where you put the passed value when you call it. So:
The reason I usually don't do this is for the same reason it's a bit silly to do it for this example, it's already more obvious from the code what n is and does than I can make it by renaming it "switch" and saying what values are acceptable. But if you code complex functions with lots of variables it can help…probably.
function Gr(n)
if n == 1 then print("On") redstone.setOutput("back", true) sleep(2) os.reboot()
elseif n==2 then print("Off") redstone.setOutput("back", false) sleep(2) os.reboot()
elseif n==3 the os.reboot()
end
end
A good practice (which I almost never follow) is to put a comment there mentioning the type of value passed and use an identifier that indicates what the value is for. So the first line might look like: function Gr(switch) – integer number 1-3.The reason I usually don't do this is for the same reason it's a bit silly to do it for this example, it's already more obvious from the code what n is and does than I can make it by renaming it "switch" and saying what values are acceptable. But if you code complex functions with lots of variables it can help…probably.
Edited on 27 December 2012 - 02:47 PM
Posted 27 December 2012 - 05:47 PM
i have that for some already, my problem is when i do the os.reboot() it will forget whether the redstone is suppose to be on or off so i need to know how to solve that problem. This is just a small part of the code if i cant get it to save the everything else is useless. I know i need something like how Remix said but i dont know how to save the redstone.setOutput() to the file and then when it starts call the same file and put it where it belongs.
http://pastebin.com/nEak0u3U
http://pastebin.com/nEak0u3U
Posted 27 December 2012 - 09:34 PM
This should work, I added two functions, readState and writeState and another variable 'state' which is the state of the lights, on/off.
pastebin
edit: durr, fixed link xD
pastebin
edit: durr, fixed link xD
Posted 27 December 2012 - 10:17 PM
local hndl = fs.open("startup","w")
hndl.writeLine("shell.run(\"programName\","..passed..","..arguments..","..in..","..order")")
hndl.close()
Posted 28 December 2012 - 04:56 AM
ok so i put the function in and pretty sure i copied it right. it says 169: '=' expected
updated version of the code with the functions put in and being called
http://pastebin.com/3vBCdspR
updated version of the code with the functions put in and being called
http://pastebin.com/3vBCdspR
Posted 28 December 2012 - 06:57 AM
state == "on"
You're setting a variable, so use only one =
== is for comparing two values (if state == "on" then)
= is for setting variables
Posted 28 December 2012 - 08:49 AM
can u redo the link plz should be my last question
Posted 28 December 2012 - 09:06 AM
I see you added quite a bit of options.
You will need a state for each like:
state_doors
state_nuclearReactor
state_lights
etc…
You can write them into separate files or into the same, I'd suggest separate – easier access.
You will need a state for each like:
state_doors
state_nuclearReactor
state_lights
etc…
You can write them into separate files or into the same, I'd suggest separate – easier access.
Posted 28 December 2012 - 11:15 AM
can u ur do ur link cause it doesnt go to the functions u gave me anymore and it says that a string is expected at line 83 so i want to see if i did it right
Posted 28 December 2012 - 06:42 PM
It is saying 76: Expected string, string
http://pastebin.com/Bf1AHJ0K
http://pastebin.com/Bf1AHJ0K
Posted 28 December 2012 - 10:53 PM
You removed the variable for the filename…
Add this to the top of your code:
Add this to the top of your code:
stateFile = ".state"
Posted 29 December 2012 - 12:59 AM
I see you're probably struggling with the code, so I thought I'll help out a bit more.
I redid the whole code and it works flawlessly.
If you need any explaining to do, feel free to ask :)/>
Find it here
I redid the whole code and it works flawlessly.
If you need any explaining to do, feel free to ask :)/>
Find it here
Posted 29 December 2012 - 06:00 AM
Ok love the code actually put some of the features in that i was going to put in, in the future. Just so i can do something like this in the future i have some question so i understand how everything works.
1. So ".doors.txt" is the file that the doors state is stored in along with all of the others. is the txt just part of the name or does it play a role in the function later on?
2. Colour write line 101-115 ive never been able to use args successfully is all its doing is replacing the #arg with the length of the table? Ill look at more arg tuts in future.
3. how your able to scroll all the to the bottom and it jump to the top is lines 140 and 141 its just resetting the value to one so it goes to the top and vice versa.
4. how is it when select back it goes to the main options. line 220 cause i tried doing something like that for mine but it would go to the screen then when i would select something the sub menus would be blank.
also the forestry is messed up it not writing properly it works but when u scroll through it tries to attach farm to the end of all them.
1. So ".doors.txt" is the file that the doors state is stored in along with all of the others. is the txt just part of the name or does it play a role in the function later on?
2. Colour write line 101-115 ive never been able to use args successfully is all its doing is replacing the #arg with the length of the table? Ill look at more arg tuts in future.
3. how your able to scroll all the to the bottom and it jump to the top is lines 140 and 141 its just resetting the value to one so it goes to the top and vice versa.
4. how is it when select back it goes to the main options. line 220 cause i tried doing something like that for mine but it would go to the screen then when i would select something the sub menus would be blank.
also the forestry is messed up it not writing properly it works but when u scroll through it tries to attach farm to the end of all them.
Posted 29 December 2012 - 09:10 AM
1. So ".doors.txt" is the file that the doors state is stored in along with all of the others. is the txt just part of the name or does it play a role in the function later on?
the .txt part defines what type of file it is, it makes it into a Text Document then ;)/> You don't really have to, but I do it so you can open the files easily.
2. Colour write line 101-115 ive never been able to use args successfully is all its doing is replacing the #arg with the length of the table? Ill look at more arg tuts in future.
This is a very awesome advanced colour write function :D/> I actually made a thread asking for one and this is what I got, it's amazing.
What it does is that you can enter as many things in the function as arguments (Colours and Strings). If it picks up a colour, it will set the text colour to the colour,
else it will print the text in that colour. Pretty useful.
3. how your able to scroll all the to the bottom and it jump to the top is lines 140 and 141 its just resetting the value to one so it goes to the top and vice versa.
Yeah, so if the selected number is less than 1, it goes to the last item in the list, and if it goes above the number of options in the list, it reset back to 1.
4. how is it when select back it goes to the main options. line 220 cause i tried doing something like that for mine but it would go to the screen then when i would select something the sub menus would be blank.
You didn't use a loop for it to run indefinitely until you select exit. Also, instead of using numbers like you did 'n', I used a combination of strings and numbers:
Strings for Main Option and Sub Option, and
numbers for the option for On / Off / Back (And Open / Close / Back for the doors).
So it's much easier to understand.
also the forestry is messed up it not writing properly it works but when u scroll through it tries to attach farm to the end of all them.
It's because there are too many options for the foresty table and it scrolls down automatically because of the line spacing between each option.
I just removed the part that adds the line between the options.
pastebin
Posted 29 December 2012 - 08:07 PM
i got a new question for you how do i get a function to pass 2 variables. for example
function get()
while true do
n = redstone.testBundledInput("back", colors.orange)
if n == true then
print("Orange: Online")
else
print("Orange: Offline")
end
end
end
i want to be able to have it where i can assign the colors.orange and the orange in the print() by passing it through the function when called
i assume i need an args but again im not use to them at all
function get()
while true do
n = redstone.testBundledInput("back", colors.orange)
if n == true then
print("Orange: Online")
else
print("Orange: Offline")
end
end
end
i want to be able to have it where i can assign the colors.orange and the orange in the print() by passing it through the function when called
i assume i need an args but again im not use to them at all
Posted 29 December 2012 - 10:53 PM
Yes, you use Arguments within the function.
function get(col) -- Like you want the specefic colour?
while true do
n = redstone.testBundledInput("back", colors[col])
-- If you use the amazing colour write function:
colorW(colors[col], col .. ": ", n and colours.lime or colours.red, n and "Online" or "Offline")
--[[
The part with 'n and colours.lime or colours.red is basically this:'
if 'n' is true (so if redstone.testBundledInput("back", colors[col]) is true) it then
sets the colour to lime, else if it's false, it sets to red.
Same with the next part, if n is true, it prints Online in lime, else Offline in red
--]]
sleep(0) -- You will need it to yield.
end
end
-- Call it like this:
get("orange")