1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Natives and Local variables ...

Discussion in 'Triggers & Scripts' started by mckill2009, Feb 26, 2011.

  1. Fingolfin

    Fingolfin

    Joined:
    Jan 11, 2009
    Messages:
    3,196
    Resources:
    153
    Models:
    143
    Icons:
    1
    Packs:
    4
    Skins:
    2
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    153
    This is not true, the postprocessor will place all globals inside global blocks on the top of the script, so basically, you can declare your globals anywhere you want.

    @thread OP, this is how a globals block look:

    Code (vJASS):
    globals
        timer MY_TIMER
        unit MY_UNIT
    endglobals


    It is very comfortable since you don't have to add the "udg_" part in front of your variables. It is a good idea to give your globals upper-case names so that you can separate them from the locals.
     
  2. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    but if you use a library, I cant compile it If I call a global from another library which is not required by my library...
     
  3. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    OK guys another questions about functions...

    1. Are these the same?...EVENT: Time elapsed 5 seconds...

    2. Is (quote 1) 2 triggers or just 1?...

    OR...
     
  4. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    @1 - I dont understand
    @2 - one trigger, two functions

    Code (vJASS):

    //This should be on top because you can only call functions that are on top of the line which calls it
    function Test takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "I'm a test function." )
    endfunction
     
    function RunTest takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "Calling the test function..." )
        call Test()
    endfunction
     
     
  5. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    I mean is quote 1 and quote 2 the same?...
     
  6. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    no because you removed a line (the one which calls Test) on quote 2...
     
  7. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    OK thanks...

    so basically you need to 'call' the 'upper' function everytime you make functions?...

    Like...
    Code (vJASS):

    Function R1 takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "blahhhh..." )
    endfunction
     
    function R2 takes nothing returns nothing
       call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
       call R1()
    endfunction
     
    function R3 takes nothing returns nothing
       call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
       call R2()
    endfunction
     
    function R4 takes nothing returns nothing
       call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
       call R3()
    endfunction
     
     
  8. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    not really, you just call a function IF NEEDED... and its functions not Functions
    Code (vJASS):

    /*
       the above code is equivalent to this...
    */


    function R1 takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
        call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
        call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
        call DisplayTextToForce( GetPlayersAll(), "blahhhh..." )
    endfunction

     
     
  9. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    OK...but how can you call an 'uneeded' function at the moment?, by using 'if' conditions?...

    sorry for my ignorance kabayan, Im just learning :)...
     
  10. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    why would you call an unneeded function? there's no use... ^_^

    Code (vJASS):

    function IsThisIt? takes nothing returns nothing
         if I'mRight then
             call DisplayTextToForce( GetPlayersAll(), "yeah" )
         else
             call DisplayTextToForce( GetPlayersAll(), "blahhhh" )
         endif
    endfunction
     
  11. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Yeah that's what I mean...I mean 'at the moment', like your trigger example...thanks you're very helpful...
     
  12. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    np... good luck...
     
  13. baassee

    baassee

    Joined:
    Nov 14, 2008
    Messages:
    3,220
    Resources:
    17
    Spells:
    14
    Tutorials:
    3
    Resources:
    17
    It's a difference between constants and just privates. There are some standards out there.
     
  14. Fingolfin

    Fingolfin

    Joined:
    Jan 11, 2009
    Messages:
    3,196
    Resources:
    153
    Models:
    143
    Icons:
    1
    Packs:
    4
    Skins:
    2
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    153
    Ok, my bad.
     
  15. baassee

    baassee

    Joined:
    Nov 14, 2008
    Messages:
    3,220
    Resources:
    17
    Spells:
    14
    Tutorials:
    3
    Resources:
    17
    Although it's up to every coder to use what feels best. But the mods here always complains, you get the point.
     
  16. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,618
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Functions are a special programing strucutre. When you call a function it dumps the contense of your registers onto the stack, dumps the function arguments onto the stack and then jumps to the start of the code for the function recalling passed arguments from the stack. Once the function returns (all functions return, just not always with an argument) it will restore all registers and jump back to the instruction after the call. The stack related dumping and recalling is often made automatically as part of the call instruction due to how stupidly complex some of the x86 instructions are (literally doing whole parts of a program over variable number of cycles).

    Conditional statements and itteration use raw jump instructions which do not cause stack dumps and are much faster as such. These are used for program flow control basically otherwise you would be limited to prety linear instructions which is not very useful.

    So why use calls and not normal jumps? Well calls to functions do not disturbe registers and as such allow you to run code without caring what is currently loaded in the processor. This allows the creation of more predictable code and is great for improving maintainability.

    Thus jass only lets you use calls and restricts jump based structures to within a function.

    A good use of a function is in a Recycle Timer system where by you can call a function to deposit and retreive a blank timer. These functions may be used dozens of times in many systems through your map and so prevent duplication of the same core code as well as allow you to maintain all instances of the code if you find a bug (like a typo which I am sure you know will be comman as grass in a field when programming).

    A bad use of a function is in your example code as you are prety much using it to break appart a linear process into different chunks wasting time and making maintence harder (as each function does not have a clear purpose).

    JASS is rather retarded however... You can not call functions below the caller function and doing so will throw a syntax error. This is not a physical restriction of your computer and almost all languages (java, C, C++) allow you to do that. This is probably due to how the compiler works for jass (no preprocessing) as it has to compile on map load.

    I do not think jass supports itterative functions (where a function calls itself) which again is a perfectly valid behaviour in computer science which most languages support. Be warned that this causes huge strain on the stack and can easilly make your code prone to stack overflow errors in the case of it becomming too nested.
     
  17. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    OK if a function is not called, does it mean..."Skip remaining actions" in GUI???...

    And if I do like this, will it still work?...

    Code (vJASS):

    function Test1 takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "one" )
    endfunction
     
    function Test2 takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "two" )
    endfunction
     
    function Test3 takes nothing returns nothing
        call DisplayTextToForce( GetPlayersAll(), "three" )
        call Test1
    endfunction
     


    I know this can be merged like what Adiktuz said but Im just curious if this
    will work or not?...can Test3 call Test1?...
     
  18. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    test3 will call test1 but test2 wont run since there is no call for it... and its more efficient to combine them (less function calls)...
     
  19. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Now that's clear, so you can call functions even if the caller is at the very bottom...thanks again!
     
  20. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,618
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    No, why the hell should it? That does not even make sense.

    A function can call any number of functions (within the op limit restrictions) and they will be executed linearly line for line (next to when flow control statements change the current line or skip some lines).

    The Skip remaining actions is a return statement. All functions must return and can return at any time (which ignores all code below the return). By default, functions that return nothing will return automatically at the endfunction line. Functions that return a type need to have atleast 1 line that returns tha type. Making a function skip a return will probably crash the game. A function can only return 1 type and 1 value. In real progamming languages you can bundle data together in objects or structs and return a pointer to that to allow multiple pieces of different data to be returned.