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

Why won't this work?

Started by thethirdlore, 13 January 2015 - 01:51 AM
thethirdlore #1
Posted 13 January 2015 - 02:51 AM

local i = 1
local y = 5
local w = term.getSize()

function desktop()
term.clear()
		term.setCursorPos(1,19)
print("Start")
for y=6, w do
   paintutils.drawPixel(y,19,2048)
   y = y+1
end
term.setCursorPos(1,25)
end

for some reason when i run this it prints "Start" a line above where i want it to. i have tried printing it to line 20 but it just disappears, can someone please tell me how to get it to print on the bottom line?
Edited by
Quintuple Agent #2
Posted 13 January 2015 - 03:29 AM
Ok, the problem here is that you are using print("start"). What print does is it writes the line, then goes to the next line, since the next line is lower then the bottom of the screen, the computer moves everything 1 line up, line 19 where you printed "start" is now line 18 and the new line 19 is where you draw your line in that loop.

use term.write("Start") instead.
valithor #3
Posted 13 January 2015 - 03:49 AM
-snip

Your problem here is actually the use of print. Print is a function that utilizes the write function, but adds a new line character at the end. Because of the new line character the print is placed, but a blank line is placed underneath it, which is caused by the cursor pos moving when you use print. Change the print to write("Start") and it will print it where you want it to.

Btw it is usually not good to add to your counting variable in a for loop. By default it will add 1 to itself with each iteration automatically, but this can be changed to get similar results as what you are doing with the y=y+1.

for i = 2, 10, 2 do
  print(i)
end
This will print 2, 4, 6, 8, 10
The format for using this would be for i = (starting num), (ending num), (iterator) do, where starting num is the number you start at, ending num is one you end at, and iterator is the amount you increase by each iteration.

A normal for loop is as follows
for i = 1, 5 do
  print(i)
end
This will print 1, 2, 3, 4, 5

edit: added example
Edited on 13 January 2015 - 03:00 AM
Quintuple Agent #4
Posted 13 January 2015 - 03:51 AM
and i was beat too it due to the moderator approval restriction. :(/>
thethirdlore #5
Posted 13 January 2015 - 04:11 AM
-snip

Your problem here is actually the use of print. Print is a function that utilizes the write function, but adds a new line character at the end. Because of the new line character the print is placed, but a blank line is placed underneath it, which is caused by the cursor pos moving when you use print. Change the print to write("Start") and it will print it where you want it to.

Btw it is usually not good to add to your counting variable in a for loop. By default it will add 1 to itself with each iteration automatically, but this can be changed to get similar results as what you are doing with the y=y+1.

for i = 2, 10, 2 do
  print(i)
end
This will print 2, 4, 6, 8, 10
The format for using this would be for i = (starting num), (ending num), (iterator) do, where starting num is the number you start at, ending num is one you end at, and iterator is the amount you increase by each iteration.

A normal for loop is as follows
for i = 1, 5 do
  print(i)
end
This will print 1, 2, 3, 4, 5

edit: added example
that worked, thanks :D/>
First_One #6
Posted 13 January 2015 - 04:12 AM
Just as an additional

The line y = y+1 is not needed cause the y is not used afterwards…

On the wiki:

for Variable = Start, End, Interval do
	 --Insert code here
end

As mentioned by valithor, the Interval is by default 1 and can be omitted. That's why the loop works fine without that line.

Also Important
Note that the loop only inspects Start, End, and Interval once - when the loop starts - so if their values change while the loop is running, it'll ignore this and loop according to as they were when it it begun.

so:

y=y+1
is the same as

y=y+9001
(in that case)
thethirdlore #7
Posted 13 January 2015 - 06:12 AM
thanks for all the replies, i got it working :D/>