WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Need some logic help (https://www.wowinterface.com/forums/showthread.php?t=34624)

Sythalin 08-25-10 05:43 PM

Need some logic help
 
I thought I asked this once before, but my forum search/history turns up nothing.

lua Code:
  1. function CNames:ADDON_LOADED(event)
  2. ...
  3.     CNames_Options.enemy = CNames_Options.enemy or true
  4.     CNames_Options.friend = CNames_Options.friend or true
  5. ...
  6. end

When the addon loads up, .enemy and .friend are always true, even if the saved variable exists and is false. Where is my flaw in logic here?

Kjado 08-25-10 06:04 PM

true or true = true
false or true = true

The assignments will result in the same thing regardless of what enemy or friend are set to.

If they are intended to be boolean, try
Code:

CNames_Options.enemy = not (CNames_Options.enemy == false)

xConStruct 08-25-10 06:08 PM

Or explicitly check if the variable is 'nil' / not set.

Code:

if CNames_Options.enemy == nil then
    CNames_Options.enemy = true
end


Fodaro 08-26-10 09:43 AM

I would add that if you want to do what I think you want to do, you might want something like this instead of if statements:
lua Code:
  1. CNames_Options.enemy = (CNames_Options.enemy == nil) and true or CNames_Options.enemy
  2. CNames_Options.friend = (CNames_Options.friend == nil) and true or CNames_Options.friend

Sythalin 08-26-10 11:46 AM

Ok, now I'm more lost. Perhaps because I'm sick and not thinking straight at this time (I'm outta DayQuil ;))

lua Code:
  1. CNames_Options.enemy = (CNames_Options.enemy == nil) and true or CNames_Option.enemy

I think I'm understanding this wrong. From the looks, it looks like the first comparison is looking for nil and true, which is impossible as far as I know; Some cannot be true and not exist.

lua Code:
  1. CNames_Options.enemy = not (CNames_Options.enemy == false)

I'm not even sure how this works. Again, using terms of the boolean values, I read this as false = not false and true = not false. I think it's the formatting that I don't seem to understand.

lua Code:
  1. if CNames_Options.enemy == nil then
  2.     CNames_Options.enemy = true
  3. end
This is the format I'm used to, I've just been trying to eliminate the need for 10,000 if-then-else statements.

What I'm wondering at this point is if this is just the behaviour when using Boolean. Because from what I've used before CNames_Options = CNames_Options or {} works the way I'm thinking it would.

xConStruct 08-26-10 11:57 AM

Split them up and go through them step by step - note the parentheses!

Code:

CNames_Options.enemy = (CNames_Options.enemy == nil) and true or CNames_Option.enemy
Is basically the same as my if-conditional code, just inline.

(CNames_Options.enemy == nil)
- when true: true == nil --> false
- when false: false == nil --> false
- when nil: nil == nil --> true

So, if the result is true (only in case of nil == nil), CNames_Options.enemy is defined true (' and true'), otherwise it holds its previous value (' or CNCNames_Options.enemy').

-----
Same applies to
Code:

CNames_Options.enemy = not (CNames_Options.enemy == false)
(CNames_Options.enemy == false)
- when true: true == false --> false
- when false: false == false --> true
- when nil: nil == false --> false

The result is then inverted (NOT).
So, if your variable holds true/nil, the result of the comparison is 'false' (as you can see above) and your variable gets set to NOT false --> true.
And when 'false', comparison says 'true', so NOT makes it 'false' again.

-----
I prefer Fodaro's / my example, because they imho are more transparent to the reader.

Kjado 08-26-10 01:48 PM

Quote:

Originally Posted by ChaosInc (Post 203924)
What I'm wondering at this point is if this is just the behaviour when using Boolean. Because from what I've used before CNames_Options = CNames_Options or {} works the way I'm thinking it would.

The issue is largely the fact that the saved variable you are testing is storing a boolean, combined with your initialization test.

In your table example above, CNames_Options on the right-side of the = will only ever be ignored if it's nil; the behavior is very different if it's a bool, as even if it has a legitimate value, false will also be ignored and the variable will be set to true in your original code.

xConStruct 08-26-10 01:57 PM

You could also handle your SV-table defaults with some metatable magic. That's the way I normally do it:

Code:

local defaults = {
    enemy = true,
    friend = true,
}

function CNames:ADDON_LOADED(event)
    CNames_Options = CNames_Options or {}
    setmetatable(CNames_Options, { __index = defaults})
    ...
end

The metatable's __index works as following: if some value in your table is 'nil', it automatically provides the value in the default table. So, you would handle it exactly the same currently, but won't have to think lots about booleans when defining new default values :)


All times are GMT -6. The time now is 04:07 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI