I am currently trying to make an addon to send a congratz message anytime someone in the players party reaches a level where he will be able to get a new talent (way less spam than sending a congratz everytime someone levels up).
Here is my current code:
-- Variables --
local maxLevel = GetMaxPlayerLevel();
local partyFrame = CreateFrame("Frame", "DarkrunePartyFrame");
local settingsFrame = CreateFrame("Frame", "DarkruneSettingsFrame");
-- Settings
function loadSettings()
DLUSettings = DLUSettings or {
partyEnabled = false,
pvpEnabled = false,
professionsEnabled = false
}
end
-- Important functions --
function partyLevelUp(self, event, unitID)
if (UnitInParty(unitID) and UnitPlayerControlled(unitID) and not UnitIsUnit(unitID, "player") and not IsInRaid() and DLUSettings.partyEnabled) then
local partyMemberName = UnitName(unitID);
local InInstanceGroup = IsInGroup(LE_PARTY_CATEGORY_INSTANCE);
local class, classFileName = UnitClass(unitID);
local newLevel = UnitLevel(unitID);
local congratzTable = CLASS_TALENT_LEVELS["DEFAULT"];
if (classFileName == CLASS_SORT_ORDER[2] or classFileName == CLASS_SORT_ORDER[12]) then
congratzTable = CLASS_TALENT_LEVELS[classFileName];
end
for i = 1, #congratzTable do
print(congratzTable[i] == newLevel);
print(type(congratzTable[i]) .. ": " .. congratzTable[i]);
print(type(newLevel) .. ": " .. newLevel);
if (newLevel == congratzTable[i]) then
local randomCongratz = {"Gratz, %s", "Gz, %s", "Keep it up, %s"};
local congratzString = string.format(randomCongratz[math.random(#randomCongratz)], partyMemberName);
if (InInstanceGroup) then
SendChatMessage(congratzString, "INSTANCE_CHAT");
elseif (IsInGroup()) then
SendChatMessage(congratzString, "PARTY");
end
end
end
end
end
-- Adding event --
settingsFrame:RegisterEvent("PLAYER_LOGIN");
settingsFrame:SetScript("OnEvent", function()
loadSettings();
partyFrame:RegisterEvent("UNIT_LEVEL");
partyFrame:SetScript("OnEvent", partyLevelUp);
settingsFrame:UnregisterEvent("PLAYER_LOGIN");
end)
The problem is, that it seems to return false then I run this:
print(congratzTable[i] == newLevel);
Anyone who knows why? - got the class constant from searchcode.com.
Only reason why it would return false is the fact congrazTable[i] and newLevel being different values and the comparison returns false.
Maybe this would work (untested)?
function partyLevelUp(self, event, unitID)
if (UnitInParty(unitID) and UnitPlayerControlled(unitID)) and not (UnitIsUnit(unitID, "player") and IsInRaid()) and DLUSettings.partyEnabled then
local partyMemberName = UnitName(unitID);
local _, classFileName = UnitClass(unitID);
local newLevel = UnitLevel(unitID);
local congratzTable
local randomCongratz = {"Gratz, %s", "Gz, %s", "Keep it up, %s"};
if (classFileName == "DEATHKNIGHT" or classFileName == "DEMONHUNTER") then
congratzTable = CLASS_TALENT_LEVELS[classFileName];
else
congratzTable = CLASS_TALENT_LEVELS["DEFAULT"];
end
for _, level in pairs(congratzTable) do
if level == newLevel then
print("Level: "..level)
local congratzString = string.format(randomCongratz[math.random(#randomCongratz)], partyMemberName);
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then
SendChatMessage(congratzString, "INSTANCE_CHAT");
else
SendChatMessage(congratzString, "PARTY");
end
break
end
end
end
end
Only reason why it would return false is the fact congrazTable[i] and newLevel being different values and the comparison returns false.
Maybe this would work (untested)?
function partyLevelUp(self, event, unitID)
if (UnitInParty(unitID) and UnitPlayerControlled(unitID)) and not (UnitIsUnit(unitID, "player") and IsInRaid()) and DLUSettings.partyEnabled then
local partyMemberName = UnitName(unitID);
local _, classFileName = UnitClass(unitID);
local newLevel = UnitLevel(unitID);
local congratzTable
local randomCongratz = {"Gratz, %s", "Gz, %s", "Keep it up, %s"};
if (classFileName == "DEATHKNIGHT" or classFileName == "DEMONHUNTER") then
congratzTable = CLASS_TALENT_LEVELS[classFileName];
else
congratzTable = CLASS_TALENT_LEVELS["DEFAULT"];
end
for _, level in pairs(congratzTable) do
if level == newLevel then
print("Level: "..level)
local congratzString = string.format(randomCongratz[math.random(#randomCongratz)], partyMemberName);
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then
SendChatMessage(congratzString, "INSTANCE_CHAT");
else
SendChatMessage(congratzString, "PARTY");
end
break
end
end
end
end
Thanks :-). Made a few modifications to the code, but it seems to work correctly :-). At least for Demon Hunters.
I have now added the changed the function to a local function. Hopefully this won't break the addon ☺
It won't, and now it won't break other addon code, or worse, Blizzard code.
Think of it this way: assume I am either ignorant or sinister, and you did not make that function local to your addon. I could then write a function with the exact same name "partyLevelUp", and have mine do something totally different to your function. I could even call your function and pass incorrect data to your arguments causing your code to error and you'd never know why.
That would mean that one function, depending on when it was called, would overwrite the other. Yours would overwrite mine, and sometimes mine would overwrite yours. There is absolutely zero way to control that.
Now your function is local to your file scope because you added the word "local". Even if I did write something with the same name, neither of our code impacts the other, yet, because my version is not local to my file scope, some other addon or Blizzard code can cause conflicts. Your addon is safe, but mine is ... potentially (probably) corrupt.
I have now added the changed the function to a local function. Hopefully this won't break the addon ☺
It won't, and now it won't break other addon code, or worse, Blizzard code.
Think of it this way: assume I am either ignorant or sinister, and you did not make that function local to your addon. I could then write a function with the exact same name "partyLevelUp", and have mine do something totally different to your function. I could even call your function and pass incorrect data to your arguments causing your code to error and you'd never know why.
That would mean that one function, depending on when it was called, would overwrite the other. Yours would overwrite mine, and sometimes mine would overwrite yours. There is absolutely zero way to control that.
Now your function is local to your file scope because you added the word "local". Even if I did write something with the same name, neither of our code impacts the other, yet, because my version is not local to my file scope, some other addon or Blizzard code can cause conflicts. Your addon is safe, but mine is ... potentially (probably) corrupt.
I know it shouldn't, but it all depends on the order the calls/functions are declared in the file. For instance if you try to call a local function located below the call, it will be unable to run it (because the program doesn't know the function yet).
In this case it should work I guess... Thanks for the help :-)
I know it shouldn't, but it all depends on the order the calls/functions are declared in the file. For instance if you try to call a local function located below the call, it will be unable to run it (because the program doesn't know the function yet).
In this case it should work I guess... Thanks for the help :-)
If the order gives you headaches, you can define all the function names as local at the start of the file like this:
ADDON_NAME, nameSpace = ...
local firstFunctionName, secondFunctionName, thirdFunctionName
function thirdFunctionName(userInput)
return secondFunctionName(userInput)
end
function secondFunctionName(userInput)
return firstFunctionName(userInput)
end
function firstFunctionName(userInput)
if userInput then
return type(userInput)
end
return false
end
and they all are local scoped (or at least I haven't ran into any global leaking afaik using this method) but you don't have to worry about their order.
Hi.
I am currently trying to make an addon to send a congratz message anytime someone in the players party reaches a level where he will be able to get a new talent (way less spam than sending a congratz everytime someone levels up).
Here is my current code:
The problem is, that it seems to return false then I run this:
Anyone who knows why? - got the class constant from searchcode.com.
Only reason why it would return false is the fact congrazTable[i] and newLevel being different values and the comparison returns false.
Maybe this would work (untested)?
Your function partyLevelUp is not local, and is polluting the global namespace. Put the word local in front of it.
if it was unclear what to do.
I have now added the changed the function to a local function. Hopefully this won't break the addon ☺
To read more about variable scope, read here:
and they all are local scoped (or at least I haven't ran into any global leaking afaik using this method) but you don't have to worry about their order.