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:
Ps jps_tableSpec is a simple table of differnts spec Lua Code:
|
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. |
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 |
Not sure what you are up to, but I suggest you take a look at "battlegroundtargets".
|
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 |
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:
|
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) 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