WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   I do not understand why I am getting type Boolean when I should be getting number (https://www.wowinterface.com/forums/showthread.php?t=59834)

myrroddin 03-23-24 04:54 AM

I do not understand why I am getting type Boolean when I should be getting number
 
1 Attachment(s)
I have been battling with logic for a long time in RepByZone. The idea is to switch the watched reputation based on the player's location, but only if certain things are true. To that end, I have tried many, many iterations of the logic. Sometimes I get it mostly working, but it never did work 100%.

The one thing that eluded me was if there was no zone data, in which case the code should fall back to some racial default. For whatever reason, I couldn't get it to work.

My latest attempt truly baffles me. Looking at Core-Retail.lua on lines 639–647, the debug print is telling me that watchedFactionID is a Boolean when it should be a number, assuming it is not nil. How it figures to be Boolean, I have no idea, which is why I am asking here.

The short section can be read below. I attached the full zip to show that numbers exist. The function starts on line 569. If I can solve why I am getting a Boolean rather than a number, I will adapt the code for Core-Vanilla.lua and Core-Wrath.lua, both of which are a little different (and working except for the fallback reputation). You can see them in the zip.

What am I not understanding about Lua? Too many "and" per line? Does Lua not treat "true and number" as a number?
Lua Code:
  1. watchedFactionID = watchedFactionID == nil and inInstance and hasDungeonTabard and tabardID
  2. watchedFactionID = watchedFactionID == nil and lookUpSubZones and citySubZonesAndFactions[subZone] or subZonesAndFactions[subZone]
  3. watchedFactionID = watchedFactionID == nil and inInstance and instancesAndFactions[whichInstanceID]
  4. watchedFactionID = watchedFactionID == nil and not lookUpSubZones and isWoDZone and bodyguardRepID
  5. watchedFactionID = watchedFactionID == nil and not inInstance and zonesAndFactions[uiMapID] or watchedFactionID = watchedFactionID == nil and self.fallbackRepID
  6.  
  7. -- debug print
  8. self:Print("watchedFactionID is", watchedFactionID or type(watchedFactionID))

Xrystal 03-23-24 07:11 AM

I can't see anything clearly wrong with your code.

nUI auto switches rep fine ( although admittedly I haven't played properly much recently so a recent patch may have done something, but no one has reported anything yet in that regards ) .

I would suggest checking each lines values and see if any of them result in a true or false value for the watchedFactionID variable. Which would mean the following lines would not execute.

Is that last line supposed to have that extra test at the end ? It seems like it should be on the next line rather than as an or statement. Technically shouldn't make a difference but hard to say.

Edit:
Also, you might find you need to put some of those tests in brackets to ensure the right results are being tested and the right value being assigned. That's messed me up a few times when I've done assignment lines like that.

Fizzlemizz 03-23-24 09:53 AM

Code:

watchedFactionID = watchedFactionID == nil and inInstance and hasDungeonTabard and tabardID
That resoves to a boolean
Code:

if watchedFactionID == nil and inInstance == AnythingButNilOrFalse and hasDungeonTabard == AnythingButNilOrFalse and tabardID == AnythingButNilOrFalse then
  watchedFactionID = true
else
  watchedFactionID = false
end

which means watchedFactionID won't be nil for the others because it will always be true or false (boolean).


Maybe:
Code:

watchedFactionID = (watchedFactionID == nil and inInstance and hasDungeonTabard) and tabardID or nil
Code:

if watchedFactionID = (watchedFactionID == nil and inInstance == AnythingButNilOrFalse and hasDungeonTabard == AnythingButNilOrFalse) then
    watchedFactionID = tabardID
else
    watchedFactionID = nil
end

(Define your truth) and (then make an assigment if true) or (other assigment [defaults to false if no "or" and truth is not met])

SDPhantom 03-23-24 05:17 PM

Every check for watchedFactionID==nil is overwriting watchedFactionID with false whenever it isn't nil.

-also-

Code:

watchedFactionID = watchedFactionID == nil and not inInstance and zonesAndFactions[uiMapID] or watchedFactionID = watchedFactionID == nil and self.fallbackRepID
This should be throwing a syntax error since you can't have an assignment operator (=) in the middle of an expression.

SDPhantom 03-23-24 08:44 PM

This is how I would implement a fallback list using multiple ors.
Lua Code:
  1. watchedFactionID = watchedFactionID
  2.     or (inInstance and hasDungeonTabard and tabardID)
  3.     or (lookUpSubZones and (citySubZonesAndFactions[subZone] or subZonesAndFactions[subZone]))
  4.     or (inInstance and instancesAndFactions[whichInstanceID])
  5.     or (not lookUpSubZones and isWoDZone and bodyguardRepID)
  6.     or (not inInstance and zonesAndFactions[uiMapID])
  7.     or self.fallbackRepID

Note parenthesis are highly suggested to direct which order you want logic operations to run as mixing and/or operations can lead to unpredictable results.

myrroddin 03-24-24 03:18 AM

Quote:

Originally Posted by Xrystal (Post 343614)

Is that last line supposed to have that extra test at the end ? It seems like it should be on the next line rather than as an or statement. Technically shouldn't make a difference but hard to say.

That is a typo. It shouldn't look like that, no. Somehow I copied and pasted from my snippet back into the main file and saved it before I created a zip.

I will check the other answers and do some testing. I saw the SDPhantom's answer that explains it is overwriting nil with false. I realized that a few minutes before I booted my computer to check the forums. Yep, my brain is an entire day behind.

myrroddin 03-24-24 04:13 AM

Quote:

Originally Posted by SDPhantom (Post 343623)
This is how I would implement a fallback list using multiple ors.
Lua Code:
  1. watchedFactionID = watchedFactionID
  2.     or (inInstance and hasDungeonTabard and tabardID)
  3.     or (lookUpSubZones and (citySubZonesAndFactions[subZone] or subZonesAndFactions[subZone]))
  4.     or (inInstance and instancesAndFactions[whichInstanceID])
  5.     or (not lookUpSubZones and isWoDZone and bodyguardRepID)
  6.     or (not inInstance and zonesAndFactions[uiMapID])
  7.     or self.fallbackRepID

Note parenthesis are highly suggested to direct which order you want logic operations to run as mixing and/or operations can lead to unpredictable results.

This solution worked! I tested in Un'Goro Crater, which has no rep assigned and the addon watched my fallback. Then I went into the Stockades dungeon with the Ironforge faction tabard, which worked. And finally, I flew back and forth between the main section of Valdrakken and the Artisan's section and that worked.

Thank you so much!


All times are GMT -6. The time now is 12:19 AM.

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