Snippet TimedHandles

Reaction score
341
TimedHandles

Information:

TimedHandles is a script that will remove a handle after a certain amount of time. It can be used for any handle-type, and adding a new handle only takes one line of code. It can work with any patch as long as you have the correct TimerUtils version.

An example usage would look like this;

JASS:
call RemoveItemTimed(CreateItem('ratf', 0, 0), 5)


If you want to add a new handle-type, simply add this to the bottom of this system;

JASS:
//! runtextmacro TIMEDHANDLES("handle-type","TheFunctionThatDestroysTheHandle")


There are some requirements;


Code
JASS:
//***************************************************************************
//*
//*  TimedHandles - By TriggerHappy187
//*
//***************************************************************************
//*
//*  Installation
//*     Simply copy this script into your map, as well as TimerUtils.
//*
//***************************************************************************
//*
//*  Documentation
//*
//*  All this script does it associates a handle to a
//*  timer and then destroys the handle upon the timer's expiration.
//*
//***************************************************************************
//*
//*  The Public Functions
//*  
//*     $DESTROY$Timed - Starts the handle and it's timer, once it expires
//*                        the handle will be destroyed.
//*
//***************************************************************************
//*
//*  Examples
//*
//*  call DestroyEffectTimed(AddSpecialEffect("MODELNAME", X, X), 2)
//*  call DestroyEffectTimed(AddSpecialEffectTarget("MODELNAME", unit, "attachment-point"), 2)
//*
//***************************************************************************

library TimedHandles requires TimerUtils
        
    //! textmacro TIMEDHANDLES takes HANDLE,DESTROY
        
        struct $HANDLE$timed
        
            $HANDLE$ $HANDLE$_var
            
            private static method remove takes nothing returns nothing
                local timer t = GetExpiredTimer()
                local $HANDLE$timed d = GetTimerData(t)
                call $DESTROY$(d.$HANDLE$_var)
                call ReleaseTimer(t)
                call d.destroy()
            endmethod
            
            static method create takes $HANDLE$ h, real timeout returns $HANDLE$timed
                local $HANDLE$timed t = $HANDLE$timed.allocate()
                local timer ti = NewTimer()
                set t.$HANDLE$_var = h
                call SetTimerData(ti, t)
                call TimerStart(ti, timeout, false, function $HANDLE$timed.remove)
                return t
            endmethod
            
        endstruct
        
        function $DESTROY$Timed takes $HANDLE$ h, real duration returns nothing
            call $HANDLE$timed.create(h, duration)
        endfunction

    //! endtextmacro
    
    //! runtextmacro TIMEDHANDLES("effect","DestroyEffect")
    //! runtextmacro TIMEDHANDLES("lightning","DestroyLightning")
    //! runtextmacro TIMEDHANDLES("weathereffect","RemoveWeatherEffect")
    //! runtextmacro TIMEDHANDLES("item","RemoveItem")
    //! runtextmacro TIMEDHANDLES("ubersplat","DestroyUbersplat")

endlibrary
 

Romek

Super Moderator
Reaction score
963
What does it do?
How do you use it?

More information about it please.

Is there any reason as to why StopTimedBlah isn't private and inlined into DestroyTimedBlah?

The user has no way of actually using a BlahStruct because the struct itself is private, and there are no non-private functions that return a BlahStruct.
 
Reaction score
341
>>More information about it please.

Documentation added :p

>>Is there any reason as to why StopTimedBlah isn't private and inlined into DestroyTimedBlah?

If for some reason the user wants to stop the timed effect, he can.

>>The user has no way of actually using a BlahStruct because the struct itself is private, and there are no non-private functions that return a BlahStruct.

They don't need to use the struct, everything that is needed is in the functions.
 

Romek

Super Moderator
Reaction score
963
> If for some reason the user wants to stop the timed effect, he can.
Tell me how a user is meant to do that then?
With an example, preferably.

> They don't need to use the struct, everything that is needed is in the functions.
"function StopTimed$NAME$ takes $HANDLE$struct s returns nothing"
Looks like they need the struct for something.
 
Reaction score
341
>>Tell me how a user is meant to do that then?
With an example, preferably


JASS:
function test takes nothing returns nothing
    local effect e = AddSpecialEffect("", 0, 0)
    call StartTimedEffect(u)
    // if for some reason he wants to stop it before 4 seconds..
    call StopTimedEffect(1)
endfunction


>>Looks like they need the struct for something.

I guess I can make a Get$HANDLE$Index function.
 

Romek

Super Moderator
Reaction score
963
I'm sure you know that "call StopTimedEffect(1)" wouldn't be usable at all in actual maps.

And I hope you understand my point now.
 
Reaction score
341
Should I make a struct search for the handles index?

It would require a new global per handletype, and an O(n) search.

EDIT: That function has been removed, there was no need for it. Also the script has been optimized alot.
 

Viikuna

No Marlo no game.
Reaction score
265
//! runtextmacro handles("effect","DestroyEffect")
//! runtextmacro handles("lightning","DestroyLightning")
//! runtextmacro handles("weathereffect","RemoveWeatherEffect")
//! runtextmacro handles("item","RemoveItem")
//! runtextmacro handles("ubersplat","DestroyUbersplat")

Well, it is usefull for effects.

For lightnings we use TimedLightning styled lightning structs, which have not only lifespan but other cool methods too.

No one sane creates&destroys weather effects.. You just create one each type, and then enable&disable them when needed.

Someone might find this usefull for items too..

It could be usefull for ubersplats maybe, dont really know. I just rather use images and some timer to fade them in and out.
edit. I havent actually used ubersplats too much. What FinishUbersplat does? Does it fade them overtime or something?

Well, maybe this has some uses..
 

Romek

Super Moderator
Reaction score
963
"handles" is a rubbish textmacro name. Make it more "private" to the system.
 

Azlier

Old World Ghost
Reaction score
461
You don't need to null times that come from TimerUtils.
 

Jesus4Lyf

Good Idea™
Reaction score
397
It's the same as the other and my sentiments apply.
What the hell? Why would you use H2I for a delayed remove? :(

Any map using this will break on patch. Finding an alternative to TimerUtils would be a bonus that other systems may not have, but I haven't looked at other systems...
Expand your target audience past those who accept H2I. We're living in patch 1.23, with a patch that will break all H2I 1.23 releases looming just over the horizon... :)
 
Reaction score
341
It's the same as the other and my sentiments apply.

Expand your target audience past those who accept H2I. We're living in patch 1.23, with a patch that will break all H2I 1.23 releases looming just over the horizon... :)

Updated to patch 1.24 :thup:
 
Reaction score
341
That doesn't help. That's even worse. :(

So you tell me to remove H2I, I remove it and now it's even worse?

Care to explain...?

EDIT: You updated your post.

Not to mention the efficiency loss from your previous use of TU and not solving any downsides. >_<

I don't see how it's less efficient, but what your saying is I should update it with a timer attachment system that doesn't use H2I?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top