• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[Solved] Function not running

Status
Not open for further replies.
So I wrote this library and for some reason the function Activate is refusing to run. I tried to call it outside of the library via the ActivateEx version with the line call EssenceCollection_ActivateEx(GetTriggerUnit()) but still nothing happens.

JASS:
library EssenceCollection initializer init requires MOD, GetClosestWidget, SpellEffectEvent

    globals
        private unit array SourceUnit
        private real array LumberTime
        private lightning array EssenceBeam
        private destructable array Tree
        public boolean array IsOrderDeactivate
        private timer EC_Timer
        private integer Active_EC
        //Constants
        private constant real RANGE = 1500.
        private constant real TIMEOUT = 0.03125
        private constant real Z_OFFSET_TREE = 120.
        private constant real Z_OFFSET_SOURCE = 80.
        private constant real LUMBER_TIME_RESET = 8.
        private constant integer LUMBER_HARVESTED = 5
        private constant string LUMBER_BEAM_EFFECT = "GRBM"
        private constant string LUMBER_SOURCE_EFFECT = "Units\\NightElf\\Owl\\Owl.mdl"
        private constant integer ESSENCE_SIPHON_DISABLED = 'A004'
        private constant integer ESSENCE_SIPHON_OFF = 'A008'
        private constant integer ESSENCE_SIPHON_ON = 'A007'
    endglobals
  
    private function IsTreeViable takes nothing returns boolean
        local destructable tree = GetFilterDestructable()
        if IsTreeAlive(tree) and not IsDestructableInGroup(tree, MOD_IsBeingHarvested) then
            return true
        endif
        return false
    endfunction
  
    private function EssenceCollectionTimer takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer iLoop = 0
        local integer iRecycle = 0
        local integer id = 0
        local texttag lumber_text
        local real x_source = 0.
        local real y_source = 0.
        local real z_source = 0.
        local real x_tree = 0.
        local real y_tree = 0.
        local real z_tree = 0.
        loop
            set iLoop = iLoop + 1
            exitwhen iLoop > Active_EC
            set id = GetUnitUserData(SourceUnit[iLoop])
            if Tree[iLoop] == null then
                set x_source = GetUnitX(SourceUnit[iLoop])
                set y_source = GetUnitY(SourceUnit[iLoop])
                set Tree[iLoop] = GetClosestDestructableInRange(x_source, y_source, RANGE, Filter(function IsTreeViable))
                if Tree[iLoop] == null then
                    call IssueImmediateOrderById(SourceUnit[iLoop], 852178) //order unimmolate
                    //call IssueImmediateOrderById(SourceUnit[iLoop], 852600) //order channel
                else
                    call GroupAddDestructable(MOD_IsBeingHarvested, Tree[iLoop])
                    set x_tree = GetDestructableX(Tree[iLoop])
                    set y_tree = GetDestructableY(Tree[iLoop])
                    call MoveLocation(MOD_TERRAIN_Z_LOC, x_tree, y_tree)
                    set z_source = GetUnitFlyHeight(SourceUnit[iLoop]) + GetLocationZ(MOD_TERRAIN_Z_LOC) + Z_OFFSET_SOURCE
                    set z_tree = GetLocationZ(MOD_TERRAIN_Z_LOC) + Z_OFFSET_TREE
                    set EssenceBeam[iLoop] = AddLightningEx(LUMBER_BEAM_EFFECT, true, x_source, y_source, z_source, x_tree, y_tree, z_tree)
                endif
            else
                set LumberTime[iLoop] = LumberTime[iLoop] - TIMEOUT * ECLoadUnload_CountUnits[id]
                if LumberTime[iLoop] <= 0. then
                    //Reset Time
                    set LumberTime[iLoop] = LUMBER_TIME_RESET
                    //Increase Player Lumber
                    call SetPlayerState( GetOwningPlayer(SourceUnit[iLoop]), PLAYER_STATE_RESOURCE_LUMBER, /*
                    */ GetPlayerState(GetOwningPlayer(SourceUnit[iLoop]), PLAYER_STATE_RESOURCE_LUMBER) + LUMBER_HARVESTED )
                    //Create Text Tag
                    set lumber_text = CreateTextTag()
                    call SetTextTagText(lumber_text, "+" + I2S(LUMBER_HARVESTED), .023)
                    call SetTextTagPos(lumber_text, GetDestructableX(Tree[iLoop]), GetDestructableY(Tree[iLoop]), 100.00)
                    call SetTextTagColor(lumber_text, 0, 200, 80, 255)
                    call SetTextTagPermanent(lumber_text, false)
                    call SetTextTagLifespan(lumber_text, 2.0)
                    call SetTextTagFadepoint(lumber_text, 1.0)
                    call SetTextTagVelocity(lumber_text, .0355 * Cos(1.5708), .0355 * Sin(1.5708))
                    call SetTextTagVisibility(lumber_text, (GetLocalPlayer() == GetOwningPlayer(SourceUnit[iLoop])))
                    //Reduce Tree Life
                    call SetDestructableLife(Tree[iLoop], (GetDestructableLife(Tree[iLoop]) - LUMBER_HARVESTED))
                    if IsDestructableDead(Tree[iLoop]) then
                        set Tree[iLoop] = null
                        if EssenceBeam[iLoop] != null then
                            call DestroyLightning(EssenceBeam[iLoop])
                            set EssenceBeam[iLoop] = null
                        endif
                    endif
                endif
            endif
            if not UnitAlive(SourceUnit[iLoop]) or IsOrderDeactivate[id] or  ECLoadUnload_CountUnits[id] < 1 then
                set IsOrderDeactivate[id] = false
                if EssenceBeam[iLoop] != null then
                    call DestroyLightning(EssenceBeam[iLoop])
                    set EssenceBeam[iLoop] = null
                endif
                set iRecycle = iLoop
                loop
                    exitwhen iRecycle > Active_EC
                    set SourceUnit[iRecycle] = SourceUnit[iRecycle + 1]
                    set LumberTime[iRecycle] = LumberTime[iRecycle + 1]
                    set iRecycle = iRecycle + 1
                endloop
                set Active_EC = Active_EC - 1
            endif
        endloop
        if Active_EC < 1 then
            call DestroyTimer(t)
        endif
        set t = null
    endfunction
  
    public function ActivateEx takes unit source returns nothing
        set Active_EC = Active_EC + 1
        set SourceUnit[Active_EC] = source
        set LumberTime[Active_EC] = LUMBER_TIME_RESET
        call BJDebugMsg("Lumber Beam ON")
        //call UnitRemoveAbility(source, ESSENCE_SIPHON_OFF)
        //call UnitAddAbility(source, ESSENCE_SIPHON_ON)
        if EC_Timer == null then
            set EC_Timer = CreateTimer()
            call TimerStart(EC_Timer, TIMEOUT, true, function EssenceCollectionTimer)
        endif
    endfunction
  
    private function Activate takes nothing returns nothing
        local unit source = GetTriggerUnit()
        set Active_EC = Active_EC + 1
        set SourceUnit[Active_EC] = source
        set LumberTime[Active_EC] = LUMBER_TIME_RESET
        call BJDebugMsg("Lumber Beam ON")
        //call UnitRemoveAbility(source, ESSENCE_SIPHON_OFF)
        //call UnitAddAbility(source, ESSENCE_SIPHON_ON)
        if EC_Timer == null then
            set EC_Timer = CreateTimer()
            call TimerStart(EC_Timer, TIMEOUT, true, function EssenceCollectionTimer)
        endif
        set source = null
    endfunction
  
    private function Deactivate takes nothing returns nothing
        local unit source = GetTriggerUnit()
        set IsOrderDeactivate[GetUnitUserData(source)] = true
        //call UnitRemoveAbility(source, ESSENCE_SIPHON_ON)
        //call UnitAddAbility(source, ESSENCE_SIPHON_OFF)
        set source = null
    endfunction
  
    private function init takes nothing returns nothing
        call RegisterSpellEffectEvent(ESSENCE_SIPHON_OFF, function Activate)
        //call RegisterSpellEffectEvent(ESSENCE_SIPHON_ON, function Deactivate)
    endfunction
  
endlibrary

I've tried to run Activate every way I can think of. I tried giving this to different types of units/buildings, commenting out the entire ability removal/add that you can see in the code above, or running the code by trigger. For example:
  • Untitled Trigger 001
    • Events
      • Time - Elapsed game time is 1.00 seconds
    • Conditions
    • Actions
      • Custom script: local integer ID = 0
      • Set TempUnit = Gryphon Rider 0214 <gen>
      • Custom script: set ID = GetUnitUserData(udg_TempUnit)
      • Custom script: set ECLoadUnload_CountUnits[ID] = 3
      • Unit - Add Essence Siphon (Off) to TempUnit
      • Custom script: call EssenceCollection_ActivateEx(udg_TempUnit)
I thought that for some reason RegisterSpellEffectEvent was nor working (despite working for other spells on that same map). I tried going the GUI way to trigger spell and while the message "Test" does print, the ActivateEx line does nothing.
  • Untitled Trigger 002
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Essence Siphon (Off)
    • Actions
      • Custom script: call BJDebugMsg("Test")
      • Custom script: call EssenceCollection_ActivateEx(GetTriggerUnit())
Nothing works. The message "Lumber Beam ON" never prints. What am I missing?
 
Status
Not open for further replies.
Top