Darthfett
Aerospace/Cybersecurity Software Engineer
- Reaction score
- 615
This system will allow you to check whether a unit is in combat. A unit becomes in combat whenever it attacks, casts a spell, is attacked, or targetted by a spell, and will stay tagged as in combat for 4 seconds after attacks and spells stop.
Leak-less
Lag-less
MUI
MPI
Made in: vJASS
Usage:
Usage is pretty simple. If you want to use it in another JASS Code, use the functions SetUnitInCombat and GetUnitCombatState to get and set the combat state of the unit.
If you want to use it in GUI, you can use custom script to set a boolean variable to GetUnitCombatState(YOUR_UNIT). Then, that variable can be used to refer to the unit's combat status (True if it is in combat, False if it is not).
Requirements:
CSData v. 15.2
PUI v. 5.2
1 Trigger:
Trigger "InCombat"
--To put this in the map,
1. Create a new trigger.
2. Go to Edit -> Convert to Custom Text.
3. Rename it "InCombat"
4. Paste in the Code.
How it Works:
It's a very simple system. When a unit attacks, or casts a spell, it makes both the target as well as the triggering unit in combat. This is done by attaching a struct to the unit, and then running a timer which expires in 4 seconds. If the unit attacks again, that timer will start over again. If the timer expires, the attached struct is destroyed.
Lastly, if a user wants to know if a Unit is in combat or not, it simply returns whether any struct is attached to the unit.
System Code:
The example map that is attached allows the user to repick his hero through dialogs. However, you are not allowed to repick your hero, unless he/she is out of combat. Try it out!
Leak-less
Lag-less
MUI
MPI
Made in: vJASS
Usage:
Usage is pretty simple. If you want to use it in another JASS Code, use the functions SetUnitInCombat and GetUnitCombatState to get and set the combat state of the unit.
If you want to use it in GUI, you can use custom script to set a boolean variable to GetUnitCombatState(YOUR_UNIT). Then, that variable can be used to refer to the unit's combat status (True if it is in combat, False if it is not).
Requirements:
CSData v. 15.2
PUI v. 5.2
1 Trigger:
Trigger "InCombat"
--To put this in the map,
1. Create a new trigger.
2. Go to Edit -> Convert to Custom Text.
3. Rename it "InCombat"
4. Paste in the Code.
How it Works:
It's a very simple system. When a unit attacks, or casts a spell, it makes both the target as well as the triggering unit in combat. This is done by attaching a struct to the unit, and then running a timer which expires in 4 seconds. If the unit attacks again, that timer will start over again. If the timer expires, the attached struct is destroyed.
Lastly, if a user wants to know if a Unit is in combat or not, it simply returns whether any struct is attached to the unit.
System Code:
JASS:
library InCombat initializer Init requires PUI,CSData
globals
private constant real COMBAT_DURATION = 4
endglobals
struct CombatState
//! runtextmacro PUI()
unit u
timer t = CreateTimer()
method onDestroy takes nothing returns nothing
call SetCSData(this.t,0)
call PauseTimer(this.t)
call DestroyTimer(this.t)
set this.u = null
set this.t = null
endmethod
endstruct
private function EndCombat takes nothing returns nothing
local CombatState cs = GetCSData(GetExpiredTimer())
call cs.release()
endfunction
function GetUnitCombatState takes unit u returns boolean
local CombatState cs = CombatState<u>
return cs != 0
endfunction
function SetUnitInCombat takes unit u returns nothing
local CombatState cs = CombatState<u>
if cs == 0 then
set cs = CombatState.create()
set CombatState<u> = cs
endif
call SetCSData(cs.t,cs)
call TimerStart(cs.t,COMBAT_DURATION,false,function EndCombat)
endfunction
private function Conditions takes nothing returns boolean
//Change these to whatever you wish.
return IsUnitType(GetTriggerUnit(),UNIT_TYPE_HERO) == true or IsUnitType(GetAttacker(),UNIT_TYPE_HERO) == true or IsUnitType(GetSpellTargetUnit(),UNIT_TYPE_HERO) == true
endfunction
private function Actions takes nothing returns nothing
local unit tar = GetAttacker()
local unit temp = GetSpellTargetUnit()
call SetUnitInCombat(GetTriggerUnit())
if tar != null then
call SetUnitInCombat(tar)
elseif temp != null then
call SetUnitInCombat(temp)
endif
set tar = null
set temp = null
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_ATTACKED)
call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t,Condition(function Conditions))
call TriggerAddAction(t,function Actions)
endfunction
endlibrary</u></u></u>
The example map that is attached allows the user to repick his hero through dialogs. However, you are not allowed to repick your hero, unless he/she is out of combat. Try it out!