[Snippet] SkillBuild

Level 33
Joined
Apr 24, 2012
Messages
5,121
JASS:
library SkillBuild /* v1.3
*************************************************************************************
*
*    AI System Member
*
*    Allows AIs to automatically learn abilites
*
*************************************************************************************
*
*    */ uses /*
*
*    */ RegisterPlayerUnitEvent /*
*     - hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/
*
*************************************************************************************
*
*    API
*
*    static method add takes integer heroId, integer spell, integer level returns nothing
*        - adds a spell to the hero's to-learn list.
*        - spells will be learned at "level"
*
*************************************************************************************
*
*    Credits
*
*        Magtheridon96 - RegisterPlayerUnitEvent
*
*************************************************************************************/
    private module SBInit
        private static method onInit takes nothing returns nothing
            call init()
        endmethod
    endmodule
    
    struct SkillBuild extends array
        private static hashtable ht
        private static unit u
        
        static method add takes integer heroId, integer spell, integer level returns nothing
            call SaveInteger(ht, heroId, level, spell)
        endmethod

        private static method learn takes nothing returns boolean
		    local integer spell
            set u = GetTriggerUnit()
            if GetPlayerController(GetTriggerPlayer()) == MAP_CONTROL_COMPUTER and IsUnitType(u, UNIT_TYPE_HERO)  then
			    set spell = LoadInteger(ht, GetUnitTypeId(u), GetUnitLevel(u)) 
			    if 0 != spell then
                    call SelectHeroSkill(u, spell)
				endif
            endif
            return false
        endmethod
        
        private static method init takes nothing returns nothing
            set ht = InitHashtable()
            call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_LEVEL, function SkillBuild.learn)
        endmethod
        
        implement SBInit
    endstruct
endlibrary

Demo:
JASS:
struct Test
    private static method onInit takes nothing returns nothing
        local integer heroId = 'Hmkg'
        call SkillBuild.add(heroId, 'AHtc', 1)
        call SkillBuild.add(heroId, 'AHtc', 4)
        call SkillBuild.add(heroId, 'AHtc', 7)
    endmethod
endstruct
 
Last edited:
Since the creation of an instance here is meaningless, how about simply:

call SkillBuild['Hmkg'].add('AHtc', 1)

(You'd just provide a static method operator [] that returns the argument)

Oh, and you might want to check if there's anything stored in the hashtable before you actually make a hero learn an ability :p

Also, you might want to update the library to have the same name as the struct, it's a convention :v
 
Level 17
Joined
Jul 17, 2011
Messages
1,864
so this will add an ability to the hero when he she levels up? well thats nice i guess but you should make this work for player heroes too also if say a hero is "loaded" into the game via a save load system then you should add a function that re adds the hero's abilities so i duno make a loop that sets the hero's lvl +1 i could use something like this :p also i think other people can too since one problem with loading heroes with new save load systems is that usually they wont have standard abilities preplaced on them in the object editor and save load systems dont have a function like that
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
dont use your own hashtable, use Bribe's Table, that's what it is made for, to only have 1 hashtable for entire map in vJass

you could make it work for each unit independantly not for unit type, that is only suggestion tho
 
Top