WoW:API GetCritChance: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Since there are functions incorporated in the [[World of Warcraft API|WoW API]] to return [[API GetParryChance|parry]], [[API GetDodgeChance|dodge]] and [[API GetBlockChance|block]] chances but none to return critical hit chance, here is a [[GetCritChance|GetCritChance()]] function that will do so.
== GetCritChance() ==
=== Function code ===
<!-- begin code -->
  function GetCritChance()
    local critChance, iCritInfo, critNum;
    local id = 1;
    ''-- This may vary depending on WoW localizations.''
    local atkName = "Attack";
    local attackSpell = [[API GetSpellName|GetSpellName]](id,[[WoW Constants#Miscellaneous|BOOKTYPE_SPELL]]);
    if (attackSpell ~= atkName) then
      name, texture, offset, numSpells = [[API GetSpellTabInfo|GetSpellTabInfo]](1);
      for i=1, numSpells do
        if ([[API GetSpellName|GetSpellName]](i,[[WoW Constants#Miscellaneous|BOOKTYPE_SPELL]]) == atkName) then
          id = i;
        end
      end
    end
    [[Widget API#GameTooltip|GameTooltip:SetOwner]](WorldFrame,"ANCHOR_NONE");
    [[Widget API#GameTooltip|GameTooltip:SetSpell]](id, [[WoW Constants#Miscellaneous|BOOKTYPE_SPELL]]);
    local spellName = [[Widget API#GameTooltip|GameTooltipTextLeft2:GetText()]];
    [[Widget API#GameTooltip|GameTooltip:Hide()]];
    iCritInfo = string.find(spellName, "%s");
    critNum = string.sub(spellName,0,(iCritInfo -2));
    critChance = math.ceil(critNum);
    return critChance;
  end
<!-- end code -->
=== Returns ===
=== Returns ===
:Player's critical hit chance, in percentage (''without the % symbol'').
:The player's melee critical hit chance in an unformatted, floating-point figure.
:'''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.
 
== Examples ==
Simply call the function and display it, like this:
<!-- begin code -->
  -- Will display your critical hit chance in your default chat frame.
  DEFAULT_CHAT_FRAME:AddMessage("Your crit chance is: "..[[GetCritChance|GetCritChance()]].."%");
<!-- end code -->
 
You can also use this code for a more thorough display:
<!-- begin code -->
  -- Will send a "SAY" message (/s) with your [[GetCritChance|crit]], [[API GetDodgeChance|dodge]], [[API GetParryChance|parry]] and [[API GetBlockChance|block]] chances in percentage.
  [[API SendChatMessage|SendChatMessage]]("My crit chance is: "..[[GetCritChance|GetCritChance()]].."%","SAY");
  [[API SendChatMessage|SendChatMessage]]([[API UnitName|UnitName('player')]].."'s chances to...","SAY");
  [[API SendChatMessage|SendChatMessage]]("Crit: "..[[GetCritChance|GetCritChance()]].."%","SAY");
  [[API SendChatMessage|SendChatMessage]]("Dodge: "..[[API GetDodgeChance|GetDodgeChance()]].."%","SAY");
  [[API SendChatMessage|SendChatMessage]]("Parry: "..[[API GetParryChance|GetParryChance()]].."%","SAY");
  [[API SendChatMessage|SendChatMessage]]("Block: "..[[API GetBlockChance|GetBlockChance()]].."%","SAY");
<!-- end code -->
 
== How it works ==
This function simply loops through your spellbook ([[WoW Constants#Miscellaneous|BOOKTYPE_SPELL]]) for your regular melee attack skill and then grabs the text from the skill's tooltip using [[Widget API#GameTooltip|GameTooltip]] functions (the tooltip of the regular melee attack is the only place where the crit chance is displayed).
 
== Credits ==
Much of this [[GetCritChance|GetCritChance()]] function was taken and (slightly) modified from [http://ui.worldofwar.net/ui.php?id=722 mercdev's Combat Info] ([http://www.curse-gaming.com/mod.php?addid=1208 alt]) for [[Titan_Panel|Titan Panel]]. Thanks to him.


----
----
{{WoW API}}
{{WoW API}}

Revision as of 14:02, 19 December 2006

Returns

The player's melee critical hit chance in an unformatted, floating-point figure.

Template:WoW API