Public and Privates +rep

Reaction score
341
Well , ive seen many vJASS Systems and Spells using public and private functions. Ive also read some vJASS Tutorials and i dont really understand them. Could somebody please explain to me what they are and a good example use of them. + rep.
 

Trollvottel

never aging title
Reaction score
262
public can be refered to from outside the scope/library

privates are only for the scope/library

should not be so hard to find a use for this, huh
 
Reaction score
341
Haha , im a vJASS newb and trying to learn it, im am also confused about scopes

EDIT : ok , another question i think i understand structs , Basicly the main purpose it to replace locals , to make your code shorter?
 

Azlier

Old World Ghost
Reaction score
461
Okay, lets assume you have two healing functions.


JASS:

function Heal takes nothing returns nothing
//blah, blah, blah...
endfunction

//=================================
function Init takes nothing returns nothing
 local trigger t = CreateTrigger()
 call TriggerAddAction(t,Heal)
 call TriggerRegisterAnyUnitEventBJ(t,EVENT_HERE)
 set t = null
endfunction

//NEXT TRIGGER

function Heal takes nothing returns nothing
//Yap, yap yap...
endfunction

//==============================
function Init takes nothing returns nothing
 local trigger t = CreateTrigger()
 call TriggerAddAction(t,Heal)
 call TriggerRegisterAnyUnitEventBJ(t,EVENT_HERE)
 set t = null
endfunction


Now, when defining the second Heal function, there's going to be a syntax error. Since Heal is a good function name and plan to use it, what can you do besides adding some weird prefix? That's where scopes come in.

A scope is broken down into four words (or something like that).
scope - This declares the start of a scope.
HealAll - This is the name of the scope.
initializer - The tells the system that the Initializer function is next.
Init - This is the name of the chosen initializer. If you look below, you can actually see the Init function.

JASS:

scope HealAll initializer Init

private function Heal takes nothing returns nothing
//blah, blah, blah...
endfunction

//=================================
private function Init takes nothing returns nothing
 local trigger t = CreateTrigger()
 call TriggerAddAction(t,Heal)
 call TriggerRegisterAnyUnitEventBJ(t,EVENT_HERE)
 set t = null
endfunction

endscope

//NEXT TRIGGER
scope HealOne initializer Init

private function Heal takes nothing returns nothing
//blah, blah, blah...
endfunction

//=================================
private function Init takes nothing returns nothing
 local trigger t = CreateTrigger()
 call TriggerAddAction(t,Heal)
 call TriggerRegisterAnyUnitEventBJ(t,EVENT_HERE)
 set t = null
endfunction

endscope


Scopes add a prefix to private functions, so that you can use the same function name over and over, as long as they're private and in separate scopes. At least that's what I've learned...

Wow... I don't have warcraft III so I'm limited to JASS in notepad. It looks so much more professional with the coloring, even though I only know three events, how to display text, and explode units without a bj!

EDIT: Woah, TriggerHappy, weren't you the one who inspires me to learn JASS in the first place?
 
Reaction score
341
Haha , i was just messing around in world editor and learned this , and now i check back and you did the same thing. But what i dont get is , you cant call private functions from outside of the scope?....
 

Trollvottel

never aging title
Reaction score
262
But what i dont get is , you cant call private functions from outside of the scope?....

why do you think they are called "private"?^^
 

Azlier

Old World Ghost
Reaction score
461
If you want to be able to call them from outside the scope, it goes public function, not private. BEWARE: You can't have public functions with the same name AFAIK.
 

Azlier

Old World Ghost
Reaction score
461
A private function is easily called... if called within the scope in which it resides. To get them from somewhere else, you eed to use public.
 
Reaction score
341
oh.... It just needs to be inside the scope to be called. Thanks


UGH , i still have no idea on why you would need to use this
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
oh.... It just needs to be inside the scope to be called. Thanks


UGH , i still have no idea on why you would need to use this

It's to prevent you from having problems with functions with conflicting names, when creating/importing functions.

It's also nice so that you can name every trigger's InitTrig function

public/private function Init/InitTrig

public/private function Actions

public/private function Conditions

That way you don't have to change as much if you choose to copy/paste something.
 

saw792

Is known to say things. That is all.
Reaction score
280
You wouldn't...

All coding now is done in either a scope or a library. A library is like a scope, except it is automatically moved to the top of the jass script upon saving. Libraries are generally used for systems that are needed by multiple other functions.

I think somebody was confused about publics as well. You can certainly have a public function with the same name as another public function, as long as they are not in the same scope/library.
JASS:
function Test takes nothing returns nothing

compiles to

function Test takes nothing returns nothing


JASS:
public function Test takes nothing returns nothing

compiles to

function SCOPENAME_Test takes nothing returns nothing


JASS:
private function Test takes nothing returns nothing

compiles to

function SCOPENAME__Test+(Some random digits or something) takes nothing returns nothing
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top