WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Help with if and 'and not' (https://www.wowinterface.com/forums/showthread.php?t=54740)

Krainz 10-27-16 02:45 PM

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?

Lombra 10-27-16 06:07 PM

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.

Krainz 10-27-16 06:35 PM

Quote:

Originally Posted by Lombra (Post 320397)
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.

MunkDev 10-27-16 06:39 PM

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

Seerah 10-27-16 09:16 PM

http://lua-users.org/wiki/OperatorsTutorial

Ketho 10-28-16 04:38 AM

Quote:

Originally Posted by Krainz (Post 320392)
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        |        x        ∧        y
false        |        true                false        →        false
true        |        false                false        →        false
false        |        true                true        →        true
true        |        false                true        →        false


SDPhantom 10-29-16 02:39 AM

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.

Jarod24 10-29-16 04:27 AM

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

Torhal 10-29-16 04:33 AM

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.


All times are GMT -6. The time now is 08:09 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI