Quantcast
Help with if and 'and not' - WoWInterface
Thread Tools Display Modes
10-27-16, 02:45 PM   #1
Krainz
An Aku'mai Servant
Join Date: Oct 2016
Posts: 32
Help with if and 'and not'

I had this other thread in the addon help section but it evolved to be more of a LUA problem so I'm posting it in here

Hi, I'm having trouble understanding the logic within 'if not' and 'and' in the same statement. Does that mean 'if not' AND 'if and not' or 'if not' AND 'if true'?

I need to understand that to merge these two codes:

Lua Code:
  1. if UnitAffectingCombat("Player")
  2.                 or InCombatLockdown()
  3.                 or ChatFrame1EditBox:IsShown()
  4.                 or WorldMapFrame:IsShown()
  5.                 or MailFrame:IsShown()
  6.                 or GossipFrame:IsShown()
  7.                 or GameTooltipTextLeft1:GetText()
  8.                 or UnitCastingInfo("Player")
  9.                 or UnitChannelInfo("Player")
  10.                 or UnitExists("Target") then
  11.                     addon:FadeIn();
  12.         end;
and
Lua Code:
  1. addon.timer = 0
  2.         addon:SetScript("OnUpdate", function(self, elapsed)
  3.                 self.timer = self.timer + elapsed
  4.                 local speed = GetUnitSpeed("player");
  5.                 local x, y = GetCursorPosition();
  6.                 if self.x ~= x
  7.                 or self.y ~= y then
  8.                     addon:FadeIn();
  9.                 elseif not IsMouselooking() then
  10.                     self.timer = 0
  11.                     addon:FadeOut();
  12.                 end;
  13.                 if self.timer > 5 then
  14.                     -- check mouse looking here since the cursor position is static while
  15.                     -- holding down right mouse button, also check that you're not mousing
  16.                     -- over a frame since that may not be desirable either
  17.                     if not IsMouselooking() and GetMouseFocus() == WorldFrame then
  18.                         self.x = x
  19.                         self.y = y
  20.                     end
  21.                    
  22.                     self.timer = 0
  23.                 end
  24.         end)

Putting unitaffectingcombat, incombatlockdown, and the likes together with 'self.x ~= x or self.y ~= y' seems to be a good implementation, but it isn't proper. The big chunk of if situations (the first code) must be together with 'not ismouselooking() and getmousefocus() == worldframe' because these in these two situations the mouse will be standing still and it's the same for all the situations in the first big chunk of code.

How do I do that? I tried just pasting everything after worldframe with lots of 'or's, didn't work, then changed all of them to 'and', which didn't work and ultimately changed to 'and not', and also didn't work.

What to do?
  Reply With Quote
10-27-16, 06:07 PM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Hi,

I'm not real sure what you mean. "if and not" is not valid code. not only applies to the immediately following expression, unless you use parentheses, in case that helps.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
10-27-16, 06:35 PM   #3
Krainz
An Aku'mai Servant
Join Date: Oct 2016
Posts: 32
Originally Posted by Lombra View Post
Hi,

I'm not real sure what you mean. "if and not" is not valid code. not only applies to the immediately following expression, unless you use parentheses, in case that helps.
if X and not Y

That doesn't help.
  Reply With Quote
