WoW:API GetCritChance: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Added 1.10 fix to GameTooltip -- :SetOwner)
m (Move page script moved page API GetCritChance to API GetCritChance without leaving a redirect)
 
(5 intermediate revisions by 5 users not shown)
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.
{{wowapi}} __NOTOC__


== 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 ===
Gets the player's critical hit chance percentage.
:Player's critical hit chance, in percentage (''without the % symbol'').
critChance = GetCritChance()
:'''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 ==
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:
== Arguments ==
<!-- begin code -->
:none
  -- 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 ==
=== Returns ===
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.
:; critChance : Number - The player's melee critical hit chance in an unformatted, floating-point figure.
 
----
{{WoW API}}

Latest revision as of 04:45, 15 August 2023

WoW API < GetCritChance


Gets the player's critical hit chance percentage.

critChance = GetCritChance()


Arguments[edit]

none


Returns[edit]

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