Darthfett
Aerospace/Cybersecurity Software Engineer
- Reaction score
- 615
Unit Enters Range
Created by Darthfett
Requires: CSData,PUI,BoolexprUtilsCreated by Darthfett
Created in vJASS
MUI
MPI
If you've ever used the Unit comes within range of another unit type events, you've probably realized one of the flaws of the event:
There is no "Get the unit which the entering unit came in range of" type function.
Well, that's what I created this system to solve. Basically, it creates a struct, attaches it to a trigger, and adds the UnitInRange event to the trigger. Then you can add your conditions and actions to the trigger.
About the System:
One Trigger Per Unit - Since the system does not use gamecache (to create unique string keys for each unit), it cannot attach multiple units to the same trigger. Therefore it has to create a unique trigger for each unit.
How To: Use the System -
1. Get something that supports vJASS (Newgen {Recommended}, JASSHelper
2. Create a Trigger named "EnterRange"
3. Copy-paste "System Code" into the "EnterRange" Trigger
4. Read the rest of the "How To"s to learn how to use the system.
This will create a trigger, and register the event for the unit, in one line.
How To: Get the Unit -
u now contains the entering unit. v now contains the "unit which the entering unit came in range of."
How To: Destroy the Trigger-Event -
If you have a unit, and you want to get rid of the UnitInRange trigger-event for the unit, simply call RemoveUnitEnterRangeEvent with the unit.
If you somehow have the trigger, and want to destroy it, use RemoveTriggerEnterRangeEvent, rather than DestroyTrigger. However, if you are using this with GetTriggeringTrigger() (or the trigger that started the thread), use the RemoveTriggerEnterRangeEventAlt function. This will destroy the trigger in a separate thread, so that you don't cause any problems.
How To: Use the System -
1. Get something that supports vJASS (Newgen {Recommended}, JASSHelper
2. Create a Trigger named "EnterRange"
3. Copy-paste "System Code" into the "EnterRange" Trigger
4. Read the rest of the "How To"s to learn how to use the system.
JASS:
This will create a trigger, and register the event for the unit, in one line.
How To: Get the Unit -
JASS:
private function Actions takes nothing returns nothing
local unit u = GetEnteringUnit()
local unit v = GetCenterUnit()
endfunction
u now contains the entering unit. v now contains the "unit which the entering unit came in range of."
How To: Destroy the Trigger-Event -
If you have a unit, and you want to get rid of the UnitInRange trigger-event for the unit, simply call RemoveUnitEnterRangeEvent with the unit.
If you somehow have the trigger, and want to destroy it, use RemoveTriggerEnterRangeEvent, rather than DestroyTrigger. However, if you are using this with GetTriggeringTrigger() (or the trigger that started the thread), use the RemoveTriggerEnterRangeEventAlt function. This will destroy the trigger in a separate thread, so that you don't cause any problems.
System Code:
JASS:
library EnterRange uses CSData,PUI,BoolexprUtils
//===============
//
// Enter Range v1.02
// Created by Darthfett
//
// Requires:
// CSData
// PUI
//
// Place in a trigger called "EnterRange"
//
// Provides the unit which the entering unit
// came in range of: "GetCenterUnit()"
//
//===============
private struct Data
//! runtextmacro PUI()
unit whichUnit
trigger t
static method create takes unit whichUnit returns Data
local Data d = Data[whichUnit]
if d != 0 then
call d.release()
endif
set d = Data.allocate()
set d.t = CreateTrigger()
set d.whichUnit = whichUnit
call SetCSData(d.t,d)
set Data[whichUnit] = d
return d
endmethod
method onDestroy takes nothing returns nothing
call SetCSData(.t,0)
call DestroyTrigger(.t)
endmethod
endstruct
//Use the function below to create a trigger with the event
function TriggerRegisterUnitEnterRangeEvent takes unit whichUnit, real range, boolexpr filter returns trigger
local Data d = Data.create(whichUnit)
if filter == null then
call TriggerRegisterUnitInRange(d.t,whichUnit,range,BOOLEXPR_TRUE)
else
call TriggerRegisterUnitInRange(d.t,whichUnit,range,filter)
endif
return d.t
endfunction
//Use the function below to get the unit which the entering unit came in range of
function GetCenterUnit takes nothing returns unit
return Data(GetCSData(GetTriggeringTrigger())).whichUnit
endfunction
//Use the function below to dispose of the trigger and event for the unit.
function RemoveUnitEnterRangeEvent takes unit whichUnit returns boolean
local Data d = Data[whichUnit]
if d == 0 then
return false
endif
call d.release()
return true
endfunction
//This will destroy the trigger and clean the struct, but it is NOT to be used with GetTriggeringTrigger()
function RemoveTriggerEnterRangeEvent takes trigger t returns boolean
local Data d = GetCSData(t)
if d == 0 then
return false
endif
call d.release()
return true
endfunction
private function Destroy takes nothing returns nothing
call Data(GetCSData(GetExpiredTimer())).release()
call DestroyTimer(GetExpiredTimer())
endfunction
//If you want to use GetTriggeringTrigger(), use this function instead of RemoveTriggerEnterRangeEvent
function RemoveTriggerEnterRangeEventAlt takes trigger trig returns nothing
local Data d = GetCSData(trig)
local timer t = CreateTimer()
call SetCSData(t,d)
call TimerStart(t,0,false,function Destroy)
set t = null
endfunction
endlibrary
Fireshock (Attached) is a spell that uses the system to strike entering units with lightning.