HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

private global question

12-27-2007, 10:58 AM#1
StRoNgFoE_2000
I've been learning the usage of structs and scopes, and I'm a little confused on something. When declaring a "private global" within a scope, does this mean that it will not overwrite the data if the trigger is executed more than once? I cannot explain without an example. I do know that it allows you to use the same variable name in other scopes.

Collapse JASS:
scope somescope
    globals
        private group somegroup  // Fear that this can be overwritten if executed twice....
    endglobals

    struct somestruct
        timer t = CreateTimer()
        rect  r = SomeRect
         //...some other contents...
    endstruct

    function end takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local somestruct data  = somestruct(GetHandleInt(t,"struct"))
        set unit = FirstOfGroup(somegroup) // An example of using the group after 20 seconds..
    endfunction

    function start takes nothing returns nothing
        local somestruct data = somestruct.create()
        call GroupEnumUnitsInRect(somegroup , data.r, null)
        call SetHandleInt(data.t, "struct", data)
        call TimerStart(data.t, 20, true, function end)
    endfunction
endscope

If this trigger is called once, it will set units to group somegroup. Now, in 20 seconds somegroup is accessed at the timer end. If this trigger is called again, will it overwrite somegroup, or no because it is private?
12-27-2007, 11:42 AM#2
Pyrogasm
It's just like any global, so yes it would be overwritten.

Private means that it just modifies where you can access the global from:
Collapse JASS:
scope SomeScope
    globals
        private group SomeGroup = CreateGroup()
        real X = 275.00
        public real Y = 300.00
        public real R = 55.00
    endglobals

    function SomeFunc takes nothing returns nothing
        call GroupEnumUnitsInRange(SomeGroup, X, Y, R, BOOLEXPR_TRUE) //Ok because we are referencing SomeGroup inside the scope
    endfunction

    function SomeFunc2 takes nothing returns nothing
        local group SomeGroup = CreateGroup() //Bad because we're re-declaring SomeGroup
        call GroupEnumUnitsInRange(SomeGroup, X, Y, R, BOOLEXPR_TRUE) //Ok because we are referencing SomeGroup inside the scope
    endfunction
endscope

function SomeFunc3 takes nothing returns nothing
    call GroupEnumUnitsInRange(SomeGroup, X, Y, R, BOOLEXPR_TRUE) //Bad because we're trying to reference SomeGroup when we're not in that scope!
endfunction

function SomeFunc4 takes nothing returns nothing
    local group SomeGroup = CreateGroup() //Ok because we're not in the scope and there is no variable conflict
    call GroupEnumUnitsInRange(SomeGroup, X, Y, R, BOOLEXPR_TRUE) //Ok because SomeGroup exists as a local group in this function
endfunction
12-27-2007, 09:18 PM#3
moyack
You use the private keyword if you want that only the functions inside the scope / library can access and / or modify the variable.

Example:

Collapse JASS:
scope A

globals
    private integer SpellID = 'A000'
endglobals

private function test takes nothing returns nothing
    call DisplayTimedTextFromPlayer(Player(0), 0,0,5, I2S(SpellID)) // it doesn't generate error because this function is part of the scope
endfunction

endscope

function test takes nothing returns nothing
    call DisplayTimedTextFromPlayer(Player(0), 0,0,5, I2S(SpellID)) // it generates error.
endfunction

With this you can ensure that the variables, structs and functions in a scope can only be used inside the scope.

To resume:

private integer X : X only will be accessible inside the scope / library A
public integer X : X can be called outside in the way A_X, and inside the scope in the normal way.
12-28-2007, 04:25 AM#4
The_AwaKening
I just came back to mapping after about a 7 month break. Where did this new "scope" come from. Is it built into the warcraft engine, or is it a 3rd party add-on. It looks like a "class" in visual basic.
12-28-2007, 07:54 AM#5
Pyrogasm
It is kind-of like a class. It's part of the new OOP-like syntax that Vexorian has created with the JASS NewGen Pack
12-28-2007, 10:52 AM#6
StRoNgFoE_2000
Quote:
Originally Posted by The_AwaKening
I just came back to mapping after about a 7 month break. Where did this new "scope" come from. Is it built into the warcraft engine, or is it a 3rd party add-on. It looks like a "class" in visual basic.

I'm just getting used to this new concept as well but from what I've learned so far it makes your spells really fast, and is an almost alternative to handle vars. This is a mass shrink i did, just a mass bloodlust. It can shrink many units at a time without the least bit of lag, unlike when i used handle vars to do it. Im open to any criticism on this.

Hidden information:

Collapse JASS:
struct shrink
    real    x
    real    y
    unit    v 
    unit    w
    player  p
    timer   t  = CreateTimer()
    rect    r  = bj_mapInitialPlayableArea
    group   g  = CreateGroup()
    method onDestroy takes nothing returns nothing
        call PauseTimer(.t)
        call FlushHandleLocals(.t)
        call DestroyTimer(.t)
        call DestroyGroup(.g)
    endmethod
    static method create takes unit u, player p returns shrink
        local shrink data = shrink.allocate()
        set data.p = p
        set data.x = GetUnitX(u)
        set data.y = GetUnitY(u)
        return data
    endmethod    
endstruct
function ShrinkCond takes nothing returns boolean
    return GetSpellAbilityId()=='A05V'
endfunction
function TheShrink takes nothing returns nothing
    local timer t        = GetExpiredTimer()
    local shrink data    = shrink(GetHandleInt(t,"struct"))
    if CountUnitsInGroup(data.g) > 0 then
        set data.w = FirstOfGroup(data.g)
        set data.v = CreateUnit(data.p, 'h062', data.x, data.y, 270)
        call UnitAddAbility(data.v, 'A05W')
        call IssueTargetOrder(data.v, "bloodlust", data.w)
        call UnitApplyTimedLife(data.v, 'BTLF', 1)
        call GroupRemoveUnit(data.g, data.w)
    else
        call data.destroy()
    endif
    set t = null
endfunction
function Shrink takes nothing returns nothing
    local shrink data = shrink.create(GetSpellAbilityUnit(), GetOwningPlayer(GetSpellAbilityUnit()))
    local boolexpr b  = Condition(function SpellFilter)
    call GroupEnumUnitsInRect(data.g, data.r, b)
    call SetHandleInt(data.t, "struct", data)
    call TimerStart(data.t, .008, true, function TheShrink)
    call DestroyBoolExpr(b)
    set b = null
endfunction
function InitTrig_MiniShrinkCast takes nothing returns nothing
    local integer i = 0
    set gg_trg_MiniShrinkCast = CreateTrigger()
    loop
        exitwhen i > 11
        call TriggerRegisterPlayerUnitEvent(gg_trg_MiniShrinkCast, Player(i), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
        set i = i + 1
    endloop
    call TriggerAddCondition(gg_trg_MiniShrinkCast, Condition(function ShrinkCond))
    call TriggerAddAction(gg_trg_MiniShrinkCast, function Shrink)
endfunction