Hero Starts Game With Ability Learned

edited August 9 in Tutorials

Requirements:

Have Barebones installed. If you don't know what that is or need some help, click here.

Scenario:

You have a hero that has an ability you would like them to have already learned when they spawn or are created. Prelearned.

Problem:

You notice that none of the ability 'Behaviors' are able to do this...(why valve?)

Solution and Example:

Have the game check to see if your hero has the particular ability you want auto leveled and have it set the level to one(or whatever you want it to be).

Navigate to: "C:\Program Files (x86)\Steam\steamapps\common\dota 2 beta\game\dota_addons\"Your Addon Name"\scripts\vscripts" and open the 'gamemode.lua file, look for the following section:

function GameMode:OnHeroInGame(hero)

Now paste the following in beneath it:

if hero:HasAbility("ability_name") then hero:FindAbilityByName("ability_name"):SetLevel(1) end

Essentially, it checks the abilities of your heroes when they spawn into the game, looking for the specific one you mention in the first parentheses, then if it can find it, it will set the level to the number you choose.

Notes: This might seem like a silly tutorial to post, but honestly this took me some time to figure out, anything I struggle with, others are bound to run into the same issue. This tutorial is not for the veteran modder, it's for newbs like me who are trying to learn but do best with examples.

If my code is wrong or could be better, or someone knows a better solution, I am happy to learn it if you'll share it. :) Thanks!

Comments

  • Posts: 254

    If you have more heroes with innate or auto-leveled-on-start abilities. Instead of doing this:

    if hero:HasAbility("ability_name1") then hero:FindAbilityByName("ability_name1"):SetLevel(1) end
    if hero:HasAbility("ability_name2") then hero:FindAbilityByName("ability_name2"):SetLevel(1) end
    if hero:HasAbility("ability_name3") then hero:FindAbilityByName("ability_name3"):SetLevel(1) end
    ...
    

    or this:

    if hero:HasAbility("ability_name1") then 
       hero:FindAbilityByName("ability_name1"):SetLevel(1)
    elseif hero:HasAbility("ability_name2") then
       hero:FindAbilityByName("ability_name2"):SetLevel(1)
    elseif
       ...
    end
    

    Its better to do this:

    local innate_abilities = {
       "ability_name1",
       "ability_name2",
       "ability_name3",
       "ability_name4" 
    }
    
    -- Cycle through any innate abilities found, then level them
    for i = 1, #innate_abilities do
       local current_ability = hero:FindAbilityByName(innate_abilities[i])
       if current_ability then
          current_ability:SetLevel(1)
       end
    end
    
  • Thank you!

    That is really helpful!

  • Hello

    this works for no-hero units? i want to make an ability shared for creeps and heros in the same time.