Jump to content

Some help with computercraft appreciated


Recommended Posts

So, trying to make a control program for my reactor building. Right now it's mostly an experiment to see what I'm capable of making, as a start to make a more fully automated factory where most things can be controlled from a computer (or a series of computers).

Now, I have a few issues both with this specific program and general computercraft usage. I've tried the tutorials, googled and search on my own as well as experimenting and have made some progress but there are still things I don't understand. It should be said I have nearly no scripting experience at all (a little jazz for wc3 scripting, a little html and so this).

1. The more general and more important question: How do I run things remotely? I get how I can hook up computers via a wireless modem, and how to send messages - but how do I get them to run scripts? Without that it's not very useful to have modems. Since my computers will take a bit of space with all the cables, outputs and wireless transmitters I figured I'll make a main server room that contains all the main computer(s) and wireing, and then have consoles with modems hooked up in different places from where I tell the server to do stuff.

2. Once my program has been run once and exited by alternative four, it cannot be run again on the computer unless I restart the computer by the shutdown command. I imagine it has something to do with the "while" command but really have no idea.

3. Regardless of the status of the lights, whenever they are turned on or off they show the "The lights are off!" message. This is not true for the other choosable alternatives. Any ideas what is wrong?

My code:

lastaction = nada

while input~=exit do

term.clear()

term.setCursorPos(1,1)

 

lightstatus = rs.testBundledInput ("back", colors.lightBlue)

outdoorstatus = rs.testBundledInput ("back", colors.red)

indoorstatus = rs.testBundledInput ("back", colors.orange)

 

lights = "1"

outdoor = "2"

indoor = "3"

exit = "4"

 

 

print ("      Welcome to Nuclear Disaster Control 0.1!")

print ("\nWhat would you like to do?\n")

if lightstatus == false then

  print ("1. Turn lights on")

  else

  print ("1. Turn lights off")

end

 

if outdoorstatus == false then

  print ("2. Open outer door")

  else

  print ("2. Close outer door")

end

 

if indoorstatus == false then

  print ("3. Close core access")

  else

  print ("3. Open core access")

end

print ("4. Exit the NDC0.1")

print (lastaction)

 

 

  input = read()

  if input == lights then

    if lightstatus == false then

        redstone.setBundledOutput("back", rs.getBundledOutput("back")+colors.lightBlue)

        lastaction = "The lights are on!"

        lightstatus = true

        else

        redstone.setBundledOutput("back", rs.getBundledOutput("back")-colors.lightBlue) end

        lastaction = "The lights are off!"

        lightstatus = false

    else if input == outdoor then

        if outdoorstatus == false then

        redstone.setBundledOutput("back", rs.getBundledOutput("back")+colors.red)

        lastaction = "The outer door is now open!"

        outdoorstatus = true

        else

        redstone.setBundledOutput("back", rs.getBundledOutput("back")-colors.red)

        lastaction = "The outer door is now closed!"

        outdoorstatus = false

        end

      else if input == indoor then

        if indoorstatus == false then

        redstone.setBundledOutput("back", rs.getBundledOutput("back")+colors.orange)

        lastaction = "The core access is closed!"

        indoorstatus = true

        else

        redstone.setBundledOutput("back", rs.getBundledOutput("back")-colors.orange)

        lastaction = "The core access is open!"

        indoorstatus = false

        end

      else if input == exit then

        print("\n      Thank you for using this NDC0.1!")

        print("\nYou are now in your root directory.")

        break

      end

      end

    end

    end

end

Thanks for any aid!

Link to comment
Share on other sites

Been half a day, thought it might be okay with a bump... Please tell if it's against the rules (couldn't find anything against it).

Content-free bumps are against the rules, but less because they're bumps and more because they're content-free. If you had an update of some kind...

Link to comment
Share on other sites

I don't know if you've got an answer to this on the suggested Computer Craft forum, but just in case.

1) With modems attached to computers you can use the Rednet API to send/receive and broadcast/receive. Note that you use the same API over bundled cables as you do wireless modems which can be hugely convenient if you start with cables and later want to upgrade. The only difference, IIRC, is that bundled cables will return a nil distance on receive which can be useful if you need to identify the delivery method. There may be other differences though.

2) Yes, you'll want to use a while loop. See code and comments below.

3) To be honest, I'm surprised that script runs at all. Here are a couple *really* important but really easy to miss problems in your script's current format:

Line 8: "end" keyword at end of line, effectively terminating the "if lightstatus==" block.

It's also the answer to your number 3, because lines 9 and 10 will always run if input == lights.

Lines 35-39 are all "end". I'm not sure you've understood how these work. Take a look at the Conditional Statement Tutorial again. In your case, you only need as many "end"s as you have "if"s (not including "else if"s). I count 4 "ifs", 8 "ends".

To be able to spot this stuff, follow simple formatting rules such as always increasing your indent the line after an if/loop declaration, and decrement the indent again after typing end.

With that simple rule, and a change to address Line 8 to put the "end" where I think it should go, I ended up with the following (note the indentation and the 4 commented out ends at the bottom which leaves you with 4 ifs, 4 ends).


input = read()

while true do -- this is an infinite loop

  if input == lights then

    if lightstatus == false then

        redstone.setBundledOutput("back", rs.getBundledOutput("back")+colors.lightBlue)

        lastaction = "The lights are on!"

        lightstatus = true

    else

        redstone.setBundledOutput("back", rs.getBundledOutput("back")-colors.lightBlue)

        lastaction = "The lights are off!"

        lightstatus = false

    end

  else if input == outdoor then

    if outdoorstatus == false then

      redstone.setBundledOutput("back", rs.getBundledOutput("back")+colors.red)

      lastaction = "The outer door is now open!"

      outdoorstatus = true

    else

      redstone.setBundledOutput("back", rs.getBundledOutput("back")-colors.red)

      lastaction = "The outer door is now closed!"

      outdoorstatus = false

    end

  else if input == indoor then

    if indoorstatus == false then

      redstone.setBundledOutput("back", rs.getBundledOutput("back")+colors.orange)

      lastaction = "The core access is closed!"

      indoorstatus = true

    else

      redstone.setBundledOutput("back", rs.getBundledOutput("back")-colors.orange)

      lastaction = "The core access is open!"

      indoorstatus = false

    end

  else if input == exit then

    print("\n      Thank you for using this NDC0.1!")

    print("\nYou are now in your root directory.")

    --break

    return -- if there is nothing to run outside of the while loop, return will simply exit the script/function

  end

end -- extra end added here to mark the end of the while loop added

 

-- the following are all the unnecessary ends in your original script

  -- end

    -- end

    -- end

-- end

Hopefully that solves your issues?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...