• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] Functions inside a trigger

Status
Not open for further replies.
Level 2
Joined
Jul 18, 2014
Messages
10
I have trigger #1 converted to text. I deleted everything from there and wrote some functions. I use those functions in other triggers, both Jass and GUI ones. Everything works perfectly fine. Today I tried to create trigger #2 with the same purpose, but when I save a map I get error message "Undeclared function" in every trigger that used functions from trigger #2. Both #1 and #2 have the same settings ('Enabled' and 'Run on map Initialization'), but only functions from #1 can be used in other triggers. Is there something I am missing?
 
When a map is saved, the editor gathers all the triggers, converts them to JASS, and puts it in one huge script file (war3map.j). In that file, you can only call functions declared above the place you're calling it from. For example, this is a valid script:
JASS:
function A takes nothing returns nothing
endfunction

function B takes nothing returns nothing
    call A() // valid
endfunction

But what if it was like this?
JASS:
function B takes nothing returns nothing
    call A() // syntax error: undeclared function
endfunction

function A takes nothing returns nothing
endfunction

That is an error, since the function "A" doesn't come before function "B".

So how do we work around this? With the default editor, it is a bit tricky. You have a couple of options:
  • Check out this guide:
    Use Functions From Other Triggers - By Almia
    It provides some clarity on the way triggers are ordered when they are compiled together, and perhaps that solution will work for you.
  • Another option is to place the functions you need in the map header. To view the map header, open the trigger editor, and click on your map's name (where all the triggers/folders are listed). You should see a text window to your right. If you need to use a function across multiple triggers, put it there. Those functions are always moved to the top of the script. Just note that you should not put your "InitTrig_" functions there--they have to be in their respective triggers in order to be executed.
  • Another alternative is to use JassNewGenPack, found in our tools section. It makes life a bit easier as a JASS user through its extended language "vJass". If you want to use a function in another trigger, you can just put "library" around it.
    JASS:
    library Example
        function FunctionYouWantToUseInAnotherTrigger takes nothing returns nothing
            call KillUnit(GetTriggerUnit())
        endfunction 
    endlibrary

    You can also make libraries require other libraries, e.g. library A requires Example, and that will dictate the order in which things are compiled into the war3map.j. To read more about it, check out the manual:
    http://www.wc3c.net/vexorian/jasshelpermanual.html
 
Level 2
Joined
Jul 18, 2014
Messages
10
Thanks for replies and guides. They gave me an idea about that and now it works as I wanted.
Triggers are compiled not from top to down as functions inside, but from oldest to newest ones. So I just replaced old GUI trigger with Jass functions and now new triggers can freely use them.
 
Status
Not open for further replies.
Top