[vJASS] globals

Status
Not open for further replies.
Level 7
Joined
Mar 22, 2010
Messages
228
i wonder if this would error my script...
one trigger with this:
JASS:
scope are_you_dumb

globals
    private constant string shit = "NO"
endglobals
endscope

and another trigger with this

JASS:
globals
     constant string shit = "YES!!! :D"
endglobals

i mean 2 global variables with thesame name but 1 is placed on a scope and privated
 
Last edited:
Level 7
Joined
Mar 22, 2010
Messages
228
FINISHED READING :D
unfortunately i didnt understand a thing...

why use a private global variable if you could use a local -_-
 
private globals are useful if your code has several functions, since locals are only usable on the function where they are created... and because locals are created everytime, and needs to be nulled, so using private globals results to less function calls...

and also private globals are useful for constant things...
JASS:
globals
    private constant integer TRY = 0
endglobals

/*
   The global is created only once as opposed to using the local which is created every time the function is called
*/

function example takes nothing returns nothing
    local integer try = 0    
endfunction
 
Level 7
Joined
Mar 22, 2010
Messages
228
oh..! and 1 last question.. why should there be a scope? i have read many tuts but i dont understand a thing.. i want a summary one XD

The global is created only once
so you mean there will be only one global declaration? (e.g)1st Trigg declares blah blah global and 2nd Trigg declares another blooh blooh global <-- that cant be?
 
A code block is a structure used in programming. It defines how to treat what's inside of it. For example, a globals block contains global variables inside of it, meaning anything put into a globals block is treated as a global variable. You cant put a function into a global block can you? : ).

A scope block defines scope for a set of blocks, which is wherever a thing exists. For example, the scope of a local variable is the function it is defined inside of while that function is running** (its scope begins when it is declared and ends when the function ends).

Blocks always start with the blockname and end with endblockname, just like globals endglobals.

Scope blocks are primarily used to allow things to have access modifiers on them. If something is in the global scope, it can't have an access modifier because it's not contained inside of anything, If something is in a scope, it is contained in that scope and can get an access modifier.
 
private/public are access modifiers, which can be applied to fields, globals, functions, and scopes (although a private scope can't be at the top of any scope, have to have a public first).

Here is a common onInit
JASS:
struct Tester extends array
    private static method onInit takes nothing returns nothing
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Hello World")
    endmethod
endstruct
 
You should learn about the order of initialization.

http://www.hiveworkshop.com/forums/pastebin.php?id=skf396

Chapter 18, libraries.

It runs in that order, and then in that structure you have module initializer -> struct initializer -> library/scope initializers.

So first it runs all module initializers in that order (from first library to last thingie), then all struct initializers, then regular initializers (library/scope). This is why people use module initializers for general resources now : ).
 
Level 7
Joined
Mar 22, 2010
Messages
228
im sorry but i still didnt understand :goblin_cry:

and oh my only problem is the scopes.. i knew quite much about blocking ( you posted earlier )
 
Scopes aren't really anything complicated, they just act like containers for code.

Imagine you have a pile of junk.
junk0.png


Anyone can take that and do whatever they want. The junk represents normal code, just sitting around.

Once we add a scope, it is kind of like adding a box.
junk1.png


What does that do? Yeah... nothing. Anyone can still just open the box and take whatever they want. However, boxes (scopes) in this case allow us to put sticky glue on whatever we want to keep in there. That represents private.

So when you do something like this:
JASS:
globals
    integer fun = 0
endglobals

globals
    integer fun = 6
endglobals

It will give an error. If you wrap them in a scope:
JASS:
scope A
    globals
        integer fun = 0
    endglobals
endscope
scope B
    globals
        integer fun = 6
    endglobals
endscope

It will still give an error. However, if you add "private" to any of them (or both), it will make that global unique, so you can use the same name in another scope:
JASS:
scope A
    globals
        private integer fun = 0
    endglobals
endscope
scope B
    globals
        private integer fun = 6
    endglobals
endscope

No error. That means our junk won't be messed with outside of the scope.

As far as other features go... public is something you can mostly ignore. It is kind of like having no prefix, but it can prevent naming collisions. (In the case above, you could add public as the prefix instead, and it would still work fine, but that means that the junk can be messed with outside of the scope, if you want it to be)

initializer is just to point to a function to call on map initialization. (kind of like the GUI event) It is usually just for setup.
 
Level 7
Joined
Mar 22, 2010
Messages
228
well i understand now why scopes are made..
purge's summary helped me alot ^^

also thanks to nestharus gave me idea

+rep
 
Status
Not open for further replies.
Top