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 on 26 November 2014 - 12:47 AM
3057 posts
Location
United 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?
797 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 on 26 November 2014 - 12:59 AM
1023 posts
Posted 26 November 2014 - 02:00 AM
-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 on 26 November 2014 - 01:01 AM
74 posts
Posted 26 November 2014 - 02:43 AM
-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 :)/>
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!
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 on 26 November 2014 - 01:44 AM
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")
797 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 on 26 November 2014 - 01:59 AM