This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mistertimn's profile picture

Loop Back To Another Spot In A Program Solved

Started by mistertimn, 18 July 2013 - 11:39 PM
mistertimn #1
Posted 19 July 2013 - 01:39 AM
I'm building a Vault from Fallout with some friends, and I'm trying to create the RobCo UOS for the Overseer's computer. I'm using os.pullEvent("char") to allow the user to hit a key to make a menu selection, have the computer go to a new menu screen or run a program. After a program finishes, for example, a letter displays out on the screen, how would I have the program loop back to the menu screen before the letter to allow the user to select something else, or to hit another key to go back to the previous menu?

Code: (couldn't find a spoiler option, sorry)

term.clear()
term.setCursorPos(1,1)

-- being menu screen 1

textutils.slowPrint("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM")
textutils.slowPrint("COPYRIGHT 2075-2077 ROBCO INDUSTRIES")
print(" ")
textutils.slowPrint("Welcome Overseer")
sleep(0.5)
print(" ")
print("[1] Resident Files")
sleep(0.5)
print(" ")
print("[2] VAULT-TEC Files")
sleep(0.5)
print(" ")
print("[3] Open Overseer Passage")

-- end menu screen 1
os.pullEvent ("key")
event, menu1 = os.pullEvent("char") -- get menu 1 entry

if menu1 == "1" then -- resident files block
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM")
  textutils.slowPrint("COPYRIGHT 2075-2077 ROBCO INDUSTRIES")
  textutils.slowPrint("-Server 1-")
  sleep(1)

  print(" ")
  print("[1] canuckminer")
  print(" ")
  sleep(0.5)
  print("[2] t020342")
  print(" ")
  sleep(0.5)
  print("[3] Tvink98")
  --start selections to list files
end

if menu1 == "2" then -- vault-tec files block
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM")
  textutils.slowPrint("COPYRIGHT 2075-2077 ROBCO INDUSTRIES")
  textutils.slowPrint("-Server 2-")
  sleep(1)

  print("[1] Letter from Dr. Stanislaus Braun")
  print(" ")
  sleep(0.5)
  print("[2] Attachment A")
  print(" ")
  sleep(0.5)
  print("[3] CONFIDENTIAL")
end

  event, menu3 = os.pullEvent("char") -- input for vault-tec menu

  if menu3 == "1" then
	term.clear()
	term.setCursorPos(1,1)
	shell.run("lettertooverseer")
  end
Lyqyd #2
Posted 19 July 2013 - 02:20 AM
Split into new topic.

You'll want to use loops, or for a small piece of code called throughout, a function.
mistertimn #3
Posted 19 July 2013 - 02:39 AM
Okay so I think I may have figured this out, but I'm not sure. I've gone and set up an if block for menu3 which I think should loop back to like x=6 and run the code from there.

This is the if block I wrote:

if menu3 == "4" then
    for x=6, 1 do
  end

When I run it, I get the error bios:337: [string "menu"]:110: 'end' expected (to close 'if' at line 96)

The if block I added starts at line 96, and it has end after it so I'm not sure what I did wrong.
GopherAtl #4
Posted 19 July 2013 - 03:53 AM
the end there is ending the for loop. The if needs another end. The haphazard indenting is confusing things, it should look like this


if menu3 == "4" then	
  for x=6, 1 do  
	--//repeated code goes here
  end --//ends the for loop
--//end for if should go here
With proper indenting like this, you can tell at a glance if you're missing an end, because the last end should be all the way to the left again, not indented at all.
mistertimn #5
Posted 19 July 2013 - 01:57 PM
Ah okay. Last night I was looking back at the post and kinda thought identing had something to do with it, but wasn't able to go an fix it. So after the for x=6, 1 do I need to rewrite the code I want it to repeat?
albrat #6
Posted 19 July 2013 - 07:13 PM
for, while … do, if, function() … etc need an End.
mistertimn #7
Posted 19 July 2013 - 10:47 PM
Yeah I got that part, but in the line


if menu3 == "4" then
	for x=6, 1 do
	-- code
	end
end

Do I need to re-write what I want the computer to execute where '– code' is? I'm thinking I may split each menu screen into it's own program, and us shell.run() to execute them when the user hits the correct key.

And thanks for all the help everyone!
albrat #8
Posted 20 July 2013 - 06:33 AM
you could do


function Header()  -- Print our header
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM")
  textutils.slowPrint("COPYRIGHT 2075-2077 ROBCO INDUSTRIES")
end

This saves us a few lines of code on each header call. (3 occurances in your script)



function Ptext(text)
  if type(text) ~= string then print ("Error : Ptext : got "..type(text).."expected string") break end

  print(text)
  print(" ")
  sleep(0.5)
end

instead of each

print("[1] canuckminer")
print(" ")
sleep(0.5)

just put "Ptext("[1] canuckminer")" in your code (or what ever you wish to print in that style.) then it prints 1 line, menu option, 1 line. and pauses.

so when you are repeating the same code alot you can shorten the code by calling repeated function calls that do the exact same thing.
mistertimn #9
Posted 20 July 2013 - 12:23 PM
Awesome! I'll have to try that when I have a chance later today. Thanks for your help!
mistertimn #10
Posted 20 July 2013 - 08:05 PM
Okay I figured out the functions, thanks for that. But at the time I was so far along in the way I was doing it before, having each menu as it's own script, that I finished it up. But now that it's all done, the os.pullEvent("char") lines are conflicting with each other. When I hit 4 to go back on the second menu, it shuts down the computer. I'm thinking it's because the first menu which is the main script uses four as a shutdown key. So now I'm going to combine it all into on script in TextWrangler. Thanks for all your help everyone! I'm sure I'll be back.
Edit: I've managed to fix it by changing the shutdown key on the main menu to x instead of 4.
Engineer #11
Posted 20 July 2013 - 08:08 PM
Okay I figured out the functions, thanks for that. But at the time I was so far along in the way I was doing it before, having each menu as it's own script, that I finished it up. But now that it's all done, the os.pullEvent("char") lines are conflicting with each other. When I hit 4 to go back on the second menu, it shuts down the computer. I'm thinking it's because the first menu which is the main script uses four as a shutdown key. So now I'm going to combine it all into on script in TextWrangler. Thanks for all your help everyone! I'm sure I'll be back.

Please provide some code so we can help you out :)/> But, when something isnt working, and it turns out you need something that is very critical and you havent known it until now. I think in a few cases a rewrite is necessary, so again, please provide some code when you finished in TextWrangler
mistertimn #12
Posted 20 July 2013 - 11:21 PM
Okay I figured out the functions, thanks for that. But at the time I was so far along in the way I was doing it before, having each menu as it's own script, that I finished it up. But now that it's all done, the os.pullEvent("char") lines are conflicting with each other. When I hit 4 to go back on the second menu, it shuts down the computer. I'm thinking it's because the first menu which is the main script uses four as a shutdown key. So now I'm going to combine it all into on script in TextWrangler. Thanks for all your help everyone! I'm sure I'll be back.

Please provide some code so we can help you out :)/> But, when something isnt working, and it turns out you need something that is very critical and you havent known it until now. I think in a few cases a rewrite is necessary, so again, please provide some code when you finished in TextWrangler

I will definately post it once it's finished! I'm planning to upload the version I currently have working as V1, then once I've rewritten it and condensed it into one file, I'll post that too.

Thanks everyone for all your help!
mistertimn #13
Posted 21 July 2013 - 12:37 AM
Okay here's a quick alpha-ish verison. I can't upload more than 3 files in 24 hours from my computer :/. So this one if missing the files in the VAULT-TEC Files section, they're marked with *'s. But everything should work, I hope…

Type:

pastebin get dZHRAD8L installuos

Then run installuos and cross your fingers! Ha ha.
After it reboots, go

cd uosv1
and run mainmenu.