Snippet RemoveUnitEx

Reaction score
341
RemoveUnitEx
by TriggerHappy187​


Introduction:

The script idea originally by Ammorth, though it did not work on units that have decayed and been removed.
This event registers when a unit is removed by the included RemoveUnitEx function. My script (hopefully) functions whether or not the unit has decayed.
I have tested it, though I could have been wrong, I set all the decay times in the gameplay constants to 0.50 then TSA'd 20 seconds and called RemoveUnitEx, and it still worked. Any other testing would be great

An example use of this would look like.

JASS:

function test takes nothing returns nothing
    call BJDebugMsg("Unit Removed")
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterRemoveUnitEvent(t, gg_unit_hfoo_0001)
    call TriggerAddAction(t, function test)
endfunction


The script

JASS:
library RemoveUnitEx

globals
    private integer count = 0
    private unit lastRemovedUnit = null
endglobals

private struct index

    trigger t
    unit u

    static method create takes trigger t, unit u returns index
        local index i = index.allocate()
        set i.u = u
        set i.t = t
        return i
    endmethod
    
endstruct

function TriggerRegisterRemoveUnitEvent takes trigger whichTrigger, unit whichUnit returns nothing 
    local index i = index.create(whichTrigger, whichUnit)
    set count = count + 1
endfunction

function RemoveUnitEx takes unit u returns nothing
    local integer i = count
    local index d = i
    loop
        exitwhen i <= 0
        if u == d.u then
            call TriggerExecute(d.t)
            call d.destroy()
        endif
        set i = i - 1
        set d = i
    endloop
    set lastRemovedUnit = u
    call RemoveUnit(u)
endfunction

function GetLastRemovedUnit takes nothing returns unit
    return lastRemovedUnit
endfunction

endlibrary
 

Azlier

Old World Ghost
Reaction score
461
Why not make lastRemovedUnit non-private? Using GetLastRemovedUnit() just to get at a variable is just like using GetLastCreatedUnit() when we can use bj_lastCreatedUnit.
 
Reaction score
341
JASS:
function GetLastRemovedUnit takes nothing returns unit
    return lastRemovedUnit
endfunction


Is inlined when jasshelper compiles.

I'd rather people not mess the global.
 

Darius34

New Member
Reaction score
30
The script idea originally by Ammorth, though it did not work on units that have decayed and been removed.
I think you've misunderstood - this doesn't fix the problem with Ammorth's original incarnation of the script. I can't see how units automatically removed by the game upon decaying would fire events with the code above.

Surely you tested Ammorth's version?

Side note: why would you want to remove a decayed, and hence already removed, unit? (That's a double-free, if I'm not mistaken - something you'd actually want to avoid, not encourage...)
 

Flare

Stops copies me!
Reaction score
662
You're never decreasing count when a struct instance is being destroyed, so your loop would constantly be getting larger
JASS:
        if u == d.u then
            call TriggerExecute(d.t)
            call d.destroy()
            set count = count - 1 //here <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />
        endif


I'd rather people not mess the global.
Well, it's not your responsibility to care for utter stupidity

Also, the fact that the event only works when you call RemoveUnitEx is a bit meh. If it'd work when a unit has fully decayed naturally, it might be much more useful
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> This event registers when a unit is removed by the included RemoveUnitEx function

So, this calls a function when I called RemoveUnitEx, and sets a global to the unit I passed?
Uhm... I clearly missed something here...

What is this doing?
And why would I need this?
 

Tom_Kazansky

--- wraith it ! ---
Reaction score
157
At first glance, I think this would be useful for me because all units in my map have a struct attached to them. If I want to destroy the struct, I can only use the "unit dies" event, wait some times for decaying and destroy the struct, but I still worry about the "does not decay" units.
Well, but:
it did not work on units that have decayed
:(
-----
"animated dead" units also considered as "removed" when they dies right ? also units who have their corpse used for "raise dead" are considered as "removed", right ?
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!

      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