• 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] Quick question about private globals and scopes

Status
Not open for further replies.
Level 14
Joined
Apr 20, 2009
Messages
1,543
Suppose I where to create this:

JASS:
scope derp initializer init
   globals
      private integer MUI = 0
   endglobals

    private function doSomething takes nothing returns nothing
        set MUI = MUI + 1
    endfunction

    private function init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        loop
            exitwhen i>11
            call TriggerRegisterPlayerUnitEvent(t, Player(i), 'EVENT_PLAYER_UNIT_DEATH', null)
            set i = i + 1
        endloop
        call TriggerAddAction(t, function doSomething)
        set t = null
    endfunction
endscope

Would the private global variable be MUI?
2 units of different players die at the same time, is the same private global variable used?
Does this mean that MUI keeps increasing for each time a unit dies for every player, or is MUI reset?

Suppose I where to make a spell with more private global variables, would this method be safe to use or should I use a different approach?

(I finally found some time to start on working with vJass so I'm still kind of new. I do however have experience in other programming languages and know enough about OOP.
Which means my learning curve will probably improve with the right explanations.)
 
Level 7
Joined
Apr 5, 2011
Messages
245
Would the private global variable be MUI?
Your example is too abstract for me to talk something about MUI.
2 units of different players die at the same time, is the same private global variable used?
Of course.
Does this mean that MUI keeps increasing for each time a unit dies for every player
Of course.

Also this
JASS:
        loop
            exitwhen i>11
            call TriggerRegisterPlayerUnitEvent(t, Player(i), 'EVENT_PLAYER_UNIT_DEATH', null)
            set i = i + 1
        endloop
looks bad.
This
JASS:
        loop
            call TriggerRegisterPlayerUnitEvent(t, Player(i), 'EVENT_PLAYER_UNIT_DEATH', null)
            set i = i + 1
            exitwhen i == 12
        endloop
looks better.
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
Your example is too abstract for me to talk something about MUI.

Doesn't MUI stand for Multi User Instanceability? Thus when the global variable will be altered on the event of multiple users means it's not MUI?

Also this
JASS:
        loop
            exitwhen i>11
            call TriggerRegisterPlayerUnitEvent(t, Player(i), 'EVENT_PLAYER_UNIT_DEATH', null)
            set i = i + 1
        endloop
looks bad.
This
JASS:
        loop
            call TriggerRegisterPlayerUnitEvent(t, Player(i), 'EVENT_PLAYER_UNIT_DEATH', null)
            set i = i + 1
            exitwhen i == 12
        endloop
looks better.
This is simply personal prefference for readability and doesn't improve the code in any bit.
By the way can you tell me how the initializer is instantiated in the first place? I'm still unfamillair with it.


Also, I'd like to have confirmation of someone else before concluding that this can be seen as not MUI.
I really thought it was :/ How else would I create a MUI spell with scopes? I'm not in the mood of a hashtable solution. Unless it's mandatory ofcourse...
 
Level 7
Joined
Apr 5, 2011
Messages
245
This is simply personal prefference for readability and doesn't improve the code in any bit.
Your code just works for one cycle more.
How else would I create a MUI spell with scopes? I'm not in the mood of a hashtable solution.
What is scope in your op? Does it allow MUI anyhow as itself?
Ofc, no. It's just an incapsulation, nothing significant more. And if it would be, that would cause a hardest desync in your life probably.
"MUI" variable tells me nothing. If it's DEATH_COUNT, that counts all deaths, it's MUI. If it's MY_DEATH_COUNT, that counts your deaths, it is not. You always need indexing (hashtable or arrays) to do something like this.
 
Level 7
Joined
Apr 5, 2011
Messages
245
JASS:
scope derp initializer init
   globals
      private integer array player_death_count
      private integer all_death_count = 0 //to start count with 0
   endglobals

    private function doSomething takes nothing returns nothing
        local integer i = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
        set player_death_count[i] = player_death_count[i] + 1 //personal count
        set all_death_count = all_death_count + 1 //general count
    endfunction

    private function init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        loop
            call TriggerRegisterPlayerUnitEvent(t, Player(i), 'EVENT_PLAYER_UNIT_DEATH', null)
            set array_player_death[i] = 0 //to start count with 0
            set i = i + 1
            exitwhen i == 12
        endloop
        call TriggerAddAction(t, function doSomething)
        set t = null
    endfunction
endscope
This is MUI, but very bad performed ofc.
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
Your code just works for one cycle more.
Oops, you're right it should've been exitwhen i>10 in that case. Your example would be better for readability but I don't really care.

EDIT: wait, wtf am I doing?

What is scope in your op? Does it allow MUI anyhow as itself?
Ofc, no. It's just an incapsulation, nothing significant more. And if it would be, that would cause a hardest desync in your life probably.
"MUI" variable tells me nothing. If it's DEATH_COUNT, that counts all deaths, it's MUI. If it's MY_DEATH_COUNT, that counts your deaths, it is not. You always need indexing (hashtable or arrays) to do something like this.

That's a valid response. I see, thank you. I don't know what I was thinking. I will probably go for a indexing solution in this case. Probably a hashtable so that I can use the handle ID of the unit.
(a unit indexer would be better I suppose, but I'm creating a spell which shouldn't require other systems to be imported.)
 
Last edited:
Level 14
Joined
Apr 20, 2009
Messages
1,543
So, your code worked for 13.33 cycles, since exitwhen was proced in 14th. :p

Now you're confusing me.

Isn't the cycle like this?:
i = 0
0<11

i = 1
1<11

i = 2
2<11

etc. till:
i = 12
12>11

Which means it itterated from 0-11?
 
Level 7
Joined
Apr 5, 2011
Messages
245
^That is not I am talking about.
When i becomes 12 your loop starts 13th cycle. Since each loop is the same code, variable i, pointer to start of the cycle and exit point, you do 1 operation more going to pointer to start of the cycle.
This should make it clear:
JASS:
point BEGIN
if i == 12
 goto EXIT
i = i + 1
goto BEGIN
point EXIT
(bad)
or
JASS:
point BEGIN
i = i + 1
if i == 12
 goto EXIT
goto BEGIN
point EXIT
(good)
Nothing scary if you do as 1st variant tells, but you should realize that it's not just a visual option.
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
^That is not I am talking about.
When i becomes 12 your loop starts 13th cycle. Since each loop is the same code, variable i, pointer to start of the cycle and exit point, you do 1 operation more going to pointer to start of the cycle.
This should make it clear:
JASS:
point BEGIN
if i == 12
 goto EXIT
i = i + 1
goto BEGIN
point EXIT
(bad)
or
JASS:
point BEGIN
i = i + 1
if i == 12
 goto EXIT
goto BEGIN
point EXIT
(good)
Nothing scary if you do as 1st variant tells, but you should realize that it's not just a visual option.

I see what you mean. My mistake, I didn't think of this to happen.
 
Status
Not open for further replies.
Top