10-27-16, 06:39 PM   #4
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 427
Lua Code:
  1. firstArgument = true
  2. secondArgument = false
  3.  
  4. if not (firstArgument and secondArgument) then print(1)
  5.     -- This is true, because the arguments in the
  6.     -- parenthesis are resolved first.
  7.     -- Since secondArgument is false, the overall outcome inside
  8.     -- the parenthesis will also be false.
  9.     -- Once it gets back outside the parenthesis, the statement is negated,
  10.     -- meaning it results in the reverse value of the parenthesis outcome.
  11. end
  12.  
  13. if not firstArgument and secondArgument then print(2)
  14.     -- This is false, because the keyword 'not' will in this case
  15.     -- negate the firstArgument, but has no effect on the secondArgument.
  16. end
  17.  
  18. if firstArgument and not secondArgument then print(3)
  19.     -- This is true, because the secondArgument will be negated into true,
  20.     -- and since firstArgument is also true, the overall outcome is true.
  21. end
  22.  
  23. if (not firstArgument and secondArgument) then print(4)
  24.     -- This is false, because it's not any different from the second
  25.     -- example. Since nothing affects the if-case outside the
  26.     -- parenthesis, it'll result in the same outcome.
  27. end
  28.  
  29. if not (not firstArgument and secondArgument) then print(5)
  30.     -- This is true, because the parenthesis returns false, which is then
  31.     -- negated into true. firstArgument is negated into false, consolidated
  32.     -- with secondArgument into false again, and then negated into true.
  33. end
  34.  
  35. if firstArgument or secondArgument then print(6)
  36.     -- This is true, because the firstArgument is true.
  37.     -- If either argument is true when using 'or', it'll result in true,
  38.     -- even if one or more arguments are false.
  39. end
  40.  
  41. if secondArgument or firstArgument then print(7)
  42.     -- This is true all the same, because even though secondArgument is
  43.     -- checked first, firstArgument is still true and thus the outcome is true.
  44. end
  45.  
  46. if secondArgument or not firstArgument then print(8)
  47.     -- This is false, because both arguments will be false.
  48.     -- secondArgument is already false, and firstArgument is negated into false,
  49.     -- as explained in the second example.
  50. end
  51.  
  52. if ( firstArgument and not secondArgument) or (not firstArgument and secondArgument) then print(9)
  53.     -- The first parenthesis results in true, the second parenthesis results in false.
  54.     -- Since either argument will do, because we're using 'or', the result is true.
  55. end
__________________
  Reply With Quote
10-27-16, 09:16 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,732
http://lua-users.org/wiki/OperatorsTutorial
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
10-28-16, 04:38 AM   #6
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 928
Originally Posted by Krainz View Post
Hi, I'm having trouble understanding the logic within 'if not' and 'and' in the same statement.
Does that mean 'if not' AND 'if and not' or 'if not' AND 'if true'?

You can test it in the Live demo too
http://www.lua.org/cgi-bin/demo

Basically what MunkDev already described
Lua Code:
  1. if not IsMouselooking() and GetMouseFocus() == WorldFrame then
  2.     self.x = x
  3.     self.y = y
  4. end
Lua Code:
  1. not false and false -- false
  2. not true and false -- false
  3. not false and true -- true
  4. not true and true -- false
Truth table: not x and y
Code:
x	|	 xy
false	|	true		false	→	false
true	|	false		false	→	false
false	|	true		truetrue
true	|	false		true	→	false

Last edited by Ketho : 10-28-16 at 04:49 AM.
  Reply With Quote
10-29-16, 02:39 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,924
Something standing out to me is the check for GameTooltipTextLeft1:GetText(). In most if not all situations, I'd think this would always be returning something that would evaluate to true. I wouldn't expect the tooltip to actually clear the text when it hides, just when it's called to show something else. You might want to check GameTooltip:IsShown() instead.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
10-29-16, 04:27 AM   #8
Jarod24
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2012
Posts: 66
1. When in doubt; use parantesis:

Code:
IF a and b or c and not d...

IF (a and b) or (c and (not d))...

IF (a) and (b) or (c) and (not d)...

2. You can always use nesting of IF's.

Code:
IF a and b 
    IF c 
      ...
    ElseIF not d
      ...
   end
end
My main thing is always to make the code readable and understandable for yourself (and others). Imagine coming back to the code 6 months from now when you dont remember anything about why it's there. Making it super-condenced most of the time just makes code convoluted, hard to understand and hard to maintain.


LUA uses the same approach as most other languages when dealing with AND/OR: short-circuiting inclusive logical disjunction on two expressions. In short this means that it will skip the evaluation of the second part of an AND/OR expression if the first one is true.

You can learn more about that by reading here; This is for VB.net that uses the opposite approach for AND/OR but has additional keywords (ANDALSO, ORELSE) that works like AND/OR in other languages.
OrElse: https://msdn.microsoft.com/en-us/library/ea1sssb2.aspx
AndAlso: https://msdn.microsoft.com/en-us/library/cb8x3kfz.aspx
__________________
Author of IfThen, Links in Chat
  Reply With Quote
10-29-16, 04:33 AM   #9
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Lua (NOT LUA) is case-sensitive, so your examples of "IF" and "ElseIF" are a bit misleading. Likewise, showing examples of completely unrelated boolean logic for a semantically different language is... baffling.

There should be no "when in doubt" in this scenario; indeed, "Learn the language rules" should be the message.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with if and 'and not'

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off