Jump to content




Help using return


  • You cannot reply to this topic
11 replies to this topic

#1 Chain99

  • Members
  • 30 posts

Posted 01 April 2014 - 05: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.

#2 CometWolf

  • Members
  • 1,283 posts

Posted 01 April 2014 - 06: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 Cranium, 01 April 2014 - 01:31 PM.
Language


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 April 2014 - 07: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

View PostCometWolf, on 01 April 2014 - 06:48 AM, said:

Your indentation is way [redacted] up lol.
Language CometWolf, you should know better!

#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 01 April 2014 - 01:32 PM

Indeed. Keep it clean, folks. I don't want to start washing out mouths. This is a family friendly forum.

#5 Chain99

  • Members
  • 30 posts

Posted 01 April 2014 - 02:33 PM

View PostCometWolf, on 01 April 2014 - 06:48 AM, said:

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.

#6 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

Posted 01 April 2014 - 03: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. ;)

#7 Chain99

  • Members
  • 30 posts

Posted 02 April 2014 - 02:35 AM

View PostJiloacom, on 01 April 2014 - 03:01 PM, said:

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 by Lyqyd, 02 April 2014 - 04:05 AM.


#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 02 April 2014 - 04:05 AM

Any time the code is changed, you should post the full code in its updated state.

#9 Chain99

  • Members
  • 30 posts

Posted 02 April 2014 - 05: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


#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 April 2014 - 07: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.

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2014 - 07:21 AM

View PostChain99, on 02 April 2014 - 05:28 AM, said:

-snip-

Take a look at the code I suggested after CometWolf to see a working example of what Bomb Bloke said.

View Posttheoriginalbit, on 01 April 2014 - 07:50 AM, 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


#12 Chain99

  • Members
  • 30 posts

Posted 03 April 2014 - 06:46 AM

View PostBomb Bloke, on 02 April 2014 - 07:16 AM, said:

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!





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users