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

Need your help...please

Started by Freez, 26 February 2017 - 11:43 AM
Freez #1
Posted 26 February 2017 - 12:43 PM
Can someone fix the problems, because i not very good in Computercraft.

term.clear()
term.setCursorPos(1,1)
term.setCursorBlink(true)
turtle.select(1)
textutiles.slowPrint("Welcome...please enter the WaterBukkit:")
while true do
if turtle.getItemCount(1) == 1 then
break
end
textutiles.slowPrint("Start in 3 seconds!")
os.sleep(3)
term.clear()
term.setCursorPos(1,1)
textutiles.slowPrint("Program has startes")
eStop = io.read()
textutiles.slowWrite("Type stop to stop the program")
while true do
if eStop == "stop" then
break
end
term.write("It works")
end
end

Thank you
Dave-ee Jones #2
Posted 26 February 2017 - 10:55 PM
You mispelt textutils:


textutils.slowPrint("your text")

In your while loops you forgot a few things as well:

local bCheck = true
while bCheck do
  if turtle.getItemCount(1) == 1 then
    bCheck = false
  end
end

You may noticed I used a variable called bCheck. When I say "while bCheck do" it means "While bCheck is true do this:", so if you change bCheck to false during the while loop it stops the loop. I find it more reliable than 'while true do break'.

You can get away with doing this for sleeping:

sleep(3) -- Shorter than os.sleep(3)

There is no function called :

textutils.slowWrite()
So you'll have to use:

textutils.slowPrint()

If you want to enter the text on the same line you can do it the ugly way of manually setting the cursor position after it. Just requires either trial and error or a bit of maths. :)/>

You have a similar while loop problem down below. You can do the same as earlier with the new if statement to fix it.

That SHOULD work, but I am not too familiar with the turtle functions so I can't help there.
Happy to help :D/>
fishermedders #3
Posted 27 February 2017 - 02:08 AM
There is no function called :

textutils.slowWrite()
So you'll have to use:

textutils.slowPrint()

I was suprised to see this too, but there actually is a function called textutils.slowWrite()
KingofGamesYami #4
Posted 27 February 2017 - 02:17 AM
You may noticed I used a variable called bCheck. When I say "while bCheck do" it means "While bCheck is true do this:", so if you change bCheck to false during the while loop it stops the loop. I find it more reliable than 'while true do break'.

I see no reason break should be unreliable. Care to elaborate on which situations it does not work in?
Dave-ee Jones #5
Posted 27 February 2017 - 05:50 AM
You may noticed I used a variable called bCheck. When I say "while bCheck do" it means "While bCheck is true do this:", so if you change bCheck to false during the while loop it stops the loop. I find it more reliable than 'while true do break'.

I see no reason break should be unreliable. Care to elaborate on which situations it does not work in?

I've had some problems in the past where you have while loops going and you've got lots of if statements and break never really cut it for me. Just tended to cause problems for unknown reasons. Once I swapped to bRunning or whatever and using a variable to determine how long a loop runs for its much more simple. You can also use that variable to stop the loop in other parts of code as well, say calling a function that says

bRunning = false
and it will stop the loop from outside the loop (almost, technically it is still in the loop, but you catch ma drift). Cool little thing, especially if you wanted to make 2 or more while loops running parallel, one could stop the other. It's just useful imo.

For this purpose of code I guess it doesn't matter too much. Just a precaution in case you change the code up a bit and it needs a bit more modularity. :D/>

There is no function called :

textutils.slowWrite()
So you'll have to use:

textutils.slowPrint()

I was suprised to see this too, but there actually is a function called textutils.slowWrite()

Eh? I just looked up the textutils API and it wasn't listed (at least on the Computer's side of the API). Weird…

help textutils
that is.
supernicejohn #6
Posted 27 February 2017 - 10:56 AM
Well, one big thing is that using for example, bRunning, is that you could make the loop end from within a nested loop or a function. One other big thing is that 'break' stops the loop immediately, and there are certainly usages for both. Maybe there was some neccesary code after a 'break' that would cause the rest of the program to behave weirdly.
Personally I prefer a nice 'break'. ;)/>
Neekow #7
Posted 27 February 2017 - 02:44 PM
Well, as i'm a newbie, there are my way to try to have a neat and clear thing ^^:

- Using comments to "cut" my program in different steps:

  -- Init
	-- Color
	-- Variables
	-- Program
  -- Functions
  -- Program
  -- Ending


To know how to cut, i'm asking thing like: "What should happend at start but never again?"

For you, think should be something like that:

  -- Init
	-- Color
	-- Variables
	-- Program
	  term.clear()
	  term.setCursorPos(1,1)
	  term.setCursorBlink(true)
	  turtle.select(1)
	  textutiles.slowPrint("Welcome...please enter the WaterBukkit:")
  -- Functions
  -- Program
  -- Ending

As your while will only run if there isnt 1 item in slot 1, you can do
 while turtle.getItemCount(1) ~= 1 do 
and delete your if.

Beside having eStop = ioread() then an if
if io.read() == "stop" then
works well

As turtle.getItemCount(1) will look in 1st slot, turtle.select(1) isnt needed. actually you'r doing: "i'm selecting 1st slot then i count item in 1st slot"
but turtle.getItemCount(1) = "i count item in 1st slot"
this line can be deleted

io.read() will pauses the program, waiting an input, so the 2nd while isn't needed.

So … rewriten, looks like (note: its "textutils" not "textutiles" ^^):

  -- Init
	-- Colors
	-- Variables
	-- Program
	  term.clear()
	  term.setCursorPos(1,1)
	  term.setCursorBlink(true)
	  textutils.slowPrint("Welcome...please enter the WaterBukkit:")
  -- Functions
  -- Program
	while turtle.getItemCount(1) ~= 1 do
	  textutils.slowPrint("Start in 3 seconds!")
	  sleep(3)
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Program has startes")
	  textutils.slowWrite("Type stop to stop the program")
		if io.read() == "stop" then
		  break
		end
	end
  -- Ending
	term.write("It works")

Hope it will help =)