Need some logic help
I thought I asked this once before, but my forum search/history turns up nothing.
lua Code:
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? |
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) |
Or explicitly check if the variable is 'nil' / not set.
Code:
if CNames_Options.enemy == nil then |
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:
|
Ok, now I'm more lost. Perhaps because I'm sick and not thinking straight at this time (I'm outta DayQuil ;))
lua Code:
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:
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:
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. |
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 (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) - 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. |
Quote:
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. |
You could also handle your SV-table defaults with some metatable magic. That's the way I normally do it:
Code:
local defaults = { |
All times are GMT -6. The time now is 04:07 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI