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

[JASS] Single Cooldown Reset

Status
Not open for further replies.
Level 5
Joined
Jul 14, 2008
Messages
121
This is my first jass trigger, I've been listening to Nestharus video tutorial and I'd like to have constructive criticism on it.

JASS:
library ResetSingleCooldown

/******************************************************************************
*   To reset a cooldown just use:
*       call UnitResetSingleCooldown(Unit Targeted, Ability ID)
*                                       unit        integer
*******************************************************************************/
    
    function UnitResetSingleCooldown takes unit u, integer a returns nothing
        local integer i = GetUnitAbilityLevel(u, a)
        if i > 0 then
            call UnitRemoveAbility(u, a)
            call UnitAddAbility(u, a)
            call SetUnitAbilityLevel(u, a, i)
        endif
    endfunction
    
endlibrary
 
Last edited:
Level 5
Joined
Jul 14, 2008
Messages
121
I'll have to agree with Maker on this one, it's true that in most cases it will be useless but not all cases.
 
Level 4
Joined
Jan 27, 2010
Messages
133
Just going to throw out some thoughts about naming.

Most functions that begin with a unit argument will have Unit in their name, so I would consider:

UnitResetSingleCooldown

The code will be a slight bit more self-documenting if you use something more explicit than 'integer a'. Like, for instance abilityID or abiID. ('unit u' feels like de-facto standard, so that's fine)

JASS:
function UnitResetSingleCooldown takes unit u, integer abilityID

Also, I know some people say you should always keep short variable names to improve speed. I've never encountered a situation where that would matter to a library function like this (and even if it would matter, you can just use the map optimizer, and get both nice code and a lot more speed than you would get from manually trying to shorten variable names...)
 
Level 5
Joined
Jul 14, 2008
Messages
121
I change the function name to UnitResetSingleCooldown since I though it made a lot of sense. But I kept "a" as ability id, I added a small description.

Thanks for the feedback.
 
Level 11
Joined
Mar 31, 2009
Messages
732
JASS:
library ResetSingleCooldown

/******************************************************************************
*   To reset a cooldown just use:
*       call UnitResetSingleCooldown(Unit Targeted, Ability ID)
*                                       unit        integer
*******************************************************************************/
   
    function UnitResetSingleCooldown takes unit u, integer a returns nothing
        local integer i = GetUnitAbilityLevel(u, a)

        if i == 0 then
            return
        endif

        call UnitRemoveAbility(u, a)
        call UnitAddAbility(u, a)
        call SetUnitAbilityLevel(u, a, i)
    endfunction
   
endlibrary

I don't like unneeded if blocks.
 
Level 5
Joined
Jul 14, 2008
Messages
121
Doesn't that do exactly the same thing? I honestly don't understand the difference, can you please explain further.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
a little bit (maybe faster, maybe not) different way of doing this is:
JASS:
library ResetSingleCooldown

/******************************************************************************
*   To reset a cooldown just use:
*       call UnitResetSingleCooldown(Unit Targeted, Ability ID)
*                                       unit        integer
*******************************************************************************/
   
    function UnitResetSingleCooldown takes unit u, integer a returns nothing
        local integer i = GetUnitAbilityLevel(u, a)
        if i > 0 and UnitRemoveAbility(u, a) then
            call UnitAddAbility(u, a)
            call SetUnitAbilityLevel(u, a, i)
        endif
    endfunction
   
endlibrary

I dont know if it is faster but if nothing its the same speed
Dont worry, the UnitRemoveAbility will not fire if i > 0 is not evaluated to true and even if it did, if you dont have the ability, this function returns false and nothing is removed anyway

but because it is so easy I think your way(or my but I would say its the same speed) is the fastest way to do it, unless you are some Jass wizard
 
Level 5
Joined
Jul 14, 2008
Messages
121
Aaah, ok must be quite hard to see the speed difference. Which ever is faster it's by so little I'm assuming since the code is so short.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
nah, I dont really think it makes any difference, he must call the function one way or the other, but there may be some hidden speed boost in if block just as there is in Condition block compared to Action block of trigger.
This way its a little bit shorter so it can be more readable but it also depends on how much stuff you have in the condition side of if (if i > 0 ...)
 
Level 11
Joined
Mar 31, 2009
Messages
732
Doesn't that do exactly the same thing? I honestly don't understand the difference, can you please explain further.

Its about code readability. Your way wraps the bulk of your code block around a condition. I prefer to perform the assertions and just return false if they fail, then perform the code block.

Take for example, would you prefer:

Code:
if (...) {
action
action
action
action
action
action
action
action
action
action
action
}

or

Code:
if (!...) return;
action
action
action
action
action
action
action
action
action
action
action

By wrapping a code block in an if statement, you've got to keep track of nesting it properly. Even with a 3 line code block, you're still leaving room to expand it later. Its just a style, and you were looking for opinions.
 
Level 5
Joined
Jul 14, 2008
Messages
121
Aaah ok I'm getting what you meant, I probably will keep it as is since I'm used to GUI and it's more similar that way :p. Thanks for the comments anyways :D.
 
Status
Not open for further replies.
Top