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

Need to enter prompt twice

Started by graywolf69, 01 October 2014 - 01:26 PM
graywolf69 #1
Posted 01 October 2014 - 03:26 PM
Again, I'm stuck. I was testing my code and whenever I was typing something into it (like a password, anything i typed in), it didn't do anything unless i retyped it. for example i would type 12345, enter, 12345, enter, for it to work. Here is the code, and thanks for your help!

http://pastebin.com/UnVkVL8v
Choonster #2
Posted 01 October 2014 - 03:49 PM
I'm guessing it's because you're calling read() once for every conditional in your if statements. You should only call it once any time you want the user to input something, storing the result in a variable and using the value of that in your conditionals (like you do for the initial command).

As an example, change this:
if read() == "on" then
    term.setTextColor(colors.lime)
    print("Power coming ONLINE")
    rednet.send(INSERT16, "powereon")
    term.setTextColor(colors.white)

elseif read() == "off" then
    term.setTextColor(colors.red)
    print("Power going OFFLINE")
    rednet.send(INSERT16, "poweroff")
    term.setTextColor(colors.white)

else
    print("Invalid. Accepted Commands: on, off.")
end

To this:
local input = read()
if input == "on" then
    term.setTextColor(colors.lime)
    print("Power coming ONLINE")
    rednet.send(INSERT16, "powereon")
    term.setTextColor(colors.white)

elseif input == "off" then
    term.setTextColor(colors.red)
    print("Power going OFFLINE")
    rednet.send(INSERT16, "poweroff")
    term.setTextColor(colors.white)

else
    print("Invalid. Accepted Commands: on, off.")
end
TheOddByte #3
Posted 01 October 2014 - 03:51 PM
Because you're doing this

if read() == "something" then
    ...

elseif read() == "something_else" then
    ...
end
it calls read again.

Try assigning it to a variable instead and use that for checking

local command = read()
if command == "fc" then
    local password = read( "*" )
    if password == "Password" then
        local input = read()
        if input == "on" then
            ...
        elseif input == "off" then
            ...
        end
    end
end
graywolf69 #4
Posted 01 October 2014 - 03:52 PM
Thanks!