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

Help using return

Started by Chain99, 01 April 2014 - 04:16 AM
Chain99 #1
Posted 01 April 2014 - 06:16 AM
Hi, I've been trying to write a API in which you input the desired xminimum, maximum, yminimum, maximum and it is supposed to return either c1 for left click or c2 for right click. When I run this program I receive no errors and now value is returned with the function is called as a variable. Here is my code

function touch(xmin,xmax,ymin,ymax)
local b,e,x,y=os.pullEvent("mouse_click")
if e=="mouse_click" then
if b=="1" then
    if x >= xmin and x <= xmax and y >= ymin and y <= ymax then
	   return "c1"
	   elseif
	    e=="mouse_click" then
		  if b=="2" then
		    if x>=xmin and x<=xmax and y>=ymin and y<=ymax then
		    return "c2"
		   else
		   end
		 end
	  end	 
    end
  end
end
and here is my code http://pastebin.com/SahWFJ4W
Please help me if you find the time or redirect me to the proper thread if I made a mistake by posting.
-Best Regards
Chain

Sorry pasting this from pastebin screwed up the spacing.
CometWolf #2
Posted 01 April 2014 - 07:48 AM

function touch(xmin,xmax,ymin,ymax)
  local b,e,x,y=os.pullEvent("mouse_click")
  if e=="mouse_click" then
	if b=="1" then --this will never happen
	  if x >= xmin and x <= xmax and y >= ymin and y <= ymax then
		 return "c1"
	   elseif e=="mouse_click" then
		 if b=="2" then
		   if x>=xmin and x<=xmax and y>=ymin and y<=ymax then
			 return "c2"
		   end
		 end
	  end	  
	end
  end
end
Your indentation is way [messed] up lol. Anyways the first return value from os.pullEvent is the event name. In this case, that would be "mouse_click", not 1 :P/>
Edited by
theoriginalbit #3
Posted 01 April 2014 - 08:50 AM
here is a slightly cleaner approach to your problem, note the cleaner conditionals for the if statements

function touch(xmin,xmax,ymin,ymax)
  local e,b,x,y = os.pullEvent("mouse_click") --# switched e and b around... this is why meaningful variable names are important
  if e == "mouse_click" then
    if x >= xmin and x <= xmax and y >= ymin and y <= ymax then --# check where they've clicked
      if b == 1 then --# if it was left mouse click
        return "c1"
      elseif b == 2 then --# if ti was right mouse click
        return "c2"
      end
    end
  end
end

however it could be further improved like so

function touch(xmin,xmax,ymin,ymax)
  local e,b,x,y = os.pullEvent("mouse_click")
  if e == "mouse_click" then
    if x >= xmin and x <= xmax and y >= ymin and y <= ymax then
      return "c"..tostring(B)/> --# add the button number to the end of "c", this will work for all mouse buttons now without updating the logic
    end
  end
end
as the comment in the code says, all mouse buttons will work without having to update the logic in the function

Your indentation is way [redacted] up lol.
Language CometWolf, you should know better!
Cranium #4
Posted 01 April 2014 - 02:32 PM
Indeed. Keep it clean, folks. I don't want to start washing out mouths. This is a family friendly forum.
Chain99 #5
Posted 01 April 2014 - 03:33 PM

function touch(xmin,xmax,ymin,ymax)
  local b,e,x,y=os.pullEvent("mouse_click")
  if e=="mouse_click" then
	if b=="1" then --this will never happen
	  if x >= xmin and x <= xmax and y >= ymin and y <= ymax then
		 return "c1"
	   elseif e=="mouse_click" then
		 if b=="2" then
		   if x>=xmin and x<=xmax and y>=ymin and y<=ymax then
			 return "c2"
		   end
		 end
	  end	  
	end
  end
