HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Some questions on functions.

08-22-2007, 09:50 PM#1
Dil999
In the past few days I've devoted myself to learning more JASS, and accomplished alot (timers ,gamecaches, handles, now structs), but there are still a few things which I don't understand. In many JESP spells I've looked at, they use things such as a Constant Function, Private Function, Public Function, Library, etc. I looked through the tutorials here and on HiveWorkshop, and couldnt find anything about what these do. Could you tell me what each of these are meant to do and how to use them?
08-22-2007, 10:02 PM#2
Pyrogasm
A "constant" function is no different than a regular function (it was once though that they were faster) except that they can't call any non-constant function and they're inlined by Vexorian's Map Optimizer. They're normally used for configuration in JESP non-vJASS spells.

A library is basically like a map's Header (Custom Script section).
Quote:
Originally Posted by Toink
Think of a library as a section of code, something like a folder which contains functions. Instead of having them in the custom script section, you can make libraries instead, put all of your desired functions there and organize it along with other libraries. If for instance your library requires some functions outside the library, you can use the line requires <library name> By using that line, libraries that are required will be put "ontop" or before it in the map's head

As for what a Scope is, you ought to read this tutorial.

Public/private functions can only be used inside libraries/scopes. A private function is one that may only be called from within the scope/library it's declared it. They may have the same name as a public function (which is the exact opposite of a private function). Toink explains it rather well:
Quote:
Originally Posted by Toink
What are private members and public members?

Privates are members of it's class. Privates can only be used inside the scope they are in. Privates can also have the same name as other functions/global vars without giving an error. For example :

Collapse JASS:
scope wtfart

    globals
        private unit u // This global can only be used inside the scope wtfart.
    endglobals
   
    function Func takes nothing returns nothing
         set u = GetTriggerUnit() // This function is inside the scope so it can freely use that private
         call KillUnit(u)
    endfunction

endscope

globals
    unit u // Notice that we are using the name u again.
endglobals

function OtherFunc takes nothing returns nothing
    set u = GetTriggerUnit()
    call KillUnit(u)
endfunction

Publics are the opposite of privates. They act just like privates except that they can be used by functions outside the scope. For example :

Collapse JASS:

scope wtfart
    
    private function PrivFunc takes nothing returns string
        return "Hi"
    endfunction

    public function PubFunc takes nothing returns nothing
        call BJDebugMsg(PrivFunc()) // Gets the value returned by the private
    endfunction

endscope

function Func takes nothing returns nothing
    call wtfart_PubFunc() //You have to use the name of the scope it is in as a prefix and add a _
    call PubFunc() // This will not work
    call ExecuteFunc("wtfart_PubFunc")// You still need the prefix
endfunction