• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

question about "Unit within range event"

Status
Not open for further replies.
Level 13
Joined
Jan 2, 2016
Messages
978
Is there a way to detect which unit has been aproached when this event runs?
GetTriggerUnit() gives the aproaching unit, not the aproached one.
I am asking this, cuz I'm dynamically creating such triggers, and at the moment I have to link the trigger with the unit via a hashtable, but it's kind a uncomfortable xP
 
Use a hashtable.
Yeah, I already am :P
But whenever a unit dies - I need to check if it has a saved trigger linked to it, then clear the table with the trigger's parent key, then destroy the trigger, and clear the table with the dying unit's parent key...
Would've been a bit better if I only had to load the trigger, destroy it and clear the unit's table :D
 
Does this work?
JASS:
library UnitInRangeEvent requires /*
    */ Table,               /* https://www.hiveworkshop.com/threads/snippet-new-table.188084/
    */ optional Typecasting /* http://www.thehelper.net/threads/typecasting.121176/
   
    UnitInRangeEvent UnitInRangeEvent.create(unit whichUnit,real range,boolexpr filter,boolexpr condition)
        - returns new instance of UnitInRangeEvent
   
    UnitInRangeEvent GetTriggeringUnitInRangeEvent()
        - returns instance whose condition is being executed
       
    unit .unit
        - returns the "whichUnit" you gave at create - function
       
    void .destroy()
        - requires Typecasting
        - destroys given instance
       
*/
   
// -----
   
    private module Init
       
        static method onInit takes nothing returns nothing
            set t = Table.create()
        endmethod
       
    endmodule
   
// -----
   
    struct UnitInRangeEvent extends array
       
        private static Table t
       
        method operator unit takes nothing returns unit
            return t.unit[this]
        endmethod
       
        static method create takes unit whichUnit, real range, boolexpr filter, boolexpr condition returns thistype
            local trigger trig = CreateTrigger()
            local thistype this = GetHandleId(trig)
           
            set t.unit[this] = whichUnit
            call TriggerRegisterUnitInRange(trig,whichUnit,range,filter)
            call TriggerAddCondition(trig,condition)
           
            set trig = null
            return this
        endmethod
       
        private method operator trigger takes nothing returns trigger
            static if(LIBRARY_Typecasting) then
                return Int2Trigger(this)
            else
                debug call BJDebugMsg("|cffff0000UnitInRangeEvent ERROR: Typecasting required|r")
                call R2I(1/0.)
                return null
            endif
        endmethod
       
        method disable takes nothing returns nothing
            call DisableTrigger(trigger)
        endmethod
        method enable takes nothing returns nothing
            call EnableTrigger(trigger)
        endmethod
        method operator isEnabled takes nothing returns boolean
            return IsTriggerEnabled(trigger)
        endmethod
        method destroy takes nothing returns nothing
            call DestroyTrigger(trigger)
        endmethod
       
        implement Init
       
    endstruct
   
// -----

    function GetTriggeringUnitInRangeEvent takes nothing returns UnitInRangeEvent
        return GetHandleId(GetTriggeringTrigger())
    endfunction
   
// -----
   
endlibrary

Edit: Updated, here's a demo:
JASS:
function MyCondition takes nothing returns nothing
    call BJDebugMsg(GetUnitName(GetTriggeringUnitInRangeEvent().unit)+" was approached")
endfunction

function InitTrig_Melee_Initialization takes nothing returns nothing
    call SelectUnit(CreateUnit(Player(0),'hfoo',0,0,270),true)
    call PanCameraToTimed(0,0,0)
    call UnitInRangeEvent.create(/*
        */ CreateUnit(Player(0),'hpea',350,0,270), /*
        */ 300,null,Filter(function MyCondition))
    call UnitInRangeEvent.create(/*
        */ CreateUnit(Player(0),'hrif',-350,0,270), /*
        */ 300,null,Filter(function MyCondition))
endfunction
 
Last edited by a moderator:
Status
Not open for further replies.
Back
Top