Hashtable help/questions

Discussion in 'World Editor Help' started by afisakov, Sep 15, 2017.

  1. afisakov

    afisakov You can change this now in User CP.

    Ratings:
    +38 / 0 / -0
    Hello, I am very new to using hashtables
    some Hopefully quick questions
    1) does "RemoveSavedHandle" remove the unit like it sounds, or only destroy the reference and leave unit itself intact
    2) does LoadItemHandle truly load/create something that needs to be removed? or is it more similar to referencing a variable
    e.g. can I use SetUnitPosition(LoadUnitHandle(1,2), x, y) or do I need to use
    Code:
    set dummy= LoadUnitHandle(1,2)
    call SetUnitPosition(dummy, x, y)
    set dummy= null 
     
  2. GFreak45

    GFreak45 I didnt slap you, i high 5'd your face.

    Ratings:
    +132 / 0 / -0
    Hashtables are extremely similar to variables with a few key differences and similarities:

    Similarly:
    Loading handles can replace the use of a variable of the same type
    Setting null is the equivelant to removing a saved handle, however, the saved handle is not scope restricted, so it should be released when the handle is destroyed rather than at the end of a function etc

    Differences (aka Efficiency vs Utility):
    As everyone knows variables are relatively limited as far as array max size and global definitions etc, this is where hashtables are wonderfull... They are like a seemingly endless nested array that can use text values instead similar to json key value pairs etc.

    Where hashtables fall short is their efficiency... while they have almost the same exact uses as variables it is less efficienct to save and load them than it is to access a variable, making them more usefull for thinks like long term storage. Typically if values are to be used more than once in a single function they are loaded at the beginning, and set to a local variable.
     
  3. afisakov

    afisakov You can change this now in User CP.

    Ratings:
    +38 / 0 / -0
    Can someone take a look at my trigger and see if I did this right?
    I already tested that it functions in-game, but wanted to check thtat did not miss any leaks/cleanups, especiailly in regards to hashtables.

    The spell creates ice pillars around target point, then they push inwards, compressing any units inside, deals damage and applies debuff at very end.

    Activation
    Code:
    globals
       location array PillarSpot
       hashtable Pillars= InitHashtable()
    endglobals
    
    function Trig_Ice_Pillars_Conditions takes nothing returns boolean
        return GetSpellAbilityId() == 'A098'
    endfunction
    
    function Trig_Ice_Pillars_Actions takes nothing returns nothing
    local integer pid= GetPlayerId(GetOwningPlayer(GetTriggerUnit())) + 1
    local integer i=0
    local unit u
    local real x
    local real y
    local real angle
    local real range= (240+80*GetUnitAbilityLevel(GetTriggerUnit(),'A098') )*(1.+ BoostValue[pid]*.5)
       set PillarSpot[pid]=GetSpellTargetLoc()
       set x= GetLocationX(PillarSpot[pid])
       set y= GetLocationY(PillarSpot[pid])
       loop
           exitwhen i>7
           set angle=bj_PI * 2 * i / 8.
           set u= CreateUnit(Player(15), 'e0xe', x + range*Cos(angle), y + range*Sin(angle), bj_UNIT_FACING)
           call UnitApplyTimedLife(u, 'BTLF', 10)
           set bj_lastCreatedEffect = AddSpecialEffectTarget("Doodads\\Icecrown\\Rocks\\IceBlock\\IceBlock3.mdl", u, "origin")
           call SaveEffectHandle(Pillars, pid, i+8, bj_lastCreatedEffect)
           call SaveUnitHandle(Pillars, pid, i, u)
           set u= null
           set i= i+1
       endloop
       call EnableTrigger(gg_trg_Ice_Pillars_Periodic)
    endfunction
    
    //===========================================================================
    function InitTrig_Ice_Pillars takes nothing returns nothing
        set gg_trg_Ice_Pillars=CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(gg_trg_Ice_Pillars, EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(gg_trg_Ice_Pillars, Condition(function Trig_Ice_Pillars_Conditions))
        call TriggerAddAction(gg_trg_Ice_Pillars, function Trig_Ice_Pillars_Actions)
    endfunction
    
    
    Effect
    Code:
    
    function Trig_Ice_Pillars_Periodic_Actions takes nothing returns nothing
    local integer i=0
    local integer i2
    local unit dummy
    local unit target
    local boolean activeCasts=false
    local real boost
    local real dist
    local real angle
    local real x
    local real y
    local group innerG
    local group outerG
    local real jump= 9
    local real dmg
    loop
       exitwhen i > 8
       set x=GetLocationX(PillarSpot[i])
       set y=GetLocationY(PillarSpot[i])
       if x!=0 then
    //
       set boost= 1. +BoostValue[i]
       set dummy= LoadUnitHandle(Pillars, i, 0)
       set dist= SquareRoot( (GetUnitX(dummy)-x)*(GetUnitX(dummy)-x) + (GetUnitY(dummy)-y)*(GetUnitY(dummy)-y) )
       set dummy= null
       if dist > 160 then
           set outerG=GetUnitsInRangeOfLocMatching(dist * boost, PillarSpot[i], Condition(function ishostileUnit))
           set dist= dist -jump
           set innerG=GetUnitsInRangeOfLocMatching(dist , PillarSpot[i], Condition(function ishostileUnit))
           set activeCasts=true
           set i2= 0
           loop //move dummy effects
               exitwhen i2>7
               set angle= bj_PI *2 *i2 / 8.
               call SetUnitPosition(LoadUnitHandle(Pillars, i, i2), x +dist*Cos(angle), y +dist*Sin(angle) )
               set i2=i2+1
           endloop
       
           set dist=dist*.9
           loop //move outlier units inward
               set target=FirstOfGroup(outerG)
               exitwhen target == null
               call GroupRemoveUnit(outerG, target)
               if IsUnitInGroup(target, innerG)==false then
                   set angle=Atan2((GetUnitY(target)-y), (GetUnitX(target)-x))
                   call SetUnitPosition(target, x +dist*Cos(angle), y +dist*Sin(angle) )
                   call SetUnitPathing(target, false)
               endif
               set target=null
           endloop
           call DestroyGroup(outerG)
           set outerG= null
           call DestroyGroup(innerG)
           set innerG= null
       else
           set i2=0
           loop
               exitwhen i2>7
               call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Undead\\FrostNova\\FrostNovaTarget.mdl", GetUnitX(LoadUnitHandle(Pillars, i, i2)), GetUnitY(LoadUnitHandle(Pillars, i, i2)) ))
               call DestroyEffect(LoadEffectHandle(Pillars, i, i2+8))
               call RemoveUnit(LoadUnitHandle(Pillars, i, i2))
               set i2= i2+1
           endloop
           call FlushChildHashtable(Pillars,i)
           call DestroyEffect(AddSpecialEffectLoc("Abilities\\Spells\\Undead\\FrostNova\\FrostNovaTarget.mdl", PillarSpot[i] ))
           set outerG=GetUnitsInRangeOfLocMatching(dist * boost, PillarSpot[i], Condition(function ishostile))
           call RemoveLocation(PillarSpot[i])
           set dmg= GetHeroInt(udg_PlayersCharacter[i],true)*GetUnitAbilityLevel(udg_PlayersCharacter[i],'A098')*boost
           loop // damage at the end
               set target=FirstOfGroup(outerG)
               exitwhen target == null
               call GroupRemoveUnit(outerG, target)
               call SetUnitPathing(target, true)
               call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Undead\\FrostNova\\FrostNovaTarget.mdl", GetUnitX(target), GetUnitY(target)))
               if GetUnitAbilityLevel(target, 'B01G')>0 then
                   call UnitDamageTargetBJ(udg_PlayersCharacter[i], target, dmg*1.5*boost, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC)
                   call IssueTargetOrder(CreateDummyWithAbilityCoord(GetOwningPlayer(udg_PlayersCharacter[i]) , 'A0DS' , R2I(3*boost) , GetUnitX(target), GetUnitY(target)), "thunderbolt", target)
               else
                   call UnitDamageTargetBJ(udg_PlayersCharacter[i], target, dmg, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC)
                   call IssueTargetOrder(CreateDummyWithAbilityCoord(GetOwningPlayer(udg_PlayersCharacter[i]) , 'A0DS' , R2I(2*boost) , GetUnitX(target), GetUnitY(target)), "thunderbolt", target)
               endif
               set target=null
           endloop
           call DestroyGroup(outerG)
           set outerG= null
       endif
    //
       endif
       set i=i + 1
    endloop
    
    if activeCasts == false then
       call DisableTrigger(gg_trg_Ice_Pillars_Periodic)
    endif
    endfunction
    
    //===========================================================================
    function InitTrig_Ice_Pillars_Periodic takes nothing returns nothing
        set gg_trg_Ice_Pillars_Periodic=CreateTrigger()
        call TriggerRegisterTimerEventPeriodic(gg_trg_Ice_Pillars_Periodic, 0.03)
        call TriggerAddAction(gg_trg_Ice_Pillars_Periodic, function Trig_Ice_Pillars_Periodic_Actions)
    endfunction
    
    

    Thank you in advance
     

Share This Page