This article relates to the game "Dota 2". Click here for more information.
This article relates to the SDK/Workshop Tools for "Dota 2 Workshop Tools". Click here for more information.
This article's documentation is for Source 2. Click here for more information.

Abilities Scripted Example

From Valve Developer Community
Jump to navigation Jump to search

By calling script functions, it is possible to create an item that drops upon death.

	"item_divine_rapier_datadriven"
	{
		"ID"						"2100"
		"AbilityBehavior"				"DOTA_ABILITY_BEHAVIOR_PASSIVE"
		"BaseClass"					"item_datadriven"
		"Model"						"models/props_gameplay/divine_rapier.vmdl"
		"AbilityTextureName"				"rapier"


		// Item Info
		//---------------------------------------------------------------------------------------------------------
		"ItemCost"					"6200"
		"ItemDroppable"					"0"
		"ItemSellable"					"0"
		"ItemKillable"					"0"
		"ItemQuality"					"epic"
		"ItemShopTags"					"damage"
		"ItemAliases"					"rapier"
		"ItemShareability"				"ITEM_NOT_SHAREABLE"
		"ItemDeclarations"				"DECLARE_PURCHASES_TO_TEAMMATES | DECLARE_PURCHASES_TO_SPECTATORS | DECLARE_PURCHASES_IN_SPEECH"
		"OnOwnerDied"
		{
			"RunScript"
			{
				"ScriptFile"			"scripts/vscripts/itemfunctions.lua"
				"Function"			"DropItemOnDeath"
			}
		}

		"Modifiers"
		{
			"modifier_rapier"
			{
				"Passive"			"1"
				"IsHidden"			"1"
				"Properties"
				{
					"MODIFIER_PROPERTY_BASEATTACK_BONUSDAMAGE" 			"%bonus_damage"
				}
			}
		}
		// Special	
		//---------------------------------------------------------------------------------------------------------
		"AbilitySpecial"
		{
			"01"
			{
				"var_type"			"FIELD_INTEGER"
				"bonus_damage"			"300"
			}
		}
	}

"OnOwnerDied" - runs the next lines whenever the owner dies. "RunScript" - runs the following Function from the designated ScriptFile

Inside of itemfunctions.lua is the following code:

if itemFunctions == nil then
	print ( '[ItemFunctions] creating itemFunctions' )
	itemFunctions = {} -- Creates an array to let us beable to index itemFunctions when creating new functions
	itemFunctions.__index = itemFunctions
end

function itemFunctions:new() -- Creates the new class
	print ( '[ItemFunctions] itemFunctions:new' )
	o = o or {}
	setmetatable( o, itemFunctions )
	return o
end

function itemFunctions:start() -- Runs whenever the itemFunctions.lua is ran
	print('[ItemFunctions] itemFunctions started!')
end

function DropItemOnDeath(keys) -- keys is the information sent by the ability
	print( '[ItemFunctions] DropItemOnDeath Called' )
	local killedUnit = EntIndexToHScript( keys.caster_entindex ) -- EntIndexToHScript takes the keys.caster_entindex, which is the number assigned to the entity that ran the function from the ability, and finds the actual entity from it.
	local itemName = tostring(keys.ability:GetAbilityName()) -- In order to drop only the item that ran the ability, the name needs to be grabbed. keys.ability gets the actual ability and then GetAbilityName() gets the configname of that ability such as juggernaut_blade_dance.
	if killedUnit:IsHero() or killedUnit:HasInventory() then -- In order to make sure that the unit that died actually has items, it checks if it is either a hero or if it has an inventory.
		for itemSlot = 0, 5, 1 do --a For loop is needed to loop through each slot and check if it is the item that it needs to drop
	        	if killedUnit ~= nil then --checks to make sure the killed unit is not nonexistent.
                		local Item = killedUnit:GetItemInSlot( itemSlot ) -- uses a variable which gets the actual item in the slot specified starting at 0, 1st slot, and ending at 5,the 6th slot.
                		if Item ~= nil and Item:GetName() == itemName then -- makes sure that the item exists and making sure it is the correct item
                			local newItem = CreateItem(itemName, nil, nil) -- creates a new variable which recreates the item we want to drop and then sets it to have no owner
                    			CreateItemOnPositionSync(killedUnit:GetOrigin(), newItem) -- takes the newItem variable and creates the physical item at the killed unit's location
                    			killedUnit:RemoveItem(Item) -- finally, the item is removed from the original units inventory.
                		end
	        	end
		end
	end
end