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.

Use functions from other triggers

Discussion in 'JASS/AI Scripts Tutorials' started by Almia, Apr 4, 2013.

  1. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Use functions from other triggers [Standard JASS]



    I. Introduction
    This tutorial allows you to use the functions from other triggers.
    There are problems we experience when we want to use other functions from other triggers.Examples is that we get syntax error when we try to use functions from other triggers with or without arguements. You will probably need a JNGP to use the functions without conflicts/problems that you will experience.




    II. What are the problems?
    Code (vJASS):

    function Message takes string s returns nothing
        call BJDebugMsg(s)
    endfunction

    //===========================================================================
    function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
        set gg_trg_Untitled_Trigger_001 = CreateTrigger(  )
    endfunction


    This example of snippet will explain it.

    We want to use the function MessageX to display messages, but the thing is that if we try to use this like this from other triggers :
    call Message("Hello")
    , this will give us a syntax error " Expected a function name ".But if you use it inside the trigger it is located, it will work perfectly.




    III. The Solution

    I discovered the solution when I tried to code a system using scripts. The solution to this problem is to use a simple snippet of function inside the Map Header(the top-of-the-script Script, located in the upper-most of the Trigger Editor with the map icon). We shall add a dummy function there so that we can use the functions properly. Example : Let's add this simple snippet of code :
    Code (vJASS):
    function EnableScriptUsage takes nothing returns nothing
    endfunction


    So what does this do?
    Nothing. But when a map header has a function inside it, it will allow any functions from the triggers to be used by external triggers.
    Simple huh?





    IV. Known bugs

    You can experience some bugs when using this trick. Try removing any function in the map header while other triggers uses the functions from the external triggers.Enable/Disable the trigger that uses external functions (to check syntax), this won't show any syntax error.But when you tried to save the map, a syntax error message will show.

    There is a bug also when attempting to move the scripts. The trigger you are going to move in must be the first trigger to be created on your map,or else you are doomed.

    The solution to the problem is replacing your first trigger,then move all of the actions to a replica.Then move the scripts you want to the trigger replaced.

    Note: All of the triggers in your map will be moved in the header,and then will be arranged based on how old they are in the Trigger Editor,starting from the oldest,which will be on top,and the newest,will be on the bottom.



     
    Last edited by a moderator: May 1, 2013
  2. Maker

    Maker

    Joined:
    Mar 6, 2006
    Messages:
    9,181
    Resources:
    17
    Maps:
    2
    Spells:
    14
    Tutorials:
    1
    Resources:
    17
    I'm not sure if this warrants a tutorial.

    Additionally I don't encourage people to rely on bunch of map header code, as it can get very cluttered. Just stick to gui or use vjass.
     
  3. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    They will just use the map header code for a 2-lined function.
     
  4. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,625
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    so if I put a empty function inside the map header I wont get expected function name?
     
  5. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Correct.
    This is good to prevent the map header with so much API inside it.
     
  6. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,625
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    that is really useful, rep +
     
  7. looking_for_help

    looking_for_help

    Joined:
    Dec 12, 2012
    Messages:
    961
    Resources:
    5
    Spells:
    2
    JASS:
    3
    Resources:
    5
    You don't even have to keep the function.

    Once you saved your map with a function inside the map header external scripts get unlocked even if you remove the function again (you get a message like "own script code is used...").

    So you can keep your map header completly clean.
    -> Very useful.
     
    Last edited: Apr 9, 2013
  8. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Thanks for the feedback :D
     
  9. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Updated.
    I found some bugs.
     
  10. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Sorry about the super late response. I had to test this myself.

    It seems to work for the most part. There are some issues (e.g. if you have A call B, then B call C or something like that), but I seemed to work around them by deleting and recreating the triggers as you said.

    I remember hearing someone say that WE compiles it in a random order, it would be cool if you could figure out the exact order WE compiles it in (or confirm that it is random). Would be interesting to know. :)

    As for the tutorial, it is well-explained. The only issues are the signature and the font + color. However, those are somewhat minor. I will detach the signature, and then it is approved.
     
  11. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Actually,the scripts are ordered based on how old the trigger is(just like what i have said)
    It goes like this:
    Oldest Trigger A
    Older Trigger B
    Old Trigger C
    Young Trigger D
    Younger Trigger E
    Youngest Trigger F
     
  12. Daffa the Mage

    Daffa the Mage

    Map Moderator

    Joined:
    Jan 30, 2013
    Messages:
    7,730
    Resources:
    28
    Packs:
    1
    Maps:
    8
    Spells:
    17
    Tutorials:
    2
    Resources:
    28
    This doesn't apply if
    ExecuteFunc
    is used (which is ugly with their slow response though), and the
    TriggerExecute
    thingy as well. Sadly, they're not so similar with executing the function with their returns and such.
     
  13. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,641
    Resources:
    3
    Spells:
    3
    Resources:
    3
    @Daffa the Mage
    I did make a system so ExecuteFunc() can have return values, parameters, etc.
    I made a String2JASS system :D

    It is supposed to convert chat messages to JASS so you can get variable values, set variable values, execute functions, etc during gameplay to test the map.
    However... it is hard to implement and is not yet finished.
    But someone said it was bull shit to have... so I stopped working on it.
     
  14. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,625
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    This no longer works fully for me btw. Even with a function in the header the editor still saves the oldest trigger first. So I get undeclared function name in quite a lot of my projects these days. I have to keep copy pasting stuff :-/
     
  15. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,641
    Resources:
    3
    Spells:
    3
    Resources:
    3
    A function in the header is always saved at the top (with the exception of libraries).
    I guess you are missing something.
     
  16. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,839
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Maybe you didn't read my instruction :)

    I rather suggest pasting first the systems where other codes rely, codes that rely to systems that will be relied, and codes that rely to other codes
     
  17. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,625
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    Easier said than done :p
     
  18. SAUS

    SAUS

    Joined:
    May 22, 2015
    Messages:
    1,050
    Resources:
    0
    Resources:
    0
    Hey, I found something out about the trigger order when saving:

    When you add a new trigger and save, it is saved at the bottom of the script. This means it will break if you create a new trigger that has a function that you want to use in a trigger that already existed or a trigger that you created in the current session before this new trigger. This is the known problem that forces you to put your global functions inside the header script.

    However, I found out something that might be useful. If you save your map, and then close the editor, reopen the editor, load the map, and then write a syntax error in the header script (then try to save), you will see that the triggers are now defined in the order that they appear in your trigger editor window. This allows some purposeful ordering of trigger creation (the InitTrig functions) and also allows you to write libraries in regular JASS and use them in all your triggers.

    The steps to make it work are:
    1) Create the new trigger that will be your system or whatever.
    2) Leave your trigger blank, and move it in the trigger editor to the spot you want it to be loaded.
    3) Save your map.
    4) Close the world editor.
    5) Open the world editor and load your map.
    6) Add any functions you want to use to the blank trigger.

    Then you can use those functions from any other trigger below it in your code. Just remember that if you add a new trigger above this system trigger, it might work if you use functions from it on the first save, but it might also start to report errors if you try to save again after closing and reopening.

    Anyway, I thought some people might like this.
     
  19. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,641
    Resources:
    3
    Spells:
    3
    Resources:
    3
    Aka... use libraries.

    When you save the map, the editor compiles all triggers/scripts into one big file, known as the "war3map.j".

    Thinking code-wise for the editor, all triggers are inside a list (array) and when you create a new trigger, you just increase the array by 1 and place your new trigger in the last slot.
    "But hey! Doesnt that mess with the order of the triggers?" Not really, because you dont want to rely on something like the order of triggers, so we dont care how it is.
    If you want to use functions globally, then use either the header script or a precompiler like JASSHelper (vJASS).

    That the order of how the triggers are used in the order of how they are shown is probably because the array will be recreated in a certain moment of the steps... which is a glitch... or at least, it looks like it.

    So... It is funny to know such things, but not really useful as there are way better things to rely on.
     
  20. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    @SAUS: Pretty cool! But is it in the order specified in the trigger editor, or is it based on when the trigger was made? (e.g. older ones go first?)

    The reason I ask is because Almia's note at the end seems to conflict with your theory:
    If you can verify that it behaves like you said, I'll link your post in the main tutorial for the benefit of other users. :)