Thread Tools Display Modes
03/12/15, 05:58 PM   #1
Balver
 
Balver's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 39
Double output

I'm trying to modify an outdated addon (El Chivato) for myself and there's a bug I can't fix with my limited coding knowledge.
When I complete a quest the XP appear twice.


The code I use is:
Lua Code:
  1. local function PrintXP(color, text)
  2.     -- convert text to string to prevent passing nil arguments to d
  3.     d(color .. tostring(text))
  4. end
  5.  
  6.  
  7. -- Finish a Quest
  8. local function OnQuestCompleteXP(event, questName, level, prevXP, newXP, rank, prevVP, newVP)
  9.         PrintXP(colors.Quest, "Completed Quest: " .. questName ..". +" .. newXP - prevXP .. " XP")
  10. end
  11.  
  12. -- Finish a Quest
  13. EVENT_MANAGER:RegisterForEvent(Chivato.name, EVENT_QUEST_COMPLETE, OnQuestCompleteXP)

In the Wiki I read "- For some quests, can triggers twice " for EVENT_QUEST_COMPLETE so it seems to be an API problem.
Is there a way to prevent a double output?

And when I'm here already, how do I get rid of the suffixes in German names? I've seen that other addons managed to do this.



Thanks in advance!
  Reply With Quote
03/12/15, 06:08 PM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
1) I'd try to save previous experience and compare if experience was changed.
2) I recommend using zo_strformat to remove suffixes.
  Reply With Quote
03/12/15, 06:10 PM   #3
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,960
About the format you could use this snippet:
Lua Code:
  1. local formattedText = zo_strformat('<<C:1>>', unformatedText)

unformatedText is your variable with the german names, containing e.g. "Sturmhang-Krypta^fd,in".
formatedText should be the new variable without the extra stuff, like "Die Sturmhang-Krypta".


For the double output Garkin is right. You could realize it like this:
Lua Code:
  1. --local variable array in your addon, outside OnQuestCompleteXP
  2. local helperVars = {}
  3. helperVars.lastXPGainedQuestName = ""
  4. helperVars.lastXPGainedXP = 0
  5.  
  6. -- Finish a Quest
  7. local function OnQuestCompleteXP(event, questName, level, prevXP, newXP, rank, prevVP, newVP)
  8.     local xpGained = newXP - prevXP
  9.     if helperVars.lastXPGainedQuestName ~= questName and helperVars.lastXPGainedXP ~= xpGained then
  10.         PrintXP(colors.Quest, "Completed Quest: " .. questName ..". +" .. xpGained .. " XP")
  11.         helperVars.lastXPGainedQuestName = questName
  12.         helperVars.lastXPGainedXP = xpGained
  13.     end
  14. end

Wenn du noch Fragen hast schreib mir 'ne PM

Last edited by Baertram : 03/12/15 at 06:17 PM.
  Reply With Quote
03/12/15, 06:20 PM   #4
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Lua Code:
  1. local previousXP = 0
  2.  
  3. local function PrintXP(color, text)
  4.     -- convert text to string to prevent passing nil arguments to d
  5.     d(color .. tostring(text))
  6. end
  7.  
  8. -- Finish a Quest
  9. local function OnQuestCompleteXP(event, questName, level, prevXP, newXP, rank, prevVP, newVP)
  10.     local currentXP = GetUnitXP("player")
  11.     local gain = currentXP - previousXP
  12.  
  13.     if gain > 0 then
  14.         PrintXP(colors.Quest, zo_strformat("Completed Quest: <<t:1>>. +<<2>> XP", questName, gain))
  15.         previousXP = currentXP
  16.     end
  17. end
  18.  
  19. -- Finish a Quest
  20. EVENT_MANAGER:RegisterForEvent(Chivato.name, EVENT_QUEST_COMPLETE, OnQuestCompleteXP)
  21.  
  22. --get initial XP value
  23. local function OnUnitCreated(event, unitTag)
  24.     if unitTag == "player" then
  25.         previousXP = GetUnitXP("player")
  26.     end
  27. end
  28.  
  29. EVENT_MANAGER:RegisterForEvent(Chivato.name, EVENT_UNIT_CREATED, OnUnitCreated)

Last edited by Garkin : 03/12/15 at 06:22 PM.
  Reply With Quote
03/12/15, 06:35 PM   #5
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
I was typing something up, but I'm to slow.
Although if they gain xp from some form other than a quest (discovery or something) and then complete a quest that will show the wrong xp gain because its printing currentXP - previousXP and previousXP is only set when completing a quest, so:
Lua Code:
  1. -- Finish a Quest
  2. local function OnQuestCompleteXP(event, questName, level, prevXP, newXP, rank, prevVP, newVP)
  3.     --local currentXP = GetUnitXP("player")
  4.    -- local gain = currentXP - previousXP
  5.  
  6.    -- if gain > 0 then
  7.     if newXP > previousXP then
  8.         local xpGain = newXP - prevXP
  9.         --PrintXP(colors.Quest, zo_strformat("Completed Quest: <<t:1>>. +<<2>> XP", questName, gain))
  10.         PrintXP(colors.Quest, zo_strformat("Completed Quest: <<t:1>>. +<<2>> XP", questName, xpGain))
  11.         --previousXP = currentXP
  12.         previousXP = newXP
  13.     end
  14. end

Last edited by circonian : 03/12/15 at 06:39 PM.
  Reply With Quote
03/12/15, 07:03 PM   #6
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
I had reported this bug since months, because for some quests, EVENT_QUEST_COMPLETE can fires twice.

I've personally set an internal cooldown of 2 seconds
(you can check questname with parameter given by the event
  Reply With Quote
03/12/15, 07:13 PM   #7
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by circonian View Post
I was typing something up, but I'm to slow.
Although if they gain xp from some form other than a quest (discovery or something) and then complete a quest that will show the wrong xp gain because its printing currentXP - previousXP and previousXP is only set when completing a quest, so:
True, I didn't think about it. I was always using EVENT_EXPERIENCE_UPDATE where are all XP gains.
  Reply With Quote
03/12/15, 08:38 PM   #8
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,960
Thats why I had used the questName + newXP for the comparison, and not the total gained XP (player current - prevXP).
  Reply With Quote
03/13/15, 04:53 AM   #9
Balver
 
Balver's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 39
Thanks for the help guys.
I fiddled the code together and it worked at first, but after a /reloadui I got an error
Code:
2015-03-13T04:12:29.501+01:00 |cff0000Lua Error: user:/AddOns/Chivato/Chivato.lua:78: operator < is not supported for nil < number
stack traceback:
	user:/AddOns/Chivato/Chivato.lua:78: in function 'OnQuestCompleteXP'|r
Could it be, that EVENT_UNIT_CREATED only fires at login? I replaced it with EVENT_ADD_ON_LOADED and now it works fine.
German suffixes are gone too, thanks again.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Double output

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