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

Help with multiple inputs from user, then redirecting to program

Started by disruptive, 01 April 2017 - 06:36 AM
disruptive #1
Posted 01 April 2017 - 08:36 AM
Hi. So the problem is that I am making a simple mining program and I have a section where I want the user to be able type in START, CANCEL or CHECKFUEL. I have gotten these to work by using this code:

textutils.slowPrint("Initialising mine2x20g...")
sleep(1)
term.clear()
term.setCursorPos(1,1)

print("Welcome to mine2x10g")
print("Place turtle on TOP-LEFT block")
print("Put coal into TOP-LEFT slot")
print("Options: START, CANCEL, CHECKFUEL")

input = read()

if input == "START" then
	print("Executing program!")
	
	while true do
		refuel()
		dig2x20Right()
		dig2x20Left()
	end
	
elseif input == "CANCEL" then
	print("Cancelling program!")
	return
	
elseif input == "CHECKFUEL" then
	print("My fuel level is " .. turtle.getFuelLevel() .. "!")
	
else
	print("Unknown command")
	
end


But now the problem is that I want the user to continue typing commands after the outcomes of CHECKFUEL and the Unknown Command. How would I let the user continue typing commands?

If what I am asking is too confusing please let me know and I will try re-explain.

Thanks in avance!

(PS. Im still pretty new to coding in general.)
Edited on 01 April 2017 - 08:35 AM
Bomb Bloke #2
Posted 01 April 2017 - 10:37 AM
All you really need is to wrap another "while true do" loop around the section that accepts and then processes user input.
disruptive #3
Posted 01 April 2017 - 12:58 PM
All you really need is to wrap another "while true do" loop around the section that accepts and then processes user input.

Thankyou for your help! :)/>
Dave-ee Jones #4
Posted 03 April 2017 - 02:31 AM
All you really need is to wrap another "while true do" loop around the section that accepts and then processes user input.

That wouldn't help him cancelling a program during the turtle's actions…He would need to use coroutines/multitasking.
KingofGamesYami #5
Posted 03 April 2017 - 03:43 AM
That wouldn't help him cancelling a program during the turtle's actions…He would need to use coroutines/multitasking.

I would encourage you to re-read the OP. Nowhere in it did he imply he wanted to stop the turtle mid-action. Furthermore, "He would need to use coroutines/multitasking" is a very wrong answer. The parallel API is all that is required*. As the easiest option, it should be mentioned first. Seriously. Don't try to scare people away!

*Yes, I know how the parallel API works and it does use coroutines. However, they aren't exposed to the user so they shouldn't be mentioned, unless the problem in question cannot be solved without using them or the solution would be more complicated than using coroutines.
Dave-ee Jones #6
Posted 04 April 2017 - 07:12 AM
That wouldn't help him cancelling a program during the turtle's actions…He would need to use coroutines/multitasking.

I would encourage you to re-read the OP. Nowhere in it did he imply he wanted to stop the turtle mid-action. Furthermore, "He would need to use coroutines/multitasking" is a very wrong answer. The parallel API is all that is required*. As the easiest option, it should be mentioned first. Seriously. Don't try to scare people away!

*Yes, I know how the parallel API works and it does use coroutines. However, they aren't exposed to the user so they shouldn't be mentioned, unless the problem in question cannot be solved without using them or the solution would be more complicated than using coroutines.

How silly do you think I am if I am trying to scare him away??? :blink:/>

And why did you just contradict yourself?!
Furthermore, "He would need to use coroutines/multitasking" is a very wrong answer.
*Yes, I know how the parallel API works and it does use coroutines.
Even if he DID use the Parallel API IT IS STILL USING COROUTINES therefore it is not a wrong answer at all!
Saying 'he would need to use coroutines' means he would need to use coroutines, in whatever form that may be. I feel like you are being very silly…

Furthermore, if he wants to learn how it actually works and do it properly (the parallel API is quite limited compared to using raw coroutines yourself) he should look at using coroutines.
They aren't hard.

Half the fun is learning about new things in programming. Whenever you have got something working after an hour or so of trying it you can't help but want to show people your new masterpiece :D/>

@YourStatementAboutCoroutinesShouldStayHidden

1. Why should they have to stay hidden? So what if you can't see them behind the parallel API? You can't see what happens when you use drawBox() in my djOS, doesn't mean you shouldn't know what they are…

2. It's not like coroutines are ridiculously hard to learn either. I only learnt how they work during the construction of djOS, and that was only a few days.

Sure, the parallel API might help him now, but what about if he wants to do proper multitasking in future? The quicker you get into it, the more you can utilise it in smaller programs you make. The more of a habit it becomes, as well.

Just my opinion.

You needn't be so aggressive Yami, and you should probably re-read your posts before submitting them, as you could find you are being silly in your attempt to hurriedly respond to someone else's seemingly wrong reply.

Nothing wrong with trying to quickly try and help someone who may have their facts wrong, but it doesn't mean you should reply aggressively or inappropriately, as you only know as far as you think you know, and that may not be fact.
KingofGamesYami #7
Posted 04 April 2017 - 01:25 PM
Here's the problem: You didn't mention the parallel API at all. He'd never find the documentation for the parallel API (the simplest and quickest solution) by searching for information about coroutines or multitasking. You can certainly mention them, giving him the option to learn that, but you shouldn't arbitrarily withhold information from somebody because you think they should try the hardest concept in ComputerCraft. The OP didn't know how to apply a loop to his program, much less anything quite so fancy as coroutines.

I'm not being very aggressive. I did not insult you. I did not use bold, capital letters (implying shouting… which you did). I simply stated my opinion. I did use one exclamation point. That sentence was half joking, as I'm very sure you weren't actively trying to scare someone away.

As far as your argument against my suggestion that coroutines should stay hidden: I have used the parallel API numerous times after figuring out coroutines. Why? Because it is by far the quickest and easiest solution, and there are no drawbacks to using it in most situations. It eliminates many potential problems, which I appreciate even as an experienced programmer. I admit, there are times coroutines are necessary. However, this is not one of those times.