WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   get class spec of Enemy Unit in BG (https://www.wowinterface.com/forums/showthread.php?t=45543)

htordeux 12-31-12 11:17 AM

get class spec of Enemy Unit in BG
 
Hello,
I want to get class & spec of EnemyUnit in BG ( "target", "raid1target"...
the function print correctly the class & Spec but returns nil,nil,nil when I want to use it in an other function.
e.g.
local unit = "target"
local enemyRole,enemySpec,enemyClass = jps_EnemySpec_BG(unit)
print("|cFFFFff00",enemyRole,enemySpec,enemyClass)

returns nil,nil,nil
I don't understand why?


Lua Code:
  1. function jps_EnemySpec_BG(unit)
  2.     if not UnitExists(unit) then unit = "player"
  3.     else unit = "target" end
  4.  
  5.     if CheckInteractDistance(unit,1)  then
  6.         NotifyInspect(unit)
  7.     end
  8.     local f=CreateFrame("Frame")
  9.     f:RegisterEvent("INSPECT_READY")
  10.     f:SetScript("OnEvent",function(self,event,...)
  11.         if event == "INSPECT_READY" then
  12.             local specID = GetInspectSpecialization(unit)
  13.             local enemySpec = select(2,GetSpecializationInfoByID(specID))
  14.             local enemyRole = select(6,GetSpecializationInfoByID(specID))
  15.             local enemyClass = select(7,GetSpecializationInfoByID(specID))
  16.             print(specID,"-",UnitName(unit),"is",jps_tableSpec[specID],"-",enemyClass,"-",enemySpec,"-",enemyRole)
  17.         end
  18.         f:UnregisterEvent("INSPECT_READY")
  19.     end)
  20.  
  21. return enemyRole,enemySpec,enemyClass
  22. end

Ps jps_tableSpec is a simple table of differnts spec

Lua Code:
  1. jps_tableSpec =
  2. {
  3.   [250]="Blood", [251]="Frost", [252]="Unholy", [102]="Balance", [103]="Feral",
  4.   [104]="Guardian", [105]="Restoration", [253]="Beast Mastery", [254]="Marksmanship",
  5.   [255]="Survival", [62]="Arcane", [63]="Fire", [64]="Frost", [268]="Brewmaster",
  6.   [270]="Mistweaver", [269]="Windwalker", [65]="Holy", [66]="Protection",
  7.   [70]="Retribution", [256]="Discipline", [257]="Holy", [258]="Shadow",
  8.   [259]="Assassination", [260]="Combat", [261]="Subtlety", [262]="Elemental",
  9.   [263]="Enhancement", [264]="Restoration", [265]="Affliction", [266]="Demonology",
  10.   [267]="Destruction", [71]="Arms", [72]="Fury", [73]="Protection",
  11.   [0]="Unknown Spec"
  12. }

endx7 12-31-12 02:04 PM

The simple answer is that enemyRole, enemySpec, and enemyClass haven't been set yet when the function returns their values. The INSPECT_READY event is fired at some point in the future after that point.

Also, you also really don't want to be creating a new frame/event handler for every call.

Phanx 12-31-12 04:04 PM

Also, even if you fixed the other issues, your function will never return any information about any unit other than "player" or "target" because of this (fixed formatting for readability):
Code:

    if not UnitExists(unit) then
        unit = "player"
    else
        unit = "target"
    end

No matter what unit you pass to the function, it will overwrite it with "player" or "target".

Dawn 12-31-12 11:10 PM

Not sure what you are up to, but I suggest you take a look at "battlegroundtargets".

htordeux 01-01-13 09:53 AM

Thnaks for your answers.

@endx7 , yes you're right the INSPECT_READY event is fired after enemyRole,enemySpec,enemyClass haven't been set
the print returns nil nil nil
then INSPECT_READY is fired
then print correctly the spec of the target

@Phanx the target or player was only written for testing purpose

@Dawn Thanks i'am going to look at this addon

Choonstertwo 01-06-13 03:45 PM

Apart from the issues the others mentioned, the local variables you're trying to return the values of are out of the scope of the return statement. When you use the local keyword, the local variable is created for the innermost block and isn't visible to any code outside of the block.

The manual explains this well:
http://www.lua.org/manual/5.1/manual.html#2.6

Your local variables are defined in the innermost if block, but the return statement that tries to access them is outside of that block, meaning it accesses global variables of the same name (which are nil in this case).

Here's an untested example of what your code could look like:
Lua Code:
  1. jps_tableSpec =
  2. {
  3.   [250]="Blood", [251]="Frost", [252]="Unholy", [102]="Balance", [103]="Feral",
  4.   [104]="Guardian", [105]="Restoration", [253]="Beast Mastery", [254]="Marksmanship",
  5.   [255]="Survival", [62]="Arcane", [63]="Fire", [64]="Frost", [268]="Brewmaster",
  6.   [270]="Mistweaver", [269]="Windwalker", [65]="Holy", [66]="Protection",
  7.   [70]="Retribution", [256]="Discipline", [257]="Holy", [258]="Shadow",
  8.   [259]="Assassination", [260]="Combat", [261]="Subtlety", [262]="Elemental",
  9.   [263]="Enhancement", [264]="Restoration", [265]="Affliction", [266]="Demonology",
  10.   [267]="Destruction", [71]="Arms", [72]="Fury", [73]="Protection",
  11.   [0]="Unknown Spec"
  12. }
  13.  
  14. local f = CreateFrame("Frame")
  15. f:SetScript("OnEvent",function(self, event, ...)
  16.     if self[event] then
  17.         self[event](self, ...) -- Call the method with the same name as the event. (INSPECT_READY fires the f:INSPECT_READY(...) method)
  18.     end
  19. end)
  20.  
  21. f:RegisterEvent("INSPECT_READY")
  22.  
  23. function f:INSPECT_READY(guid) -- Wowpedia says it has a single GUID argument, WoW Programming says it doesn't. If it doesn't have this argument, get rid of the GUID check below.
  24.     local unit = self.unit
  25.     if unit and UnitGUID(unit) == guid then
  26.         self.unit = nil
  27.         local specID = GetInspectSpecialization(unit)
  28.         local _, enemySpec, _, _, _, enemyRole, enemyClass = GetSpecializationInfoByID(specID) -- It's more efficient to call the function once and use dummy variables for the returns you don't need than to call it once for each return you want.
  29.         print(specID, "-", UnitName(unit), "is", jps_tableSpec[specID], "-", enemyClass, "-", enemySpec, "-", enemyRole)
  30.         self:InspectReadyCallback(unit, specID, enemySpec, enemyRole, enemyClass) -- You can rename this and reorganise the arguments to your liking, just make sure you change its definition below.
  31.     end
  32. end
  33.  
  34. function f:InspectReadyCallback(unit, specID, enemySpec, enemyRole, enemyClass) -- Called when INSPECT_READY fires for the unit we inspected
  35.     -- Do some stuff with the information we received from the inspection
  36. end
  37.  
  38. function jps_EnemySpec_BG(unit)
  39.     if not UnitExists(unit) then unit = "player"
  40.     else unit = "target" end
  41.  
  42.     if CheckInteractDistance(unit,1) then
  43.         f.unit = unit
  44.         NotifyInspect(unit)
  45.     end
  46. end

Dridzt 01-06-13 07:02 PM

I'm just going to leave here that you don't need that static table to get the spec name.
This should get you there and be dynamic to classes added as long as the API doesn't change significantly.

After you've validated your 'unit' in whatever method to ensure it's one you want to process this snippet will return you the relevant data.
Code:

local locClass, enClass, classID = UnitClass(unit)
local global_spec_id, spec_name, description, icon, background = GetSpecializationInfoForClassID(classID)

You don't need all those returns obviously I left them in for verbosity.

I think based on what you're trying to do, you might also be better served by embedding LibGroupInSpecT-1.0
and letting it handle all the gritty details.


All times are GMT -6. The time now is 10:13 AM.

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