end
Your indentation is way [messed] up lol. Anyways the first return value from os.pullEvent is the event name. In this case, that would be "mouse_click", not 1 :P/>/>
Thanks! I knew it would be a simple error in the variables, anyways helped a lot thanks.
Saldor010 #6
Posted 01 April 2014 - 04:01 PM
Also, for future reference, I suggest adding a print statement on all of your conditionals for debugging purposes, such as this.


if x == true then
    print("This part works!")
else
    print("This other part also works!")
end

For instance, in your scenario, it was never reaching that point in your code, and with this tip, you would have been able to figure out the problem faster. ;)/>
Chain99 #7
Posted 02 April 2014 - 03:35 AM
Also, for future reference, I suggest adding a print statement on all of your conditionals for debugging purposes, such as this.


if x == true then
	print("This part works!")
else
	print("This other part also works!")
end

For instance, in your scenario, it was never reaching that point in your code, and with this tip, you would have been able to figure out the problem faster. ;)/>/>
I actually had a ton of debugging code in there but removed because it made the code hard to read

Ok, now the variable issue is sorted but it still doesn't return a value.
Edited on 02 April 2014 - 03:05 AM
Lyqyd #8
Posted 02 April 2014 - 05:05 AM
Any time the code is changed, you should post the full code in its updated state.
Chain99 #9
Posted 02 April 2014 - 06:28 AM

function touch(xmin,xmax,ymin,ymax)
local e,b,x,y=os.pullEvent("mouse_click")
if e=="mouse_click" then
if b=="1" then
	if x >= xmin and x <= xmax and y >= ymin and y <= ymax then
	   return "c1"
	   elseif
		e=="mouse_click" then
		  if b=="2" then
			if x>=xmin and x<=xmax and y>=ymin and y<=ymax then
			return "c2"
		   else
		   end
		 end
	  end	
	end
  end
end
Bomb Bloke #10
Posted 02 April 2014 - 08:16 AM
if b=="1" then

Mouse click events return the buttons used as "number"-types, not as strings. Try:

if b==1 then

Same fix goes for when you check for a right-click.

You can also remove the second check for a mouse click event (use a straight up "else" instead of an "elseif"), as by that point in the code it's already been established what sort of event you've got. Finally, you'll want to shuffle your checks around a bit - currently you only check to see if that right button is used, if the left button has already been identified as used… That's a circumstance that'll never occur.
theoriginalbit #11
Posted 02 April 2014 - 08:21 AM
-snip-

Take a look at the code I suggested after CometWolf to see a working example of what Bomb Bloke said.
here is a slightly cleaner approach to your problem, note the cleaner conditionals for the if statements

function touch(xmin,xmax,ymin,ymax)
  local e,b,x,y = os.pullEvent("mouse_click") --# switched e and b around... this is why meaningful variable names are important
  if e == "mouse_click" then
	if x >= xmin and x <= xmax and y >= ymin and y <= ymax then --# check where they've clicked
	  if b == 1 then --# if it was left mouse click
		return "c1"
	  elseif b == 2 then --# if ti was right mouse click
		return "c2"
	  end
	end
  end
end

however it could be further improved like so

function touch(xmin,xmax,ymin,ymax)
  local e,b,x,y = os.pullEvent("mouse_click")
  if e == "mouse_click" then
	if x >= xmin and x <= xmax and y >= ymin and y <= ymax then
	  return "c"..tostring(B)/>/> --# add the button number to the end of "c", this will work for all mouse buttons now without updating the logic
	end
  end
end
as the comment in the code says, all mouse buttons will work without having to update the logic in the function
Chain99 #12
Posted 03 April 2014 - 07:46 AM
if b=="1" then

Mouse click events return the buttons used as "number"-types, not as strings. Try:

if b==1 then

Same fix goes for when you check for a right-click.

You can also remove the second check for a mouse click event (use a straight up "else" instead of an "elseif"), as by that point in the code it's already been established what sort of event you've got. Finally, you'll want to shuffle your checks around a bit - currently you only check to see if that right button is used, if the left button has already been identified as used… That's a circumstance that'll never occur.
Thanks, everything works fine now!