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

[vJASS] Making TimerUtils optional?

Status
Not open for further replies.
Level 19
Joined
Mar 18, 2012
Messages
1,716
For TimerUtils you do: static if LIBRARY_TimerUtils then.

Edit:
static ifs are like normal ifs, except that the condition must contain only constant booleans, the and operator and the not operator.

Note the following doesn't work for TimerUtils, because it doesn't use structs at all.

In general you can also do: static if struct.method.exists then for instance static if DummyCaster.castTarget.exists then

or when implementing modules (here Alloc) you can do stuff like static if thistype.allocate.exists then

Lately I've been using this for my uploaded spells:
JASS:
        private static method onInit takes nothing returns nothing
            static if LIBRARY_SpellEffectEvent then
                call RegisterSpellEffectEvent(AID, function thistype.run)
            else
                local trigger t = CreateTrigger()
                call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
                call TriggerAddCondition(t, Condition(function thistype.check))
                set t = null
            endif
            
            static if not LIBRARY_DummyCaster then
                static if LIBRARY_UnitIndexer then
                    set UnitIndexer.enabled = false
                    set c = CreateUnit(NEUTRAL,DUMMY_CASTER,0,0,0)
                    set UnitIndexer.enabled = true
                else
                    set c = CreateUnit(NEUTRAL,DUMMY_CASTER,0,0,0)
                endif
                call SetUnitPosition(c,2147483647,2147483647)
                call UnitAddAbility(c, BUFF_CAST_ID)
            endif
        endmethod

or here is a small piece out of my Blizzard Alternative

JASS:
    private struct Needle extends array
        implement optional Alloc
        /*
        *   x and y are requires to determine the impact location.
        *   t keeps track of the falling time.
        */
        real x
        real y
        real t
       
        static if (not thistype.allocate.exists) then
            static integer array rn
            static integer ic = 0
        endif
       
        static method create takes real x, real y, real field, real angle returns thistype
            static if (thistype.allocate.exists) then
                local thistype this = allocate()
            else
                local thistype this = rn[0]
                if this == 0 then
                    set ic = ic + 1
                    set this = ic
                else
                    set rn[0] = rn[this]
                endif
            endif

You're welcome :), btw the allocation in Alloc is done different then mine. It only uses one integer array and nothing else.
 
Last edited:
Level 11
Joined
Oct 11, 2012
Messages
711
For TimerUtils you do: static if LIBRARY_TimerUtils then.

Edit:

Note the following doesn't work for TimerUtils, because it doesn't use structs at all.

In general you can also do: static if struct.method.exists then for instance static if DummyCaster.castTarget.exists then

or when implementing modules (here Alloc) you can do stuff like static if thistype.allocate.exists then

Lately I've been using this for my uploaded spells:
JASS:
        private static method onInit takes nothing returns nothing
            static if LIBRARY_SpellEffectEvent then
                call RegisterSpellEffectEvent(AID, function thistype.run)
            else
                local trigger t = CreateTrigger()
                call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
                call TriggerAddCondition(t, Condition(function thistype.check))
                set t = null
            endif
            
            static if not LIBRARY_DummyCaster then
                static if LIBRARY_UnitIndexer then
                    set UnitIndexer.enabled = false
                    set c = CreateUnit(NEUTRAL,DUMMY_CASTER,0,0,0)
                    set UnitIndexer.enabled = true
                else
                    set c = CreateUnit(NEUTRAL,DUMMY_CASTER,0,0,0)
                endif
                call SetUnitPosition(c,2147483647,2147483647)
                call UnitAddAbility(c, BUFF_CAST_ID)
            endif
        endmethod

or here is a small piece out of my Blizzard Alternative

JASS:
    private struct Needle extends array
        implement optional Alloc
        /*
        *   x and y are requires to determine the impact location.
        *   t keeps track of the falling time.
        */
        real x
        real y
        real t
       
        static if (not thistype.allocate.exists) then
            static integer array rn
            static integer ic = 0
        endif
       
        static method create takes real x, real y, real field, real angle returns thistype
            static if (thistype.allocate.exists) then
                local thistype this = allocate()
            else
                local thistype this = rn[0]
                if this == 0 then
                    set ic = ic + 1
                    set this = ic
                else
                    set rn[0] = rn[this]
                endif
            endif

You're welcome :), btw the allocation in Alloc is done different then mine. It only uses one integer array and nothing else.

Thanks a lot, BPower!! :) Nice example!
+Rep if I can!
 
Status
Not open for further replies.
Top