AbilityPreload

Scripts

14 years
edited 8 years
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.

Code (jass) Select
1
2
library AbilityPreload
3
//===========================================================================
4
// Information:
5
//==============
6
//
7
//      Preloading removes the noticeable delay the first time an ability
8
//  is loaded in a game. If an ability was not already on a pre-placed unit
9
//  or a unit that was created during initialization, preloading is needed
10
//  to prevent a delay.
11
//
12
//===========================================================================
13
// AbilityPreload API:
14
//=====================
15
//
16
//  AbilityPreload(abilityid) :
17
//        Call this before any time has elapsed to preload a specific
18
//     ability. If debug mode is enabled, you will see an error message
19
//     if you call this after initialization, or if you try to preload
20
//     an ability that does not exist. Will inline to a UnitAddAbility
21
//     call if debug mode is disabled.
22
//
23
//  AbilityRangePreload(start, end) :
24
//        Same as AbilityPreload, but preloads a range of abilities.
25
//      It will iterates between the two rawcode values and preload
26
//      every ability along the way. It will not show an error message
27
//      for non-existent abilities.
28
// 
29
//===========================================================================
30
// Configuration:
31
//================
32
33
globals
34
    private constant integer PreloadUnitRawcode = 'zsmc'
35
    //This is the rawcode for "Sammy!". It is never used and has no model,
36
    //which makes an ideal preloading unit. Change it if you want to.
37
endglobals
38
39
//===========================================================================
40
41
globals
42
    private unit PreloadUnit
43
endglobals
44
45
function AbilityPreload takes integer abilityid returns nothing
46
    static if DEBUG_MODE then
47
        if GetUnitTypeId(PreloadUnit) == 0 then
48
            call BJDebugMsg("AbilityPreload error: Can't preload an ability after initialization")
49
            return
50
        endif
51
    endif
52
    call UnitAddAbility(PreloadUnit, abilityid)
53
    static if DEBUG_MODE then
54
        if GetUnitAbilityLevel(PreloadUnit, abilityid) == 0 then
55
            call BJDebugMsg("AbilityPreload error: Attempted to preload a non-existent ability")
56
        endif
57
    endif
58
endfunction
59
60
function AbilityRangePreload takes integer start, integer end returns nothing
61
    local integer i = 1
62
        static if DEBUG_MODE then
63
            if GetUnitTypeId(PreloadUnit) == 0 then
64
                call BJDebugMsg("AbilityPreload error: Can't preload an ability after initialization")
65
                return
66
            endif
67
        endif
68
        if start > end then
69
            set i = -1
70
        endif
71
        loop
72
            exitwhen start > end
73
            call UnitAddAbility(PreloadUnit, start)
74
            set start = start + i
75
        endloop
76
endfunction
77
78
//===========================================================================
79
80
private struct Init extends array
81
    private static method onInit takes nothing returns nothing
82
        set PreloadUnit = CreateUnit(Player(15), PreloadUnitRawcode, 0., 0., 0.)
83
        call UnitApplyTimedLife(PreloadUnit, 0, .001)
84
        call ShowUnit(PreloadUnit, false)
85
        call UnitAddAbility(PreloadUnit, 'Aloc')
86
    endmethod
87
endstruct
88
89
endlibrary
90
91