Jump to content




For loops


6 replies to this topic

#1 ShadowDisruptor

  • Members
  • 74 posts

Posted 26 November 2014 - 01:46 AM

I have a function and I can't seem to figure out why it refuses to work. I've used for loops in Java but never Lua, so the way it's done is a bit strange to me. I haven't gotten any errors, just nothing happens

Here is the code:
function createBar(x,y,height,width,color)
  --Please note, mon is correctly called in when this is used.
  --Getcolor is also not defined here, just returns colors.red at the moment
  mon.setBackgroundColor(getColor(color))
  for i=y-1,height do
	for i=0,width do
	  mon.setCursorPos(i,x+i)
	  mon.write(" ")
	end
  end
end

Here is how I'm calling it:
createBar(15,15,5,2,"color")

Edited by ShadowDisruptor, 26 November 2014 - 01:47 AM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 26 November 2014 - 01:53 AM

Well, first thing I noticed is you both loops using the 'i' variable. Are you referencing different ones in your mon.setCursorPos?

#3 Exerro

  • Members
  • 801 posts

Posted 26 November 2014 - 01:58 AM

Only the variable i from the second for loop (for i=0,width do) will be used in the next section of code. I'm guessing you want to refer to the first "i" for the y position, and the second "i" for the x position, so this isn't working because it is only referring to the second "i".

On a side note, having 2 loops is unnecessary. You can use string.rep() to create a line of spaces, and draw that, rather than drawing each space individually.

Edit: it's mon.setCursorPos( x, y ), not mon.setCursorPos( y, x ) which it looks like you are trying to use.

Edited by awsumben13, 26 November 2014 - 01:59 AM.


#4 valithor

  • Members
  • 1,053 posts

Posted 26 November 2014 - 02:00 AM

View PostShadowDisruptor, on 26 November 2014 - 01:46 AM, said:

-snip

How tall is the monitor you are using (how many blocks)

where you are actually writing stuff to the monitor for the y coord you use x+i for the y coord. Your problem might be that you are writing off the screen.

edit: awsumben13 beat me to it. :ph34r:'d

Edited by valithor, 26 November 2014 - 02:01 AM.


#5 ShadowDisruptor

  • Members
  • 74 posts

Posted 26 November 2014 - 02:43 AM

View Postvalithor, on 26 November 2014 - 02:00 AM, said:

View PostShadowDisruptor, on 26 November 2014 - 01:46 AM, said:

-snip

How tall is the monitor you are using (how many blocks)

where you are actually writing stuff to the monitor for the y coord you use x+i for the y coord. Your problem might be that you are writing off the screen.

edit: awsumben13 beat me to it. :ph34r:'d

Monitor is already big enough, ruled that out early. Thanks for the reply :)

View PostKingofGamesYami, on 26 November 2014 - 01:53 AM, said:

Well, first thing I noticed is you both loops using the 'i' variable. Are you referencing different ones in your mon.setCursorPos?

Got that one already too, it's fixed in my in-game code but not on the source. Thanks for the answer!

View Postawsumben13, on 26 November 2014 - 01:58 AM, said:

Only the variable i from the second for loop (for i=0,width do) will be used in the next section of code. I'm guessing you want to refer to the first "i" for the y position, and the second "i" for the x position, so this isn't working because it is only referring to the second "i".

On a side note, having 2 loops is unnecessary. You can use string.rep() to create a line of spaces, and draw that, rather than drawing each space individually.

Edit: it's mon.setCursorPos( x, y ), not mon.setCursorPos( y, x ) which it looks like you are trying to use.

Fixed the x and y. Thanks for the answer!

Edited by ShadowDisruptor, 26 November 2014 - 02:44 AM.


#6 ShadowDisruptor

  • Members
  • 74 posts

Posted 26 November 2014 - 02:54 AM

Still doesn't work; nothing happens at all.

Updated code (Full length):

function getColor(input)
  color = colors.red
  return color
end

function setMonitor(side)
  mon = peripheral.wrap(side)
end

function createBar(x,y,height,width,color)
  mon.setBackgroundColor(getColor(color))
  for i=y-1,height do
     mon.setCursorPos(x,y+i)
     mon.write(string.rep(" ",width))
  end
end

setMonitor("top")
createBar(15,15,5,2,"color")


#7 Exerro

  • Members
  • 801 posts

Posted 26 November 2014 - 02:56 AM

Ah, it's because of the variables you're using in the for loop.
y is 15, height is 5
You're running a loop from y-1 (14) to height (5), but 14 is already far greater than 5, so it never runs the code in the loop.
Make it iterate from 1 to height, and then use y + i - 1 as the y coordinate when you set the cursor position.

Edit:
for i = 1, height do
   mon.setCursorPos( x, y + i - 1 )
   -- ...
end

Edited by awsumben13, 26 November 2014 - 02:59 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users