Reply
 
Thread Tools Display Modes
Unread 12-31-12, 11:17 AM   #1
htordeux
A Murloc Raider
Join Date: Apr 2007
Posts: 5
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. }
htordeux is offline   Reply With Quote
Unread 12-31-12, 02:04 PM   #2
endx7
An Aku'mai Servant
 
endx7's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2005
Posts: 35
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.
endx7 is offline   Reply With Quote
Unread 12-31-12, 04:04 PM   #3
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,745
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".
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Unread 12-31-12, 11:10 PM   #4
Dawn
A Molten Giant
 
Dawn's Avatar
AddOn Author - Click to view addons
Join Date: May 2006
Posts: 907
Not sure what you are up to, but I suggest you take a look at "battlegroundtargets".
__________________
Rock: "We're sub-standard DPS. Nerf Paper, Scissors are fine."
Paper: "OMG, WTF, Scissors!"
Scissors: "Rock is OP and Paper are QQers. We need PvP buffs."

"neeh the game wont be remembered as the game who made blizz the most money, it will be remembered as the game who had the most QQ'ers that just couldnt quit the game for some reason..."

Dawn is offline   Reply With Quote
Unread 01-01-13, 09:53 AM   #5
htordeux
A Murloc Raider
Join Date: Apr 2007
Posts: 5
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
htordeux is offline   Reply With Quote
Unread 01-06-13, 03:45 PM   #6
Choonstertwo
A Flamescale Wyrmkin
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 146
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
Choonstertwo is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » General Authoring Discussion » get class spec of Enemy Unit in BG

Thread Tools
Display Modes

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