Thread Tools Display Modes
02-22-23, 05:59 AM   #1
phatzz
A Defias Bandit
Join Date: Feb 2023
Posts: 3
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:
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:
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

Last edited by phatzz : 02-22-23 at 06:17 AM.
  Reply With Quote
02-22-23, 06:22 AM   #2
Vampyr78
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: May 2016
Posts: 10
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.
  Reply With Quote
02-22-23, 07:07 AM   #3
phatzz
A Defias Bandit
Join Date: Feb 2023
Posts: 3
Originally Posted by Vampyr78 View Post
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:

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
  Reply With Quote
02-22-23, 08:43 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-22-23 at 08:48 AM.
  Reply With Quote
02-22-23, 08:57 AM   #5
phatzz
A Defias Bandit
Join Date: Feb 2023
Posts: 3
Originally Posted by Fizzlemizz View Post
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
  Reply With Quote
02-22-23, 09:11 AM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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).
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-22-23 at 12:05 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Why does my code give no error, but always return 0


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