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

Return to the menu, how?

Started by Xixili, 18 December 2013 - 06:14 PM
Xixili #1
Posted 18 December 2013 - 07:14 PM
Im writing a menu with diffirent options.

if you want option A for example then the programs executes option A
When its finished it needs to return to the menu without starting all over again.
Wich means skip the login part but I dont know how.


So after the part DOORS OPEN (bottom of the code)
it should return to the menu (above the DOORS part)

the code:


os.pullEvent = os.pullEventRaw
local standby = 5
local side = "bottom"
local password = "appel"
local YES = "y"
local a = "a"
local b = "b"
local c = "c"
local d = "d"
local e = "e"

-- we'll put the "Security Panel" text exactle in the middle
-- and this'll help us fill the last row with orange
local width, height = term.getSize()

term.setCursorPos(1, 1)
term.setBackgroundColor(colors.white)
term.clear() -- fill the screen with white

term.setTextColor(colors.white)
term.setBackgroundColor(colors.red)
term.clearLine() -- that will fill the first row of the screen with orange

term.setCursorPos(1, height)
term.clearLine() -- ditto, but the cursor is on the last row, so will be the filling

local text = "<<< CAUTION >>>"
term.setCursorPos(math.floor((width - #text) / 2), 1)
-- this is the native text writing method from the term API
-- it writes text where the cursor currently is
term.write(text)

-- #### STAGE 1 LOGIN

term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
term.setCursorPos(1, 3)
term.write("STAGE I.")
term.setCursorPos(4, 5)
term.write("To continue, please enter the purge code.")
term.setTextColor(colors.black)
term.setCursorPos(4, 8)
term.write("Security Code: ") -- write the Password message
local input = read("*")
term.setCursorPos(4, 16) -- took this out of the IF block - it happens anyway
if input == password then
	term.write("Correct")
sleep(1)
else
	term.write("WRONG")
	sleep(1)
os.reboot()
end

-- #### ENDSTAGE 1 LOGIN

term.setCursorPos(1, 1)
term.setBackgroundColor(colors.white)
term.clear() -- fill the screen with white

term.setTextColor(colors.white)
term.setBackgroundColor(colors.red)
term.clearLine() -- that will fill the first row of the screen with orange

term.setCursorPos(1, height)
term.clearLine() -- ditto, but the cursor is on the last row, so will be the filling
local text = "<<< Emergency Destruct System >>>"
term.setCursorPos(math.floor((width - #text) / 2), 1)
-- this is the native text writing method from the term API
-- it writes text where the cursor currently is
term.write(text)

-- #### MENU

term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
term.setCursorPos(1, 3)
term.write("Purge Menu.")
term.setCursorPos(4, 5)
term.write("a - Open blast doors.")
term.setCursorPos(4, 6)
term.write("2 - Close blast doors.")
term.setCursorPos(4, 7)
term.write("3 - Inspect Purge System.")
term.setCursorPos(4, 8)
term.write("4 - Countdown Test.")
term.setTextColor(colors.red)
term.setBackgroundColor(colors.white)
term.setCursorPos(4, 9)
term.write("5 - COMMENCE PURGE.")
term.setCursorPos(4, 11)
term.setTextColor(colors.black)
term.write("Option: ") -- write the Password message
local input = read("*")

-- #### DOORS OPEN

term.setCursorPos(4, 16) -- took this out of the IF block - it happens anyway
if input == a then
	term.setTextColor(colors.orange)
	term.write("Opening Blast Doors")
	rs.setBundledOutput("bottom", colours.purple)
	sleep(0.2)
	rs.setBundledOutput("bottom", colours.black)
	sleep(0.8)
	rs.setBundledOutput("bottom", colours.purple)
	sleep(0.2)
	rs.setBundledOutput("bottom", colours.black)
sleep(0.8)
	rs.setBundledOutput("bottom", colours.purple)
	sleep(0.2)
	rs.setBundledOutput("bottom", colours.black)
sleep(0.8)
	rs.setBundledOutput("bottom", colours.purple)
	sleep(0.2)
	rs.setBundledOutput("bottom", colours.black)
sleep(0.8)
	rs.setBundledOutput("bottom", colours.purple)
	sleep(0.2)
	rs.setBundledOutput("bottom", colours.black)
sleep(0.8)
	rs.setBundledOutput("bottom", colours.purple)
	sleep(0.2)
	rs.setBundledOutput("bottom", colours.black)
sleep(0.8)
end
Edited on 18 December 2013 - 06:35 PM
Bomb Bloke #2
Posted 18 December 2013 - 08:02 PM
Your program ends because it runs out of instructions to follow. The "simple" answer is to wrap your main program block inside a loop:

while true do  -- A loop that repeats so long as "true" evaluates to "true".
  -- #### MENU
  .
  (Menu drawing stuff here)
  .

  if input == a then
    -- #### DOORS OPEN
    .
    (Door opening stuff here)
    .
  elseif input == b then
    -- #### DOORS CLOSE
    .
    (Door closing stuff here)
    (Add more "elseif"s as need be)
  end
end
Xixili #3
Posted 18 December 2013 - 11:24 PM
Thanks that worked!

Is it also possible to show all this on a touchscreen?
oeed #4
Posted 18 December 2013 - 11:31 PM
Thanks that worked!

Is it also possible to show all this on a touchscreen?

If you mean gold computers:

Drawing is identical. Making it "clickable" is a bit harder though. I'm on my iPad right now so i can't really give a code example, but take a look on the wiki. Your looking for the mouse_click event.

If you meant gold monitor:

Identical to above, but using monitor_touch event. Again, take a look on the wiki on how to redirect to the monitor.
Xixili #5
Posted 19 December 2013 - 08:13 AM
Thanks that worked!

Is it also possible to show all this on a touchscreen?

If you mean gold computers:

Drawing is identical. Making it "clickable" is a bit harder though. I'm on my iPad right now so i can't really give a code example, but take a look on the wiki. Your looking for the mouse_click event.

If you meant gold monitor:

Identical to above, but using monitor_touch event. Again, take a look on the wiki on how to redirect to the monitor.
thanks i found it.
Its indeed harder but I will try
Xixili #6
Posted 19 December 2013 - 11:06 AM
The script I wrote is to purge a facility.

The console (Advanced Computer) can control the purge system.
For example to open the hatch and do systemchecks.

But if I want to activate the purge system you need to enter a password.
But I for safety a password that needs to be entered the same time on 2 different computers.
When that is done the purge system is activated

I assume this has to go trough rednet?
Or is there an easier way?
MKlegoman357 #7
Posted 19 December 2013 - 11:21 AM
You could also use redstone. I don't see other good system for that.
Lyqyd #8
Posted 19 December 2013 - 11:29 AM
Please stick to one topic for all questions on a given project or piece of code.
Xixili #9
Posted 19 December 2013 - 12:50 PM
Okay I wrote a part that needs 2 computers to continue and it works.

After you logged in it should show a countdown timer wich it does but the problem is… after 1 second it refreshes the page and jumps back to the login part.
How can I avoid that?




NVM - problem fixed
Edited on 19 December 2013 - 11:54 AM
Bomb Bloke #10
Posted 19 December 2013 - 05:39 PM
I assume this has to go trough rednet?
Or is there an easier way?
Through rednet, you would have each of the two computers message a third. The third computer would sit waiting for events, and on receiving rednet messages, check to see if each password was correct. If so, it'd add one to a counter variable and start a timer that lasted just a couple of seconds or so.

If a timer expired, then it'd subtract one from the counter again. If the counter made it up to a value of two (or however many computers you wanted the password to be entered in "at once"), then it'd go ahead with the "purge". That is to say: If two computers successfully sent the correct password within the space of two seconds, you get a purge.

Redstone would be relatively simple (… barely) - the third computer would have a wire going into each side from the other two computers, and sit there waiting for redstone input change events. When each occurred, it'd check to see if both inputs were active at once, and start the purge if so.

The downsides to the redstone method are two-fold - for one you'd need to run the wire, but also it'd be very easy to hack (break computer, place redstone torch).
Xixili #11
Posted 21 December 2013 - 05:39 PM
The system also controls the lights in the room when starting te computer.

The steps are:
1.Turning on the computer
2.Login with your password
3. The computer opens the blast door (works fine)
4. the menu opens with the options

What I wish and what I tried is to add the lights aswell at step 3 (See line 109 on pastebin)
It works, it turns on the ligths in the room.

But when I use another option like opening the hatches in the menu, the lights goes off.
So how do I keep that redstone (Bundled cable) active?

http://pastebin.com/xUYPJU9T
Bomb Bloke #12
Posted 21 December 2013 - 06:25 PM
Use rs.getBundledOutput() to check what signal you're currently sending, use colors.combine() or colors.subtract() to modify it, then start outputting your new signal with rs.setBundledOutput() again.

By the way, "for" loops are awesome. You can use them to reduce repetitive stuff like this:

Spoiler
    rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
        sleep(0.6)
    rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
        sleep(0.6)
    rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
        sleep(0.6)
    rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
        sleep(0.6)
        rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
        sleep(0.6)

… down to something like this:

Spoiler
for i=1,6 do
    rs.setBundledOutput("bottom", colours.yellow)
    sleep(0.6)
    rs.setBundledOutput("bottom", colours.pink)
    sleep(0.6)
end
Xixili #13
Posted 21 December 2013 - 06:32 PM
Nice thank you!

What does i stands for?
for i=1, 4 do
Edited on 21 December 2013 - 05:37 PM
Bomb Bloke #14
Posted 21 December 2013 - 06:46 PM
"i" is a variable used to hold a counter value. When the code inside the "for" block runs the first time, "i" is set to one. The block then repeats, and "i" is automatically incremented by 1, and so on until "i" is 4 (or 6, in the example I posted).

Try running some loops which print the counter, eg:

for k=5,10 do print (k) end

Sometimes you'll want to use the counter value in your code. Sometimes you'll just want your code to repeat a certain number of times.

You'll soon get the hang of it. I suggest running a web search on "Lua for loops" for more information, as there's a little more to what you can do with them then that.