Module:Abilities: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
LVL (talk | contribs)
Fixed get_ability_prop to return element.Value for table props
LVL (talk | contribs)
get_ability_prop can now lookup abilities by their name or by their key
Line 4: Line 4:
local p = {}
local p = {}


-- returns the table of a specific ability
-- returns the table of a specific ability from Data:AbilityCards.json
function get_ability(hero_key, ability_num)
function get_ability(hero_key, ability_num)
local ui_data = data[hero_key]
local ui_data = data[hero_key]
Line 11: Line 11:
end
end


-- Returns the table of a specific ability from Data:AbilityData
-- Returns the table of a specific ability from Data:AbilityData.json by its Key or Name
-- @function  get_json_ability
-- It first attempts a direct lookup by key for efficiency. If that fails, it iterates
-- @param      {string}
-- through the data to find a matching Name property.
-- @return    {table}
-- @function  find_json_ability
local function get_json_ability(name)
-- @param      {string} identifier - The ability's key or name
-- @return    {table} or nil
local function find_json_ability(identifier)
-- First, try a direct lookup by key (most efficient method)
if raw_ability_data[identifier] then
return raw_ability_data[identifier]
end
-- If not found by key, loop through and search by the "Name" property as a fallback
for _, v in pairs(raw_ability_data) do
for _, v in pairs(raw_ability_data) do
if (v["Name"] == name) then
if (v["Name"] == identifier) then
return v
return v
end
end
end
end
-- If not found by either method, return nil
return nil
return nil
end
end
Line 34: Line 44:
end
end


--{{#invoke:Abilities|get_ability_prop|ability_name|prop1|upgrade_tier|prop3}}--
--{{#invoke:AbilityData|get_ability_prop|ability_key_or_name|prop}}--
p.get_ability_prop = function(frame)
p.get_ability_prop = function(frame)
local tableValues = {}
local ability_identifier = frame.args[1]
local ability_name = frame.args[1]
local prop = frame.args[2]
local prop1 = frame.args[2]
local upgrade_tier = frame.args[3]
     -- Use the new, more flexible lookup function
local number_int = tonumber(upgrade_tier)
local ability = find_json_ability(ability_identifier)
     --find ability JSON name--
local ability = get_json_ability(ability_name)
if(ability == nil) then return "Ability Not Found" end
if(ability == nil) then return "Ability Not Found" end
--identify if prop1 is a table, then use prop2--
 
local element = ability[prop1]
local element = ability[prop]
if element == nil then return "" end
if element == nil then return "" end



Revision as of 18:18, 13 November 2025

Overview

The Abilities module provides functions for retrieving data about hero abilities and their upgrades.

  • The function get_ability_name references Data:AbilityCards.json to link a hero and ability slot to an ability's display name.
  • The functions get_ability_prop and get_ability_upgrade_prop reference Data:AbilityData.json to get specific stats from an ability's base data or its upgrades.

Examples

get_ability_name

To get the localized display name of a hero's ability (numbered 1 to 4):

{{#invoke:Abilities|get_ability_name|hero_atlas|1}}

Returns: Siphon Life

Function can be passed with lang parameter to override the current language to a specific language code.

{{#invoke:Abilities|get_ability_name|hero_atlas|1|lang=ru}}

Returns: Siphon Life

get_ability_prop

To get a property from the base stats of an ability. You can use the ability's key (e.g., "ability_afterburn") or its proper name (e.g., "Afterburn").

{{#invoke:Abilities|get_ability_prop|Afterburn|BurnDuration}}

Returns: 3

You can also retrieve nested properties by using dot notation to drill down into the data structure. For example, to get a value nested inside a Scale table:

{{#invoke:Abilities|get_ability_prop|Lycan Curse|MaxRage.Scale.Value}}

Returns:

get_ability_upgrade_prop

To get a property from a specific upgrade of an ability.

{{#invoke:Abilities|get_ability_upgrade_prop|Afterburn|3|DPS}}

This example gets the DPS value from the 3rd upgrade for the ability "Afterburn".

Returns: Script error: The function "get_ability_upgrade_prop" does not exist.

Another example, getting the `DamageAmplificationPerStack` from the 2nd upgrade of "Marksman":

{{#invoke:Abilities|get_ability_upgrade_prop|Marksman|2|DamageAmplificationPerStack}}

Returns: Script error: The function "get_ability_upgrade_prop" does not exist.

Submodules

Abilities - Simple functions. Eg. getting ability name

Abilities/utils - Common internal functions that are shared amongst any Abilities/ modules

Abilities/card - Generates hero ability cards

Abilities/icon - Functions for retrieving ability and attribute icon data

Abilities/details table (WIP) - Generates details table to show raw ability data


local data = mw.loadJsonData("Data:AbilityCards.json")
local raw_ability_data = mw.loadJsonData("Data:AbilityData.json")

local p = {}

-- returns the table of a specific ability from Data:AbilityCards.json
function get_ability(hero_key, ability_num)
	local ui_data = data[hero_key]
	if(ui_data == nil) then return "Hero Not Found" end
	return ui_data[tonumber(ability_num)]
end

-- Returns the table of a specific ability from Data:AbilityData.json by its Key or Name
-- It first attempts a direct lookup by key for efficiency. If that fails, it iterates
-- through the data to find a matching Name property.
-- @function   find_json_ability
-- @param      {string} identifier - The ability's key or name
-- @return     {table} or nil
local function find_json_ability(identifier)
	-- First, try a direct lookup by key (most efficient method)
	if raw_ability_data[identifier] then
		return raw_ability_data[identifier]
	end
	
	-- If not found by key, loop through and search by the "Name" property as a fallback
	for _, v in pairs(raw_ability_data) do
		if (v["Name"] == identifier) then
			return v
		end
	end
	
	-- If not found by either method, return nil
	return nil
end

--{{#invoke:AbilityData|get_ability_name|HERO_NAME|ABILITY_NUM}}--
p.get_ability_name = function(frame)
	local hero_name = frame.args[1]
	local ability_num = frame.args[2]
	
	local ability = get_ability(hero_name, ability_num)
	if(ability == nil) then return "Ability Not Found" end
	return ability.Name
end

--{{#invoke:AbilityData|get_ability_prop|ability_key_or_name|prop}}--
p.get_ability_prop = function(frame)
	local ability_identifier = frame.args[1]
	local prop = frame.args[2]
	
    -- Use the new, more flexible lookup function
	local ability = find_json_ability(ability_identifier)
	if(ability == nil) then return "Ability Not Found" end

	local element = ability[prop]
	if element == nil then return "" end

	-- if the property is a table, return its Value field
	if type(element) == "table" then
	    return element.Value or ""
	end

	-- otherwise return the scalar value
	return element
end

return p