WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Why does my code give no error, but always return 0 (https://www.wowinterface.com/forums/showthread.php?t=59514)

phatzz 02-22-23 05:59 AM

Why does my code give no error, but always return 0
 
Trying to count the number of debuffs with x name with player as source from nameplates.
What am i doing wrong? very new at coding but trying to learn for entertainment and knowledge.

here is the variations i tried
variaton 1:
Quote:

local DebuffCount = 0
local spellname = "Stellar Flare"
local playerName = UnitName("player")
for i = 1, 20 do " +
local unit = "nameplate" .. i
if UnitExists(unit) then
for j = 1, 40 do
if select(11, UnitDebuff(unit,i)) == spellname and select(8, UnitDebuff(unit, i)) == playerName then
DebuffCount = DebuffCount + 1
end
end
end
end
return DebuffCount
variation 2:
Quote:

local DebuffCount = 0
local spellname = "Stellar Flare"
local playerName = UnitName("player")
for i = 1, 20 do
local unit = "nameplate" .. i
if UnitExists(unit) then
for j = 1, 40 do
local name, _, _, _, _, _, _, caster, _, _, spellId = UnitDebuff(unit, j)
if name == spellname and caster == playerName then
DebuffCount = DebuffCount + 1
end
end
end
end
return DebuffCount

Vampyr78 02-22-23 06:22 AM

No error means that code is correct according to the syntax and grammar rules of the language, that's it. I'm not sure what is wrong in your code but there is something wrong and probably it never gets to the line where you increase the counter.

I suggest you just debug it. Just put print() function with the variable you wanna check what value it has, does it ever has the value you actually expect. It is also an easy to way to check if your code ever enters inside the if statement. Just put a print inside it it. My first guess is that you are taking wrong values from UnitDebuff result because you used an outdated reference and the order of those values sometimes changes with expansions. So try running print(UnitDebuf(unit, i)) and see if the values you want to compare are the the positions you expect them.

phatzz 02-22-23 07:07 AM

Quote:

Originally Posted by Vampyr78 (Post 342187)
No error means that code is correct according to the syntax and grammar rules of the language, that's it. I'm not sure what is wrong in your code but there is something wrong and probably it never gets to the line where you increase the counter.

I suggest you just debug it. Just put print() function with the variable you wanna check what value it has, does it ever has the value you actually expect. It is also an easy to way to check if your code ever enters inside the if statement. Just put a print inside it it. My first guess is that you are taking wrong values from UnitDebuff result because you used an outdated reference and the order of those values sometimes changes with expansions. So try running print(UnitDebuf(unit, i)) and see if the values you want to compare are the the positions you expect them.

I found the correct values to be 1 (name) and 7 (unitcaster) by dumping, but i still am unable to get any return other than zero even with lots of nameplate uits existing and having the debuff name. Im not sure the code is properly checking all debuffs on target instead of just the first debuff? other debuiffs might exist.
I think i might have soemthing wrong in the (select(1 and select(7 part?
Here is my current code:

Quote:

local DebuffCount = 0
local spellname = "Stellar Flare"
local playerName = UnitName("player")
for i = 1, 20 do +
local unit = "nameplate" .. i
if UnitExists(unit) then
for j = 1, 40 do
if select(1, UnitDebuff(unit, j)) == spellname and select(7, UnitDebuff(unit, j)) == playerName then
DebuffCount = DebuffCount + 1
end
end
end
end
return DebuffCount

Fizzlemizz 02-22-23 08:43 AM

Return 7 from UnitDebuff would be unitID eg. "player", not the name.

Instead of calling the select and UnitDebuff functions twice each iteration you could use:

Code:

local spellName, _, _, _, _, _, unitID = UnitDebuff(unit, j)
if spellName == spellname and unitID == "player" then
        DebuffCount = DebuffCount + 1
end


phatzz 02-22-23 08:57 AM

Quote:

Originally Posted by Fizzlemizz (Post 342189)
Return 7 from UnitDebuff would be unitID eg. "player", not the name.

Instead of calling the select and UnitDebuff functions twice each iteration you could use:

Code:

local spellName, _, _, _, _, _, unitID = UnitDebuff(unit, j)
if spellName == spellname and unitID == "player" then
        DebuffCount = DebuffCount + 1
end


Thank you both for the tips!
I was able to get it working before i read your latest reply, using this code - but i see your solution is more elegant.
I will now just remove the prints and incorporate this into my own little learning addon.
thanks again!

You were both helpful in identifying the faults in my code.
The first error was inded wrong index from google to the current WoW API.
The 2nd was the use of UnitName("player") that will return the actual name instead of "player".

Code:

local DebuffCount = 0
local spellname = "Stellar Flare"
local playerName = "player"
for i = 1, 20 do
  local unit = "nameplate" .. i
  if UnitExists(unit) then
    print("Found unit: " .. unit)
    for j = 1, 40 do
      local debuffName, _, _, _, _, _, _, _, _, _, _ = UnitDebuff(unit, j)
      if debuffName and debuffName == spellname and select(7, UnitDebuff(unit, j)) == playerName then
        print("Debuff found: " .. debuffName .. " on " .. unit)
        DebuffCount = DebuffCount + 1
      end
    end
  end
end
print("Total debuffs found: " .. DebuffCount)
return DebuffCount


Fizzlemizz 02-22-23 09:11 AM

Code:

      local debuffName, _, _, _, _, _, _, _, _, _, _ = UnitDebuff(unit, j)
      if debuffName and debuffName == spellname and select(7, UnitDebuff(unit, j)) == playerName then

This still adds two extra function calls each iteration or up to an extra 800 unrequired calls each time the code is run.

Code:

      local debuffName, _, _, _, _, _, unit = UnitDebuff(unit, j)
      if not debuffName then
            break -- no more debuffs to check for this unit so let's move on
      end
      if debuffName == spellname and unit == playerName then

The underscores just denote the return values from the function you aren't interested in in order to get the two you are (1 and 7), all in the single call.
Without the if/break you're going through 40 debuff checks for each unit that exists, even it the unit has none or less than 40 (which will be most if not all of them).


All times are GMT -6. The time now is 05:42 PM.

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