• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

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

Status
Not open for further replies.
Level 16
Joined
Oct 12, 2008
Messages
1,570
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:
 
Level 14
Joined
Nov 23, 2008
Messages
187
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
 
Level 8
Joined
Aug 6, 2008
Messages
451
'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.
Top