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

My first API

Started by mrpoopy345, 03 January 2014 - 06:40 AM
mrpoopy345 #1
Posted 03 January 2014 - 07:40 AM
Hello! I was trying to make an API, my first one. What is basically does is create a square of a certain color, height, and length on the screen. I've checked the logistics of it multiple times,
and I think it is a problem with the outline of the API. When I run "square", nothings happens.
This is an example function I am using.

square("red" 1, 1, 5, 5)
This is the actual API's code.

function square(color, ox, oy, tx, ty)
 term.setBackgroundColor(colors[color])
 term.setCursorPos(ox, oy)
 h = oy - ty
 x = tx - ox
 for i = 1, h do
  x, y = term.getCursorPos()
  y = y + 1
  term.setCursorPos(x, y)
  for i = 1, x do
   term.write(" ")
  end
 end
end
Any help?
6677 #2
Posted 03 January 2014 - 08:27 AM
Thats a function, not an API.

Line 4. h = oy - ty that will return a negative number with the parameters you supplied and therefore wont enter the for loop as stands. Swap for h = ty - oy fixes that error. Easy mistake, we all make it.


Second issue.line 5: x = tx - ox. Not an issue on its own. But on line 7 you have x, y = term.getCursorPos(). You've overwritten x unintentionally. Changing line 5 for "w = tx - ox" and line 10 for "for i = 1, w do" fixes this. Again, small easily overlookable mistake.

Final issue. You never reset the cursor in the X axis, so each subsequent line is then drawn off centre. Easy to fix, I'll let you have a go at that one rather than serving solutions up on a plate immediately, got to learn somehow. Actually gives a rather nice diagonal line btw, just not one with logical inputs for actually giving any control over where it goes :P/>
Edited on 03 January 2014 - 07:28 AM
theoriginalbit #3
Posted 03 January 2014 - 09:37 AM
Another point of improvement would be to completely replace the second for loop, the x-axis one, with a single term.write and string.rep


term.write(string.rep(' ', x))

of course that's inefficient to build identical strings each time, so really you should just build it once into a variable and write that out each time.

lastly, don't force people into giving you a string for the colour, use the colours API, or just allow both and perform a check as to whichever they've given you.

oh almost forgot, you should also localise your variables.
mrpoopy345 #4
Posted 03 January 2014 - 09:46 AM
I followed your guys' advice and got it to work! But I have two questions for theoriginalbit.
How would I know what color they want without them telling me?
And what is the difference between local and global variables?
TheOddByte #5
Posted 03 January 2014 - 11:16 AM
I followed your guys' advice and got it to work! But I have two questions for theoriginalbit.
How would I know what color they want without them telling me?
And what is the difference between local and global variables?
For the colors you should use colors.<color>
Example

term.setTextColor( colors.white )

And the difference between globals and locals are that for example if a function or variable is global it can be used from any programs, But if it's local then it can only be used in that program, If you localize something in a function, loop or a statement then it's local to that and can only be used in that.

if true then
    local text = "Hello" -- text is local to this statement
end
print( text ) -- Will print nil
Edited on 03 January 2014 - 10:19 AM