Wc3C.net

Wc3C.net (http://www.wc3c.net/forums.php)
-   Scripts (http://www.wc3c.net/forumdisplay.php?f=737)
-   -   AbilityPreload (http://www.wc3c.net/showthread.php?t=105279)

grim001 04-01-2009 08:27 PM

AbilityPreload
 
Requires vJass

This library gives you one new function: call AbilityPreload(abilityid). It will remove the delay the first time an ability is loaded into the game. Vexorian's xe system includes a preload module, but it has not been available as a script until now.

Collapse JASS:
library AbilityPreload
//===========================================================================
// Information:
//==============
//
//      Preloading removes the noticeable delay the first time an ability
//  is loaded in a game. If an ability was not already on a pre-placed unit
//  or a unit that was created during initialization, preloading is needed
//  to prevent a delay.
//
//===========================================================================
// AbilityPreload API:
//=====================
//
//  AbilityPreload(abilityid) :
//        Call this before any time has elapsed to preload a specific
//     ability. If debug mode is enabled, you will see an error message
//     if you call this after initialization, or if you try to preload
//     an ability that does not exist. Will inline to a UnitAddAbility
//     call if debug mode is disabled.
//
//  AbilityRangePreload(start, end) :
//        Same as AbilityPreload, but preloads a range of abilities.
//      It will iterates between the two rawcode values and preload
//      every ability along the way. It will not show an error message
//      for non-existent abilities.
// 
//===========================================================================
// Configuration:
//================

globals
    private constant integer PreloadUnitRawcode = 'zsmc'
    //This is the rawcode for "Sammy!". It is never used and has no model,
    //which makes an ideal preloading unit. Change it if you want to.
endglobals

//===========================================================================

globals
    private unit PreloadUnit
endglobals

function AbilityPreload takes integer abilityid returns nothing
    static if DEBUG_MODE then
        if GetUnitTypeId(PreloadUnit) == 0 then
            call BJDebugMsg("AbilityPreload error: Can't preload an ability after initialization")
            return
        endif
    endif
    call UnitAddAbility(PreloadUnit, abilityid)
    static if DEBUG_MODE then
        if GetUnitAbilityLevel(PreloadUnit, abilityid) == 0 then
            call BJDebugMsg("AbilityPreload error: Attempted to preload a non-existent ability")
        endif
    endif
endfunction

function AbilityRangePreload takes integer start, integer end returns nothing
    local integer i = 1
        static if DEBUG_MODE then
            if GetUnitTypeId(PreloadUnit) == 0 then
                call BJDebugMsg("AbilityPreload error: Can't preload an ability after initialization")
                return
            endif
        endif
        if start > end then
            set i = -1
        endif
        loop
            exitwhen start > end
            call UnitAddAbility(PreloadUnit, start)
            set start = start + i
        endloop
endfunction

//===========================================================================

private struct Init extends array
    private static method onInit takes nothing returns nothing
        set PreloadUnit = CreateUnit(Player(15), PreloadUnitRawcode, 0., 0., 0.)
        call UnitApplyTimedLife(PreloadUnit, 0, .001)
        call ShowUnit(PreloadUnit, false)
        call UnitAddAbility(PreloadUnit, 'Aloc')
    endmethod
endstruct

endlibrary


emjlr3 04-01-2009 08:57 PM

cant you just give the unit a timedlife?

grim001 04-01-2009 09:08 PM

0 duration timedlife doesn't work. it would also leave behind a corpse, leak a unit reference, set off death detection triggers, etc.

Troll-Brain 04-01-2009 09:29 PM

An hero ability doesn't need to be preloaded ?

TheKid 04-01-2009 10:05 PM

It wouldn't leak a unit reference, you would just set the global unit to null when you apply the timed life.

grim001 04-01-2009 10:09 PM

You have to keep that global reference until after init, otherwise the function wouldn't work.

Rising_Dusk 04-01-2009 10:31 PM

You should make it return a boolean and have that boolean be false if called after map initialization. It is always good to protect users from themselves.

akolyt0r 04-01-2009 10:46 PM

Quote:

Originally Posted by Rising_Dusk
You should make it return a boolean and have that boolean be false if called after map initialization. It is always good to protect users from themselves.


lol ..
why not a simple debug message ?
yes ..those users which are to dumb to use this in init-triggers will surely do something like:
Collapse JASS:
if not preloadblabla(bla) then
debug BJDebugMsg("im dumb")
endif
to notice that they are dumb.

have i missunderstood your thought ?

grim001 04-01-2009 10:57 PM

I made it print an error message if you call it either before the library is initialized or after the unit has been removed.

Rising_Dusk 04-01-2009 11:57 PM

Hrm, it is true that the returned boolean would be pretty useless, so I agree that an error message is the better solution. You should prefix the error message with the debug keyword so that users can choose if they want it to appear or not. Once that is done, this is more than ready to be approved.

Anitarf 04-02-2009 03:08 AM

Wait, can you call it before the init function runs? Without function interfaces at least? Any library that requires this one would get initialised after it.

JauntyJackalope 04-02-2009 03:47 AM

I don't get why you set a TimedLife AND use a timer. The name will conflict with CS.

Vexorian 04-02-2009 05:49 AM

I don't get why you set a TimedLife AND use a timer. The name will conflict with CS.

grim001 04-02-2009 06:22 AM

Quote:

Originally Posted by Rising_Dusk
You should prefix the error message with the debug keyword so that users can choose if they want it to appear or not.


I don't think that all error messages should necessarily be debug. My rule is: if there's any valid reason they could encounter the error message when using the library correctly, it should be debug so it doesn't disrupt gameplay. In this case, getting an error message means you really screwed it up, so debug would just be a way to help the user not notice that. Let me know if you disagree.

Quote:

Originally Posted by Anitarf
Wait, can you call it before the init function runs? Without function interfaces at least? Any library that requires this one would get initialised after it.


You can do it by calling the function from a struct's onInit method. Vex might add other ways to do things "before init" later on.

Quote:

Originally Posted by Vexorian
I don't get why you set a TimedLife AND use a timer. The name will conflict with CS.


Typo from when I was checking emjlr3's suggestion, fixed it. About the name conflict, I am not sure what to do.

Pros of leaving it alone:
1.) PreloadAbility is a pretty specific function name, changing it would be lame.
2.) The user won't need this library if they have CS in their map.
3.) CS does not seem to be be used often anymore.

Cons:
1.) If they need both CS and a library that requires this, they would have to delete this library and make things not require it.

I am leaning toward leaving it alone.

Troll-Brain 04-02-2009 09:09 AM

An hero abiliy doesnt need to be preloaded ?


All times are GMT. The time now is 03:08 PM.

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services