This article relates to the game "Dota 2". Click here for more information.
This article relates to the workshop tools for "Dota 2". 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, search

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

		"ID"						"2100"
		"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"
				"ScriptFile"			"scripts/vscripts/itemfunctions.lua"
				"Function"			"DropItemOnDeath"

				"Passive"			"1"
				"IsHidden"			"1"
		// Special	
				"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

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

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

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.