1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[vJASS] Unit array not working

Discussion in 'Triggers & Scripts' started by Krogoth, Jul 24, 2013.

  1. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    Here is work code:
    Code (vJASS):
    //========================
    //=== SpellSpiritScout ===
    //======== v1.000 ========

    library SpellSpiritScout requires Assist, Cast, Teams, TimedLife

    //=== Settings ===
    globals
        private constant integer ABILCODE = 'X108'
        private constant integer SUMMONER = 'd003'
        private constant integer SUMMON_ABILITY = 'X121'
        private constant integer SUMMON_ORDER = 852274
        private constant string ILLUSION_SFX = "SpiritScout.mdl"
        private constant string ILLUSION_SFX_ATTACH_POINT = "origin"
        private constant string EXPLOSION_SFX = "SpiritScoutExplosion.mdl"
    //----------------
        private constant real HERO_HUNT_AOE = 600
        private constant real EXPLOSION_AOE = 350
        private constant real ACQUIRE_RANGE = 99999
        private constant real SUMMONER_LIFETIME = 5
    //----------------
        private constant unittype SPEC_ILLUSION_UNIT_TYPE = UNIT_TYPE_SAPPER
    endglobals
        struct SpellSpiritScout extends array
            private static real array AOE
            private static integer array NUM
            private static real array DURATION
            private static real array SPAWN_TIME
            private static real array DAMAGE
            static if LIBRARY_ManaLoss then
                private static real array MANA_COST
            endif
            private static method onInit takes nothing returns nothing
                set AOE[1] = 800
                set AOE[2] = 750
                set AOE[3] = 700
                set AOE[4] = 650
                set NUM[1] = 1
                set NUM[2] = 5
                set NUM[3] = 6
                set NUM[4] = 6
                set DURATION[1] = 5
                set DURATION[2] = 5
                set DURATION[3] = 5
                set DURATION[4] = 5
                set SPAWN_TIME[1] = .6
                set SPAWN_TIME[2] = .65
                set SPAWN_TIME[3] = .7
                set SPAWN_TIME[4] = .75
                set DAMAGE[1] = 50
                set DAMAGE[2] = 80
                set DAMAGE[3] = 110
                set DAMAGE[4] = 140
                static if LIBRARY_ManaLoss then
                    set MANA_COST[1] = 110
                    set MANA_COST[2] = 120
                    set MANA_COST[3] = 130
                    set MANA_COST[4] = 140
                endif
    //================
                set scout = scout.create()
            endmethod
           
            private static indexCarrier scout
            private static integer array caster_id
            private static integer array level
            private static real array x
            private static real array y
            private static unit array summoner
            private static integer array count
            private static group array g
            private static timer array t
           
            private static integer workInteger
            private static real workReal
            private static player workPlayer
            private static unit workUnit
           
            private static method summon takes nothing returns nothing
                local timer t = GetExpiredTimer()
                set workInteger = 0
                loop
                    exitwhen thistype.t[workInteger] == t //or workInteger == scout.count
                    set workInteger = workInteger + 1
                endloop
                /*if workInteger == scout.count then
                    call BJDebugMsg("FUCK")
                    return
                endif*/

                call IssueTargetOrderById(summoner[workInteger], SUMMON_ORDER, Unit[caster_id[workInteger]])
                set t = null
            endmethod
           
            private static method hunt takes nothing returns nothing
                local unit u = GetEnumUnit()
                set workInteger = GetUnitUserData(u)
                if UnitData.Unit[workInteger] == null then
                    set UnitData.Unit[workInteger] = Assist.workUnit
                    call IssueTargetOrder(u, "attack", Assist.workUnit)
                endif
                set u = null
            endmethod
           
            static method closestHeroFilter takes nothing returns boolean
                local unit u = GetFilterUnit()
                if IsUnitEnemy(u, workPlayer) and GetWidgetLife(u) > 0 and IsUnitType(u, UNIT_TYPE_HERO) then
                    set u = null
                    return true
                endif
                set u = null
                return false
            endmethod

            static method onSummonUnit takes nothing returns nothing
                local real a
                local integer i
                set Assist.workUnit = GetSummoningUnit()
                if GetUnitTypeId(Assist.workUnit) == SUMMONER then
                    set workInteger = GetUnitUserData(Assist.workUnit)
                    set Assist.workUnit = GetTriggerUnit()
                    set i = GetUnitUserData(Assist.workUnit)
                    set workReal = GetRandomReal(0, AOE[level[workInteger]])
                    set a = GetRandomReal(0, 6.283)
                    call SetUnitPosition(Assist.workUnit, x[workInteger] + workReal * Cos(a), y[workInteger] + workReal * Sin(a))
                    call SetUnitAnimation(Assist.workUnit, "stand")
                    call DestroyEffect(AddSpecialEffectTarget(ILLUSION_SFX, Assist.workUnit, ILLUSION_SFX_ATTACH_POINT))
                    call SetUnitAcquireRange(Assist.workUnit, ACQUIRE_RANGE)
                    call UnitAddType(Assist.workUnit, SPEC_ILLUSION_UNIT_TYPE)
                    call GroupAddUnit(g[workInteger], Assist.workUnit)
                    call TimedLife.apply(Assist.workUnit, DURATION[level[workInteger]])
                    set UnitData.Integer[i] = workInteger
                    set UnitData.Unit[i] = null
                    if count[workInteger] == NUM[level[workInteger]] then
                        call scout.unregister(workInteger)
                        //set caster[workInteger] = null
                        set summoner[workInteger] = null
                        call PauseTimer(t[workInteger])
                    else
                        set count[workInteger] = count[workInteger] + 1
                    endif
                    //Hero search
                    set workPlayer = GetTriggerPlayer()
                    set Assist.workUnit = GetClosestUnit(GetUnitX(Assist.workUnit), GetUnitY(Assist.workUnit), HERO_HUNT_AOE, function thistype.closestHeroFilter)
                    if Assist.workUnit != null then
                        call ForGroup(g[workInteger], function thistype.hunt)
                    endif
                endif
            endmethod
           
            private static method affect takes nothing returns boolean
                set Assist.workUnit = GetFilterUnit()
                call BJDebugMsg("AFFECT")
                if IsUnitEnemy(Assist.workUnit, workPlayer) and GetWidgetLife(Assist.workUnit) > 0 and not IsUnitType(Assist.workUnit, UNIT_TYPE_STRUCTURE) and not IsUnitType(Assist.workUnit, UNIT_TYPE_MAGIC_IMMUNE) then
                    call BJDebugMsg("NICE")
                    //call DealDamage(caster[workInteger], Assist.workUnit, workReal, MAGIC)
                    call DealDamage(summoner[workInteger], Assist.workUnit, 100, MAGIC)
                endif
                return false
            endmethod
           
            static method onUnitDeath takes nothing returns nothing
                local real x
                local real y
                local integer i
                if GetKillingUnit() != null then
                    set Assist.workUnit = GetTriggerUnit()
                    if IsUnitIllusion(Assist.workUnit) and IsUnitType(Assist.workUnit, SPEC_ILLUSION_UNIT_TYPE) then
                        set x = GetUnitX(Assist.workUnit)
                        set y = GetUnitY(Assist.workUnit)
                        set i = GetUnitUserData(Assist.workUnit)
                        set workInteger = UnitData.Integer[i]
                        call GroupRemoveUnit(g[workInteger], Assist.workUnit)
                        set workReal = DAMAGE[level[workInteger]]
                        set workPlayer = GetTriggerPlayer()
                        set workUnit = Unit[caster_id[workInteger]]
                        call KillUnit(workUnit)
                        call GroupEnumUnitsInRange(Assist.workGroup, x, y, EXPLOSION_AOE, function thistype.affect)
                        call DestroyEffect(AddSpecialEffect(EXPLOSION_SFX, x, y))
                    endif
                endif
            endmethod

            static method onSpellEffect takes nothing returns nothing
                if GetSpellAbilityId() == ABILCODE then
                    set workInteger = scout.register()
                    set summoner[workInteger] = CreateUnit(Teams.headPlayer[TeamPlayer(GetTriggerPlayer())], SUMMONER, 0, 0, 0)
                    set Assist.workUnit = GetTriggerUnit()
                    set caster_id[workInteger] = GetUnitUserData(Assist.workUnit)
                    set level[workInteger] = GetUnitAbilityLevel(Assist.workUnit, ABILCODE)
                    set x[workInteger] = GetSpellTargetX()
                    set y[workInteger] = GetSpellTargetY()
                    call SetUnitUserData(summoner[workInteger], workInteger)
                    call UnitApplyTimedLife(summoner[workInteger], 0, SUMMONER_LIFETIME)
                    call UnitAddAbility(summoner[workInteger], SUMMON_ABILITY)
                    call SetUnitAbilityLevel(summoner[workInteger], SUMMON_ABILITY, level[workInteger])
                    if g[workInteger] == null then
                        set g[workInteger] = CreateGroup()
                        set t[workInteger] = CreateTimer()
                    else
                        call GroupClear(g[workInteger])
                    endif
                    set count[workInteger] = 1
                    call TimerStart(t[workInteger], SPAWN_TIME[level[workInteger]], true, function thistype.summon)
                    //Advanced Research
                    static if LIBRARY_SpellAdvancedResearch and LIBRARY_SpellPredatorSpirit then
                        if GetUnitAbilityLevel(Assist.workUnit, SpellAdvancedResearch.abilcode) != 0 then
                            call SpellAdvancedResearch.setPredatorSpirit()
                        endif
                    endif
                endif
            endmethod
        endstruct
    endlibrary

    What I am doing here:
    1. I target area
    2. Summon illusions
    3. Illusions explode upon death
    This works!
    But WTF, after 2 hours of idiotic debugging when I just replaced everywhere unit array caster by integer array caster_id and caster[x] by Unit[caster_id[x]]. (Using standard indexing) So, this means, unit is changed?? In which place I do it??
    :vw_wtf:
     
  2. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Check your JNGP backups folder for a version from before you replaced the variables. Then post the working code.

    I haven't really looked at your code yet since it is quite large, but having the original, working version with you will help you convert it properly.