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

Button/Rectangle Creating function doesn't work correctly

Started by n1ghtk1ng, 08 January 2013 - 05:32 PM
n1ghtk1ng #1
Posted 08 January 2013 - 06:32 PM
I made a function that would spawn a button, given the length, height, coordinates(x & y), and so on.
I've been doing this for a few hours, and I still have at least, two bugs:
-If the y-coordinate is greater than or equal to the height, then the whole screen gets filled with the chosen
-If all of the variables (length, width, x-coord) except the y-coord are equal, then it only prints one line, and goes on forever, until it returns "too long without yielding" (I know this is because I don't have a sleep(?)

Please test this out and help me with the bugs. Although this isn't that big of a project nor alot of code, I've never really had to identify bugs and fix them, nor think this hard about (a) project/code


link:http://pastebin.com/GmhH4ABX

Thanks!
theoriginalbit #2
Posted 08 January 2013 - 06:37 PM
I would suggest that the easiest thing would be to add in a bunch of validation and return an error if something is wrong.

Off topic side note: I think that for the click functions and such it may be easier now if you implement it that the program 'registers' a button that is then stored in a table. which then can be used by draw and clicked

EDIT: Here is a small example of what I mean ( this was typed in pastebin, so sorry if there are some typos) :
http://pastebin.com/JMeTqWNc
If you need me to explain anything here, just ask.
Edited on 08 January 2013 - 06:08 PM
n1ghtk1ng #3
Posted 09 January 2013 - 01:31 AM
Whoops…
n1ghtk1ng #4
Posted 09 January 2013 - 01:35 AM
I would suggest that the easiest thing would be to add in a bunch of validation and return an error if something is wrong.

Off topic side note: I think that for the click functions and such it may be easier now if you implement it that the program 'registers' a button that is then stored in a table. which then can be used by draw and clicked

EDIT: Here is a small example of what I mean ( this was typed in pastebin, so sorry if there are some typos) :
http://pastebin.com/JMeTqWNc
If you need me to explain anything here, just ask.


A have a few questions,
What does "unpack", "math.floor", "string.rep", and "_" do?
Also, it returns an error; 38:Attempt to get length of number (meaning #info), also you were missing an "=" on line 26( if height == math.floor(v[2]/2) )

Thanks for the help!
theoriginalbit #5
Posted 09 January 2013 - 01:42 AM
Oh yes oops at least it was only 2, quick typing and pastebin don't make for good code… You can remove the unpack now. that was one of the last changes I made, its not needed. unpack actually puts all the values in a table into their own variable. so we would do say this
local x, y, z = unpack( tTable )
where tTable is say { 1, 6, 13 }
removing the unpack should remove the error on line 38
math.floor rounds a number down. so say I give it 7.3 it will make it 7 or if i give it 7.8 it will still give me 7. math.ceil is rounding up :)/>
as for the _ thats just a coding habit of mine. you could have any identifier for a variable there that you wish, something like k ( meaning 'key' ). I use _ for anything that I'm not actually using, just so when I read through I can quickly tell. :)/> I have seen others actually use the _ though, but thats bad naming convention.
n1ghtk1ng #6
Posted 09 January 2013 - 01:47 AM
Oh yes oops at least it was only 2, quick typing and pastebin don't make for good code… You can remove the unpack now. that was one of the last changes I made, its not needed. unpack actually puts all the values in a table into their own variable. so we would do say this
local x, y, z = unpack( tTable )
where tTable is say { 1, 6, 13 }
removing the unpack should remove the error on line 38
math.floor rounds a number down. so say I give it 7.3 it will make it 7 or if i give it 7.8 it will still give me 7. math.ceil is rounding up :)/>
as for the _ thats just a coding habit of mine. you could have any identifier for a variable there that you wish, something like k ( meaning 'key' ). I use _ for anything that I'm not actually using, just so when I read through I can quickly tell. :)/> I have seen others actually use the _ though, but thats bad naming convention.
37:Attempt to get length of nil ; Where is the table "info" made?(didn't know how to describe it)
theoriginalbit #7
Posted 09 January 2013 - 01:51 AM
sorry that was bad communication on my part do one of the following

Change this
local info = unpack( button )
to this
local info = button

OR
remove this line
local info = unpack( button )
and replace all the 'info' in that function to 'button'