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

[Lua] Detection of click algorithm bug

Started by bjornir90, 27 November 2012 - 04:31 AM
bjornir90 #1
Posted 27 November 2012 - 05:31 AM
Hello everyone !
I have tried to do a mouse/graphic API but I still blocked at the click detection algorithm. Here the code I use :

function buttonClick(x, y, a, B)/>/> 
 clic, x1, y1 = os.pullEvent("mouse_click") 
 for b=b, 1 do 
  if x1<= a-b then 
   for a=a, 1 do 
    if y1 <= b-a then 
     test = "true" 
    else 
      a = a-1 
     end 
    end 
   else 
    b = b-1 
   end 
  end 
 if test == "true" then 
  for y=y, 1 do 
   if x1 >= x-y then 
    for x=x, 1 do 
     if y1 >= y-x then 
      clicked = "true" 
      return clicked 
     else 
      x = x+1 
     end 
    end 
   else 
    y = y+1 
   end 
  end 
 end 
When I click on the button, it doesn't detect anything.
PixelToast #2
Posted 27 November 2012 - 05:40 AM
when you do "for var=number over max,max do"
it will not run anything after do
bjornir90 #3
Posted 27 November 2012 - 05:42 AM
when you do "for var=number over max,max do"
it will not run anything after do
Why ? It's not the use of the for loops ?
bjornir90 #4
Posted 27 November 2012 - 05:54 AM
Well I found the error thanks you!
PixelToast #5
Posted 27 November 2012 - 06:51 AM
when you do "for var=number over max,max do" it will not run anything after do
Why ? It's not the use of the for loops ?
yea, for loops wont run entirely if the min is greater than the max
same thing for string.sub
string.sub("hello",3,2) will return a empty string
bjornir90 #6
Posted 27 November 2012 - 07:05 AM
when you do "for var=number over max,max do" it will not run anything after do
Why ? It's not the use of the for loops ?
yea, for loops wont run entirely if the min is greater than the max
same thing for string.sub
string.sub("hello",3,2) will return a empty string
Yeah, how can I have even try to do this ? I'm so idiot !
GopherAtl #7
Posted 27 November 2012 - 08:05 AM
if you actually wanted to iterate the other way, say from 10 down to 1, you could use the optional third value for for, which is the step




--countdown

for i=10,1,-1 do

  print(i.."...")

end

print("boom")



You may have found this already, but it should be noted that where your for loops reuse variable names that exist outside the loops, the behavior may not be what you would expect, ex..





local i=1

for i=i,10 do --this WILL loop from 1 to 10...

  ...

end

--i will have reverted, as the loop was using a new, local variable "i" which ceased to exist when you exited the loop

print(i) -- will print "1"!



Also note, modifying the loop variable inside the loop has NO effect at all. This code will print all the numbers from 1 to 10, the i=i+1 inside the loop will not affect the next pass through the loop's initial value of i



for i=1,10 do

  print(i)

  i=i+1

end

bjornir90 #8
Posted 27 November 2012 - 08:30 AM
if you actually wanted to iterate the other way, say from 10 down to 1, you could use the optional third value for for, which is the step




--countdown

for i=10,1,-1 do

  print(i.."...")

end

print("boom")



You may have found this already, but it should be noted that where your for loops reuse variable names that exist outside the loops, the behavior may not be what you would expect, ex..





local i=1

for i=i,10 do --this WILL loop from 1 to 10...

  ...

end

--i will have reverted, as the loop was using a new, local variable "i" which ceased to exist when you exited the loop

print(i) -- will print "1"!



Also note, modifying the loop variable inside the loop has NO effect at all. This code will print all the numbers from 1 to 10, the i=i+1 inside the loop will not affect the next pass through the loop's initial value of i



for i=1,10 do

  print(i)

  i=i+1

end

Thanks you alot ! I did not even heard about the third variable in a for loop XD And about the i=i+1 thing it's because I need this value updated in the rest of my program. (I didn't post the code entirely because you can find it in the API subforum.)
GopherAtl #9
Posted 27 November 2012 - 08:55 AM
That's my point, looping with a variable x makes any other variable x outside the loop completely inaccessible to the code inside the loop. You'd need to use different variable names for the loop for this to work.