Thread Tools Display Modes
08-25-10, 05:43 PM   #1
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
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?
  Reply With Quote
08-25-10, 06:04 PM   #2
Kjado
A Deviate Faerie Dragon
 
Kjado's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 10
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)
__________________
My projects - Primal Defense | EchoPort | QuickTradeskill
  Reply With Quote
08-25-10, 06:08 PM   #3
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
Or explicitly check if the variable is 'nil' / not set.

Code:
if CNames_Options.enemy == nil then
    CNames_Options.enemy = true
end
__________________
« Website | GitHub »

Oh hai!
  Reply With Quote
08-26-10, 09:43 AM   #4
Fodaro
A Cyclonian
 
Fodaro's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 42
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
__________________
Fodaro
(Main: Fodaro-Bronzebeard (EU))
Author of CharNote and Consolid8
  Reply With Quote
08-26-10, 11:46 AM   #5
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
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.
  Reply With Quote
08-26-10, 11:57 AM   #6
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
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.
__________________
« Website | GitHub »

Oh hai!

Last edited by xConStruct : 08-26-10 at 12:00 PM.
  Reply With Quote
08-26-10, 01:48 PM   #7
Kjado
A Deviate Faerie Dragon
 
Kjado's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 10
Originally Posted by ChaosInc View Post
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.
__________________
My projects - Primal Defense | EchoPort | QuickTradeskill
  Reply With Quote
08-26-10, 01:57 PM   #8
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
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
__________________
« Website | GitHub »

Oh hai!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need some logic help


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