• 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!

[JASS] Will this work? Is this usefull? Is this Effective?

Status
Not open for further replies.
Level 16
Joined
Oct 12, 2008
Messages
1,569
Hello people,,

I have seen people whining about: 'Damage detection, but you cant remove any events so it might bug after some time'
So i thought, why not make a trigger for every unit then? And destroy that trigger when the unit leaves?

This is the code i made, i would like you guys to look at it and tell me all stupid mistakes i made and if it would be usefull, and if it would even work!
JASS:
library DamageDetection Initializer Init
   struct Data
      trigger t
      static method onDestroy takes nothing returns nothing
         set .t = null
      static method Enter takes nothing returns nothing
         local Data dat = Data.allocate()
         local unit u = GetTriggerUnit()
         set dat.t = CreateTrigger()
         call SetUnitUserData(u, dat)
         call TriggerRegisterUnitEvent(dat.t, u, EVENT_UNIT_DAMAGED)
         call TriggerAddAction(dat.t , Damage())
         set u = null
      endmethod
      static method Leave takes nothing returns nothing
         local unit u = GetTriggerUnit()
         local Data dat = GetUnitUserData(u)
         call DestroyTrigger(dat.t)
         call dat.destroy()
         set u = null
      endmethod
      private function Damage takes nothing returns nothing
         local unit u = GetTriggerUnit()
         local player p = GetOwningPlayer(u)
         DisplayTextToPlayer(p, GetUnitName(u) + " has taken Damage")
         set u = null
         set p = null
      private function Init takes nothing returns nothing
         local trigger t1 = CreateTrigger()
         local trigger t2 = CreateTrigger()
         local region rr = CreateRegion()
         local integer index = 0
         call RegionAddRect(rr, bj_mapInitialPlayableArea)
         loop
            call TriggerRegisterEnterRegion(t1, rr, null)
            call TriggerRegisterLeaveRegion(t2, rr, null)
            set index = index + 1
            exitwhen index == bj_MAX_PLAYER_SLOTS
         endloop
         call TriggerAddAction(t1, function Data.Enter)
         call TriggerAddAction(t2, function Data.Leave)
         set t1 = null
         set t2 = null
         set rr = null
      endfunction
   endstruct
endlibrary

Thanks! :grin:
 
Blah... Install JNGP or JassHelper at least... There's a vast plane of obvious errors.

As far as I know, "damage taken" unit events are removed on unit removal (basically after finishing decaying process, if unit has decay setting, or after dying, if not).

About system work: I'd recommend to destroy triggers in another thread (via running another function with ExecuteFunc), and to use conditions instead of actions, because actions cause leak and slower than conditions.

JASS:
globals
  trigger temp_trig = null
endglobals

function RemoveMyTrigger takes nothing returns nothing
  call DestroyTrigger(temp_trig)
  set temp_trig = null // just in case
endfunction

// . . .

static method onDestroy takes nothing returns nothing
  set temp_trig = .t
  call ExecuteFunc("RemoveMyTrigger")
  set .t = null
endmethod
 
'Damage detection, but you cant remove any events so it might bug after some time'

This makes no sense. Not being able to remove damage events is not a problem and it doesnt cause any bugs unless you do something really stupid.

It just leaks some events, which is not usually a problem at all.

( I personaly prefer to recycle my units to prevent these event leaks )

edit. You should also use one trigger for all units instead of multiple triggers.
Also using conditions instead of actions might be a good idea, since they are faster.
 
Status
Not open for further replies.
Back
Top