Jump to content




Refinery Control problem


  • You cannot reply to this topic
23 replies to this topic

#1 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 09:47 PM

Working on a script that will make a terminal act as the central control unit to an oil refining facility. Little harder than I expected...


local Alphaprimary = false
local Alphasecondary = false
local Betaprimary = false
local Betaprimary = false
local Command = stop

while Command == stop do
 term.clear()
 term.setCursorPos(1, 1)
 print("What refinery command would you like to execute?\nIf you don't know what to type, type 'help')")
 Command = read()
while Command ~= stop do 
  if Command==test then
  term.clear()
  term.setCursorPos(1, 1)
  print("This is a test")
  sleep(3)
  Command = stop
  sleep(1)
 end
 if Command == help then
  term.clear()
  term.setCursorPos(1, 1)
  print("You may type the following:")
  print("'help' to display this text.")
  print("'alpha primary on' to turn the Alpha Primary Engines on.")
  print("Type 'stop' on any screen to go back to command line")
  Command = read()
end

What I am trying to achieve is an infinite loop that when a user enters a listed command, it will execute that command and reset itself. But I have been unable to achieve that, Help? Yes this code is very much unfinished...

#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 September 2012 - 09:52 PM

You need to have your commands in quotations. Right now, you are comparing if they are equal to varibles like stop, help, and test. If you put them in quotes, you are comparing to strings that read like that.

#3 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 09:59 PM

Well that is much better... But I am still at the problem where when a command is executed, the script ends. So how would I make this run indefinitely?

#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 September 2012 - 10:07 PM

Instead of doing while command == stop do, you can do while true do, and then use break to exit out when you want to. For example:
while true do --start infinite loop
local input = read()
if input == "stop" then
break --exits the loop.
end
end


#5 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 10:15 PM

I don't quite understand how that would work. [while Command == "stop" do] accomplishes what it needs to, but only once. So when Command ~="stop", it will execute the command, but the problem I have is that it will not return to the [while Command == "stop" do] loop. Will break keep the loop running so that when Command is returned to the "stop" string, it will re-enter the [while Command == "stop" do] loop?

#6 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 10:18 PM

Don't use break
Break is bad programming

#7 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 10:20 PM

Here is a template of how you would do what you want:

local bRunning = true
while bRunning do --Set bRunning to false when you want to stop the whole thing.
  local sCommand = ""
  while sCommand ~= "stop" do
    --Do stuff
  end
end


#8 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 September 2012 - 10:21 PM

Really? How is break bad programming? If you want a program to run until you give a command, then go to the previous loop or shell, then break is the perfect thing to use...
I use break quite often in my programming, so I have no idea how you think it's bad.

#9 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 10:25 PM

View PostCranium, on 28 September 2012 - 10:21 PM, said:

Really? How is break bad programming? If you want a program to run until you give a command, then go to the previous loop or shell, then break is the perfect thing to use...
I use break quite often in my programming, so I have no idea how you think it's bad.

Break is just a glorified GOTO.

You want your program to use control flows, not jumps. In the real world break is only used for exiting on abnormal conditions.

#10 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 10:26 PM

Don't get me wrong, there are situations where using break is justifiable, but for the sake of readability, use of it is generally frowned upon.

#11 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 10:28 PM

View PostCranium, on 28 September 2012 - 10:21 PM, said:

Really? How is break bad programming? If you want a program to run until you give a command, then go to the previous loop or shell, then break is the perfect thing to use...
I use break quite often in my programming, so I have no idea how you think it's bad.

I did not create this thread to offend people, just to get help. That sounds more like what I am trying to accomplish than dimitri's latter post, So once I put the break in
while true do
 term.clear()
 term.setCursorPos(1, 1)
 print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
 Command = read()
 break
end
it should be kept continuously running? So when a command is executed and the Command variable returns to it's "stop" variable, it will continue that loop?

#12 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 September 2012 - 10:31 PM

