ItemRemovalEvent

Reaction score
341
ItemRemovalEvent
TriggerHappy187

Information

This is a simple script that will detect when an item has left the map.

An example usage;

JASS:
scope ItemLeavesTest initializer InitTrig

    private function Actions takes nothing returns nothing
        call BJDebugMsg("Item has been removed")
    endfunction

    //===========================================================================
    private function InitTrig takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterItemRemoveEvent(t)
        call TriggerAddAction(t, function Actions)
    endfunction

endscope

The Script
JASS:
//***************************************************************************
//*
//*  ItemRemovalEvent - By TriggerHappy187
//*
//***************************************************************************
//*
//*  Installation
//*    * All you need to do is copy this script into your map and you can use
//*     the provided functions.
//*
//***************************************************************************
//*
//*  Requirements
//*    * This requires no external systems, all it requires is JassHelper.
//*
//***************************************************************************
//*
//*   Usage
//*    * call TriggerRegisterItemRemoveEvent(someTrigger)
//*
//***************************************************************************

library ItemRemovalEvent initializer InitTrig

    globals
        private real UPDATE_INTERVAL = 0.07
    endglobals
    
    globals
        private integer COUNT = 0
        private integer C = 0
        private integer C_COMPARE = 0
        private timer TIMER = CreateTimer()
        private boolean INITIALIZED = false
        private trigger array T
    endglobals
    
    private function RunTriggers takes nothing returns nothing
        local integer i = 0
        loop
            exitwhen i > COUNT
            if TriggerEvaluate(T<i>) and IsTriggerEnabled(T<i>) then
                call TriggerExecute(T<i>)
            endif
            set i = i + 1
        endloop
    endfunction
    
    private function True takes nothing returns boolean
        return true
    endfunction
    
    private function Callback takes nothing returns nothing
        if not INITIALIZED then
            set C = C + 1
        else
            if GetWidgetLife(GetEnumItem()) &gt; 0.405 then
                set C_COMPARE = C_COMPARE + 1
            endif
        endif
    endfunction
    
    private function CountItems takes nothing returns nothing
        set C_COMPARE = 0
        call EnumItemsInRect(bj_mapInitialPlayableArea, Filter(function True), function Callback)
        if C_COMPARE != C and INITIALIZED then
            call RunTriggers()
            set C = C_COMPARE
        endif
        if not INITIALIZED then
            set INITIALIZED = true
        endif
    endfunction
    
    function TriggerRegisterItemRemoveEvent takes trigger t returns nothing
        set T[COUNT] = t
        set COUNT = COUNT + 1
    endfunction
    
    //! textmacro ItemRemoveEvent takes NAME,OPERATOR
    private function $NAME$ takes nothing returns nothing
        set C = C $OPERATOR$ 1
        set C_COMPARE = C_COMPARE $OPERATOR$ 1
    endfunction
    //! endtextmacro
    //! runtextmacro ItemRemoveEvent(&quot;Add&quot;, &quot;+&quot;)
    //! runtextmacro ItemRemoveEvent(&quot;Subtract&quot;, &quot;-&quot;)
    
    private function InitTrig takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TimerStart(TIMER, UPDATE_INTERVAL, true, function CountItems)
        call TimerStart(CreateTimer(), 0.01, false, function CountItems)
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DROP_ITEM )
        call TriggerAddAction(t, function Add)
        set t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM )
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SELL_ITEM )
        call TriggerAddAction(t, function Subtract)
    endfunction
    
endlibrary</i></i></i>
 
Reaction score
341
Ah, didn't know that native exists.

Graveyard please :p

I was looking for TriggerRegisterItemDeathEvent.

This could be ultra-simplified if you just checked if the widget was an item, but then this wouldn't really be a system.
 

Jesus4Lyf

Good Idea™
Reaction score
397
I was gonna grill this, but since you've already called for graveyard, I won't bother.

But I will encourage you to use Event. This still didn't correctly handle event structure because triggers weren't removed from the list and such. Also, I'm not convinced your trigger enabled check was correct, but I can't say so for sure because I didn't test it.

I wrote Event for systems like this so they could be correct, and twice you've done it poorly. At very least, copy the Event struct into the system, privatise it and use it there... If you (just you) find yourself unwilling to credit it, you don't have to - I say this just because I want to see the quality of your code increase.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top