Globals

Back to lessons

Globals Block

The globals block is another major feature added in vjass. It gives us the ability to declare variables that will be visible in all functions of a scope, but nowhere else. To review, we now have three different types of variables we can declare in vjass:

- Local: Only exists in one function
- Global block: Exists in either one scope or throughout the map
- Variable Editor Variables: exists throughout the entire map. To use these in vjass, you must write "udg_" before the variable name.

Where only full global variables are available in GUI. The syntax for declaring a global block variable is simple:

scope GlobalBlockScopeDemo
    globals
        private real damage = 100
    endglobals
endscope


This is the first time we've seen the keyword "private" used. This restricts the variable visibility. This is more important in libraries than in scopes, but still should be placed here. There will be a full lesson on variable visilibty later. For best practice, all of these functions should also be private (but it's not a big concern now).

In this case, the real damage will be visible anywhere inside scope GlobalBlockScopeDemo. The purpose of this block is that there are sometimes values we want to preserve between times that the spell is cast. Let's say that we want a spell where it does 100 damage the first cast, 150 damage the second cast, 200 damage the third cast, etc. This would not be possible with locals, but with this global block level variable we can do this easily:

scope GlobalBlockScopeDemo

    globals
        private real damage = 100
    endglobals

    function onCast takes nothing returns nothing
        local unit source = GetTriggerUnit()
        local unit target = GetSpellTargetUnit()
        call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Human\\HolyBolt\\HolyBoltSpecialArt.mdl",GetUnitX(target),GetUnitY(target)))
        call UnitDamageTarget(source,target,damage,true,false,ATTACK_TYPE_CHAOS,DAMAGE_TYPE_ENHANCED,WEAPON_TYPE_WHOKNOWS)
        set damage = damage + 50
        set source = null
        set target = null
    endfunction

    function onCheck takes nothing returns boolean
        return(GetSpellAbilityId() == 'A000')
    endfunction

    function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(t,Condition(function onCheck))
        call TriggerAddAction(t,function onCast)
        set t = null
    endfunction

endscope


As you can see in the onCast function, the variable damage is increased by 50 after causing damage. This value is preserved after the function ends, and will carry over to the next cast.

To review:

- We now have three types of variables we can use in vjass, compared to GUI with only full global variables.
- Place a globals and endglobals block, and add variables to it to create global block variables.
- These variables are visible anywhere inside the scope
- They will not reset inbetween function calls

Back to lessons