After hours of searching for my fault, i simply don't get why my function finds variables correct with my first test Spell (Shadow Word: Pain) but doesn't find these for other Spells.
Any help would be greatly appreciated.
This is the function:
local GetSpellVarsTooltip = CreateFrame( "GameTooltip", "GetSpellVarsTooltip", UIParent, "GameTooltipTemplate" );
function GetSpellVars(spellId)
GetSpellVarsTooltip:SetOwner(UIParent, "ANCHOR_NONE")
GetSpellVarsTooltip:SetSpellByID(spellId);
local spellDescription = GetSpellVarsTooltipTextLeft4:GetText();
local totalDmg, tickTime
local switchCaseTable = {
[589] = function (x) -- Shadow Word: Pain
pattern = string.gsub(string.gsub(x, "(%$o1)", "(.+)"), "(%$d)", "(.+)")
_, _, totalDmg, tickTime = string.find(spellDescription, pattern)
end,
[2944] = function (x) -- Devouring Plague
pattern = string.gsub(string.gsub(x, "(%$o1)", "(.+)"), "(%$d)", "(.+)")
_, _, totalDmg, tickTime = string.find(spellDescription, pattern)
end,
[34914] = function (x) -- Vampiric Touch
pattern = string.gsub(string.gsub(x, "(%$34914o2)", "(.+)"), "(%$d)", "(.+)")
_, _, totalDmg, tickTime = string.find(spellDescription, pattern)
end,
}
if (switchCaseTable[spellId]) then
local spellDescriptionPattern = GetSpellDescription( spellId )
switchCaseTable[spellId]( spellDescriptionPattern )
print("Search in Text: "..spellDescription)
print("With Pattern: "..pattern)
print("Returns Matches: ")
print(totalDmg)
print(tickTime)
end
end
/script GetSpellVars(589)
Prints the following in Chat: (expected Output)
Search in Text: A word of darkness that causes 5808 Shadow damage over 19.13 sec.
With Pattern: A word of darkness that causes (.+) Shadow damage over (.+).
Returns Matches:
5808
19.13 sec
/script GetSpellVars(2944)
Prints the following in Chat: (unexpected Output)
Search in Text: Afflicts the target with a disease that causes 6800 Shadow damage over 23.91 sec. 15% of damage caused by the Devouring Plague heals the caster. This spell can only affect one target at a time.
With Pattern: Afflicts the target with a disease that causes (.+) Shadow damage over (.+). 15% of damage caused by the Devouring Plague heals the caster. This spell can only affect one target at a time.
Returns Matches:
nil
nil
/script GetSpellVars(34914)
Prints the following in Chat: (unexpected Output)
Search in Text: Causes 7746 Shadow damage over 14.35 sec, and when you deal damage with Mind Blast to an affected target you cause up to 10 party or raid members to gain 1% of their maximum mana per 10 sec.
With Pattern: Causes (.+) Shadow damage over (.+), and when you deal damage with Mind Blast to an affected target you cause up to 10 party or raid members to gain 1% of their maximum mana per 10 sec.
Returns Matches:
nil
nil
^ Change all instances of "$o1" to "(.+)" then change all instances of "$d" to "(.+)". I'm not exactly sure what you're trying to accomplish with this.
Edit: Also, "." is not part of a number to be captured with "%d".
Edit2: And where's the rest of your code? GetSpellDescription() doesn't exist.
Edit3: Lastly, that's the worst implementation of anything ever. You're leaking globals and making new tables and functions every time GetSpellVars() is called.
GetSpellDescription is part of the default WoW-API (But undocumented on wowprogramming or wowwiki). It takes a SpellId as Argument, and returns the description string of the spell with variables in it.
/script print(GetSpellDescription(589)) should print in default chat:[FONT=System]
[FONT=Courier New]A word of darkness that causes $o1 Shadow damage over $d.
[FONT=Verdana]And this is the string i take to replace [/FONT][/FONT][/FONT][FONT=Verdana]all instances of "$o1" to "(.+)" then change all instances of "$d" to "(.+)" to use it as a pattern to search in the real spell description i recieved from the tooltip.
[/FONT][FONT=Verdana]
[/FONT]
Also, "." is not part of a number to be captured with "%d".
Im not using a %d in my pattern to capture anything?!?
(.+) should match all text that the gameclient filled the $vars with. So for the dmg potion of the spell this is only a number, but for the duration this is a number, that could contain a "." (or "," depending on Localisation) and has a localized string for seconds in it.
Lastly, that's the worst implementation of anything ever. You're leaking globals and making new tables and functions every time GetSpellVars() is called.
Hmm, thanks. I'm not very expirenced when it comes to lua :|
I don't even know what you mean with leaking globals in the code.
Also, "." is not part of a number to be captured with "%d".
Im not using a %d in my pattern to capture anything?!?
(.+) should match all text that the gameclient filled the $vars with. So for the dmg potion of the spell this is only a number, but for the duration this is a number, that could contain a "." (or "," depending on Localisation) and has a localized string for seconds in it.
I said that because I was unaware that GetSpellDescription() was part of the WoW-API and didn't know what it returned. Now it all makes sence. :) ... Except for the coding...
Edit: I just noticed that your patterns for the two fails contain percent symbols while the successful one does not. I suspect this to be your problem. Add another gsub() to escape those (e.g. string.gsub(pattern, "%%", "%%%%") -- I know, it's so gratuitous).
D'oh! Indeed the problem is due to the '%' character used in the spell descriptions of Devouring Plague and Vampiric Touch which i haven't escaped properly. At least this code is running now.
Just have to say thanks to both of you for the advice. :!:
I think pattern special chars deserve to be escaped, with something like string.gsub(pattern, '([-%[%]%%%.%(%)%*%+%?])', '%%%1') ; this from the top of my head and need to be verified.
This is what i use now for escaping the pattern, before replacing the variables:
[FONT=Courier New]string.gsub(pattern, "([%^%(%)%%%.%[%]%*%+%-%?])", "%%%1")
[FONT=Verdana]
Note that the '$' is also magic character for pattern matching and thus should normaly be escaped too.
[/FONT][/FONT][FONT=Courier New][FONT=Verdana]It's missing in my the statement since i replace any occurences of it later anyways.[/FONT]
[/FONT]
Any help would be greatly appreciated.
This is the function: /script GetSpellVars(589)
Prints the following in Chat: (expected Output) /script GetSpellVars(2944)
Prints the following in Chat: (unexpected Output) /script GetSpellVars(34914)
Prints the following in Chat: (unexpected Output)
Edit: Also, "." is not part of a number to be captured with "%d".
Edit2: And where's the rest of your code? GetSpellDescription() doesn't exist.
Edit3: Lastly, that's the worst implementation of anything ever. You're leaking globals and making new tables and functions every time GetSpellVars() is called.
/script print(GetSpellDescription(589)) should print in default chat:[FONT=System]
[FONT=Courier New]A word of darkness that causes $o1 Shadow damage over $d.
[FONT=Verdana]And this is the string i take to replace [/FONT][/FONT][/FONT][FONT=Verdana]all instances of "$o1" to "(.+)" then change all instances of "$d" to "(.+)" to use it as a pattern to search in the real spell description i recieved from the tooltip.
[/FONT][FONT=Verdana]
[/FONT] Im not using a %d in my pattern to capture anything?!?
(.+) should match all text that the gameclient filled the $vars with. So for the dmg potion of the spell this is only a number, but for the duration this is a number, that could contain a "." (or "," depending on Localisation) and has a localized string for seconds in it.
Hmm, thanks. I'm not very expirenced when it comes to lua :|
I don't even know what you mean with leaking globals in the code.
I said that because I was unaware that GetSpellDescription() was part of the WoW-API and didn't know what it returned. Now it all makes sence. :) ... Except for the coding...
Edit: I just noticed that your patterns for the two fails contain percent symbols while the successful one does not. I suspect this to be your problem. Add another gsub() to escape those (e.g. string.gsub(pattern, "%%", "%%%%") -- I know, it's so gratuitous).
Just have to say thanks to both of you for the advice. :!:
[FONT=Courier New]string.gsub(pattern, "([%^%(%)%%%.%[%]%*%+%-%?])", "%%%1")
[FONT=Verdana]
Note that the '$' is also magic character for pattern matching and thus should normaly be escaped too.
[/FONT][/FONT][FONT=Courier New][FONT=Verdana]It's missing in my the statement since i replace any occurences of it later anyways.[/FONT]
[/FONT]