WoW:API GetCritChance: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Added 1.10 fix to GameTooltip -- :SetOwner)
No edit summary
Line 33: Line 33:
:'''Note''': Sometimes, the percentage returned would include a '''lot''' of decimals, hence the usage of [http://www.lua.org/manual/5.0/manual.html#5.5 math.ceil()]] here to round up the number to the nearest decimal.
:'''Note''': Sometimes, the percentage returned would include a '''lot''' of decimals, hence the usage of [http://www.lua.org/manual/5.0/manual.html#5.5 math.ceil()]] here to round up the number to the nearest decimal.


== Exemples ==
== Examples ==
Simply call the function and display it, like this:
Simply call the function and display it, like this:
<!-- begin code -->
<!-- begin code -->

Revision as of 20:36, 23 August 2006

Since there are functions incorporated in the WoW API to return parry, dodge and block chances but none to return critical hit chance, here is a GetCritChance() function that will do so.

GetCritChance()

Function code

 function GetCritChance()
   local critChance, iCritInfo, critNum;
   local id = 1;
   -- This may vary depending on WoW localizations.
   local atkName = "Attack";
   local attackSpell = GetSpellName(id,BOOKTYPE_SPELL);
   if (attackSpell ~= atkName) then
     name, texture, offset, numSpells = GetSpellTabInfo(1);
     for i=1, numSpells do
       if (GetSpellName(i,BOOKTYPE_SPELL) == atkName) then
         id = i;
       end
     end
   end
   GameTooltip:SetOwner(WorldFrame,"ANCHOR_NONE");
   GameTooltip:SetSpell(id, BOOKTYPE_SPELL);
   local spellName = GameTooltipTextLeft2:GetText();
   GameTooltip:Hide();
   iCritInfo = string.find(spellName, "%s");
   critNum = string.sub(spellName,0,(iCritInfo -2));
   critChance = math.ceil(critNum);
   return critChance;
 end

Returns

Player's critical hit chance, in percentage (without the % symbol).
Note: Sometimes, the percentage returned would include a lot of decimals, hence the usage of math.ceil()] here to round up the number to the nearest decimal.

Examples

Simply call the function and display it, like this:

 -- Will display your critical hit chance in your default chat frame.
 DEFAULT_CHAT_FRAME:AddMessage("Your crit chance is: "..GetCritChance().."%");

You can also use this code for a more thorough display:

 -- Will send a "SAY" message (/s) with your crit, dodge, parry and block chances in percentage.
 SendChatMessage("My crit chance is: "..GetCritChance().."%","SAY");
 SendChatMessage(UnitName('player').."'s chances to...","SAY");
 SendChatMessage("Crit: "..GetCritChance().."%","SAY");
 SendChatMessage("Dodge: "..GetDodgeChance().."%","SAY");
 SendChatMessage("Parry: "..GetParryChance().."%","SAY");
 SendChatMessage("Block: "..GetBlockChance().."%","SAY");

How it works

This function simply loops through your spellbook (BOOKTYPE_SPELL) for your regular melee attack skill and then grabs the text from the skill's tooltip using GameTooltip functions (the tooltip of the regular melee attack is the only place where the crit chance is displayed).

Credits

Much of this GetCritChance() function was taken and (slightly) modified from mercdev's Combat Info (alt) for Titan Panel. Thanks to him.


Template:WoW API