I would disagree. I will only say that break is a tool, as is GOTO. If what I want to do is accomplished by the code and script I give it, who the hell cares? But this is not the place to argue proper syntax. I think either would be able to help mttasch1, if implemented properly.

#13 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 September 2012 - 10:33 PM

while true do
term.clear()
term.setCursorPos(1, 1)
print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
Command = read()
if Command == "stop" then break --if given the stop command, stop the loop
elseif Command =="somethingElse" then
   --do something else
 end
end
It's used more like this.

#14 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 10:37 PM

while true do
term.clear()
term.setCursorPos(1, 1)
print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
Command = read()
break
end
That break is useless...
It simply terminates the loop at the end of first execution. Here:
local Alphaprimary = false
local Alphasecondary = false
local Betaprimary = false
local Betaprimary = false
local bRunning = true
while bRunning do
  term.clear()
  term.setCursorPos(1, 1)
  print("What refinery command would you like to execute?nIf you don't know what to type, type 'help')")
  local Command
  while Command ~= "stop" do
	Command = read()
	if Command=="test" then
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("This is a test")
	  sleep(3)
	  Command = "stop"
	  sleep(1)
	elseif Command == "help" then
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("You may type the following:")
	  print("'help' to display this text.")
	  print("'alpha primary on' to turn the Alpha Primary Engines on.")
	  print("Type 'stop' on any screen to go back to command line")
	  Command = read()
	end
  end
end
That should work.

Edit: Damn forum software makes my perfectly well indented code look weird...

#15 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 10:40 PM

Can I have more than one elseif in the code? or it does not work that way? Bear in mind there will be more than "test" and "help".

#16 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 10:43 PM

It works fine but there's an infinite loop, infinite loops = bad.
Instead of "while true do" use:
local bRunning = true
while bRunning do
That makes it possible to exit without a break which is as I said before, bad.

Edit: also replace this line:
if Command == "stop" then break
with this:
if command == "stop" then bRunning = false

2nd edit: Also, being labeled a "Script Kiddie" is demeaning :P/>

#17 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 11:05 PM

YES! It works, and actually I accomplished it with both of your code. So thank you gentlemen of this thread for your help!

#18 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 28 September 2012 - 11:08 PM

Dimitriye98 is right. bRunning will work. I just never liked doing it that way. So now we have one scripter who likes it his way, but accepts others as being valid, and uses either, and one scripter vehemently denouncing a tool that works. Do what you want. Either works.

#19 dimitriye98

  • Members
  • 77 posts

Posted 28 September 2012 - 11:16 PM

I'm not making up the no break thing. That's what they teach you in programming class in school. GOTO, break, and continue make code difficult to read and become huge problems if you ever work in a collaborative project.

#20 mttasch1

  • Members
  • 10 posts

Posted 28 September 2012 - 11:17 PM

local Alphaprimary = false
local Alphasecondary = false
local Betaprimary = false
local Betaprimary = false
local bCommand = true
while bCommand do
term.clear()
term.setCursorPos(1, 1)
print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
input = read()
if input == "help" then
  term.clear()
  term.setCursorPos(1, 1)
  print("You may type the following:")
  print("*")
  print("'help' to display this text.")
  print("*")
  print("'alpha primary on' to turn the Alpha Primary Engines on.")
  print("*")
  print("PRESS ANY KEY TO CONTINUE")
  Command = read()
end
if input == "alpha primary on" then
  term.clear()
  term.setCursorPos(1, 1)
  redstone.setOutput("back", true)
  print("Alpha Primary Engines Activated!")
  sleep(3)
end
if input == "alpha primary off" then
  term.clear()
  term.setCursorPos(1, 1)
  redstone.setOutput("back", false)
  print("Alpha Primary Engines Deactivated!")
  sleep(3)
end
end

This is what I have, and it works. I would like to know of alternatives to accomplishing what has been achieved here, I do not feel I am leaving this thread with a good understanding of what break actually does. Oh and another thing, would there be a way to make a terminal power more than top, bottom, left, right, front and back? I mean I have to dictate what these sides power, but I have only 3 open spaces with 4 things that need powered.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users