• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Custom Function Inside GUI Trigger

Status
Not open for further replies.
Level 33
Joined
Mar 27, 2008
Messages
8,035
I wanna write this simple custom function inside the GUI Trigger, is it possible ?
JASS:
function Test takes nothing returns nothing
   local unit u = GetTriggerUnit()
   call KillUnit(u)
   set u = null
endfunction

When I write that function in the map header, the map does not compile any error.
But when I write that function inside GUI Trigger, it compiles errors.

  • Actions
    • Custom script: function Test takes nothing returns nothing
    • Custom script: local unit u = GetTriggerUnit()
    • Custom script: call KillUnit(u)
    • Custom script: set u = null
    • Custom script: endfunction
Is it possible to create custom function inside GUI Trigger, or the place really in the map header ?
 
Level 17
Joined
Jul 17, 2011
Messages
1,863
oh ok then do it like this
[trigger=trig]Untitled Trigger 004
Untitled Trigger 004
Events
Conditions
Actions
Custom script: endfunction
Custom script: function init takes nothing returns nothing
Custom script: endfunction
Custom script: function lol takes nothing returns nothing
[/trigger]

JASS:
function Trig_Untitled_Trigger_004_Actions takes nothing returns nothing
    endfunction
    function init takes nothing returns nothing 
    endfunction
    function lol takes nothing returns nothing
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_004 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_004 = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Untitled_Trigger_004, function Trig_Untitled_Trigger_004_Actions )
endfunction

as you can see in gui you already have one starting function and a pre placed endfunction so all you have to do is follow this example and you should not have a problem.. but why would u do it this way you dont have syntax checker or highlighter or nothing
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
I usually make systems (for GUI) and create all the codes in map header.
And the installation of my system is to copy all those jass code into map header and it will make it a bit difficult for new GUI user to do that.

So, if I make the custom function inside GUI Trigger, all they have to do is copy and paste the trigger and all the function is prepared.

  • Actions
    • Custom script: endfunction
    • Custom script: function Test takes u returns nothing
    • Custom script: call KillUnit(u)
This compiles 27 errors.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
function Test takes u returns nothing.

If it is undefined variable, why when I put that code in map header, everything works fine ?

I run with this trigger with the map header script (it works fine);
  • Untitled Trigger 002
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
    • Actions
      • Set u = (Target unit of ability being cast)
      • Custom script: call Test(udg_u)
Apparently when I put it at map header, udg_u = u (this is from takes u returns...)
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Triggers functions are constants, which means you can know the function a trigger has by turning it to custom text, check the functionname, and then "Undo" the conversion. So, you can create your custom function in GUI and call the function with the ExecuteFunc("") or just Execute the trigger, since the trigger itself has the function.

Most system uses "u" and "i" for units, and, as far as I know, globals and locals can't have the same name, or their values could get messed up, so, it's better to avoid "u", "i", "p", "h", "r" globals.

If it is undefined variable, why when I put that code in map header, everything works fine ?
Because the name of the variable the function requires is irrelevant.
JASS:
function takes unit u returns nothing
  call KillUnit(u)
endfunction

Now, if you call it from GUI
  • t
  • Actions
    • Set TheUnitIWantToManipulateWithTheCustomFunction = (Triggering Unit)
    • Custom script: call Test(udg_TheUnitIWantToManipulateWithTheCustomFunction)
The system will take "udg_TheUnitIWantToManipulateWithTheCustomFunction" and do with "udg_TheUnitIWantToManipulateWithTheCustomFunction" all the things that the function does with "u", as long as inside the function you manipulate "u".

Remember that functions uses arguments, not "strings". Doesn't matter the name you give to the variables the execution will work as long as you give the right argument (you can set u = bj_lastCreatedEffect and it won't work, because, tough you're using u, that isn't the right argument for the function). They don't even have to be a variable.

call Test(GetTriggerUnit())

This compiles 27 errors.
You're not using "endfunction" at the end of the function.

They don't just have to copy the trigger, but they have to copy it in the right place. If the trigger with your function si at #13 on the list, and the Player is using the function of that trigger in Trigger#8 in the list, it won't work, because the trigger #13 hasn't been declared when you're already calling it in the trigger #8. The map header starts before anything, even before "Map Initialization", so, that's the best spot for any custom function. In worst cases, the player would have to create a folder called "Initialization" and add there (in the correct order) all the triggers that are just used for initialization, and that doesn't call another triggers or another functions, other than the functions already written inside them.
 
Here is one way to do it:
  • GUI Functions
    • Events
    • Conditions
    • Actions
      • Custom script: call ExecuteFunc("InitRandomFunctionYahYah")
      • Custom script: endfunction
      • Custom script: function TestKillUnit takes unit u returns nothing
      • Custom script: call KillUnit(u)
      • Custom script: endfunction
      • Custom script: function InitRandomFunctionYahYah takes nothing returns nothing
      • Custom script: call TestKillUnit(GetTriggerUnit())
      • -------- Do GUI functions here --------
This is how Bribe usually does it in his GUI-friendly systems.

Just an example. It doesn't actually work or anything, but it will compile.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
Hmmm you're explaining functions and stuffs... I just wanna solve my problem, how ?

Can you show me trigger(s) example how to setup custom function inside GUI Trigger ?

Okay, the custom function should, when called, kill unit (set via variable).

Just an example. It doesn't actually work or anything, but it will compile.
How to make it work ?
Yeah, I was looking at Bribe's trigger and saw he made custom function within GUI Trigger, that's what intrigues me to learn it.

So if I name it "Blarghhh", when should I call the name again ?
I cannot see where "Blarghhh" is being called again.
So, what's the use naming your function if you can't call it ?

Like what you did, ExecuteFunc("InitRandomFunctionYahYah"), I can't see you call the name of that function ever again, why ?
Does this "call ExecuteFunc("NAME")" is just a requirement ?
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Custom script: call ExecuteFunc("InitRandomFunctionYahYah")
Custom script: endfunction
Custom script: function TestKillUnit takes unit u returns nothing
Custom script: call KillUnit(u)
Custom script: endfunction
Custom script: function InitRandomFunctionYahYah takes nothing returns nothing
Custom script: call TestKillUnit(GetTriggerUnit())
-------- Do GUI functions here --------
 
Level 17
Joined
Jul 17, 2011
Messages
1,863
oh but its nothing complicated, its just like another map header, however you need 2 things JNPG, and 2 words it looks like this:


JASS:
library Lib <- you need this on the first line 
------------------------------
function init bla bla bla 
call something 
call something 
call something 
endfunction 
function two bla bla 
endfunction 
-------------------------------------
endlibrary <- and this on the last line


the text in between is just functions you can call
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
What's the difference between having a library with several functions inside it, and having all those functions by separate in the map header?
 
Level 17
Joined
Jul 17, 2011
Messages
1,863
I usually make systems (for GUI) and create all the codes in map header.
And the installation of my system is to copy all those jass code into map header and it will make it a bit difficult for new GUI user to do that.

So, if I make the custom function inside GUI Trigger, all they have to do is copy and paste the trigger and all the function is prepared.
this i think :thumbs_up:
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
Here is one way to do it:
  • GUI Functions
    • Events
    • Conditions
    • Actions
      • Custom script: call ExecuteFunc("InitRandomFunctionYahYah")
      • Custom script: endfunction
      • Custom script: function TestKillUnit takes unit u returns nothing
      • Custom script: call KillUnit(u)
      • Custom script: endfunction
      • Custom script: function InitRandomFunctionYahYah takes nothing returns nothing
      • Custom script: call TestKillUnit(GetTriggerUnit())
      • -------- Do GUI functions here --------
This is how Bribe usually does it in his GUI-friendly systems.

Just an example. It doesn't actually work or anything, but it will compile.

Why, for function InitRandomFunctionYahYah, it calls ExecuteFunc while for function TestKillUnit, we don't do ExecuteFunc("TestKillUnit") ?

EDIT:
Wait, so TestKillUnit function is inside InitRandomFunctionYahYah function, right ?

This gives me 28 errors;
  • Actions
    • Custom script: call ExecuteFunc("UnitKill")
    • Custom script: endfunction
    • Custom script: function Test takes nothing returns nothing
    • Custom script: call KillUnit(udg_u)
    • Custom script: endfunction
    • Custom script: function UnitKill takes nothing returns nothing
    • Custom script: call Test()
Yeah it compiles perfectly (no errors), but when I want to use for another trigger, it says "Expected a function name"
  • Untitled Trigger 002
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
    • Actions
      • Custom script: call UnitKill()
 
Last edited:
Level 20
Joined
Jul 14, 2011
Messages
3,213
Why, for function InitRandomFunctionYahYah, it calls ExecuteFunc while for function TestKillUnit, we don't do ExecuteFunc("TestKillUnit") ?
Because InitRandomFunctionYahYah has no argument requirements. You could also use "call InitRandomFunctionYahYah()"

Wait, so TestKillUnit function is inside InitRandomFunctionYahYah function, right ?
No. It isn't. As you can see there's an "endfunction" before you declare the "TestKillUnit" function.

Yeah it compiles perfectly (no errors), but when I want to use for another trigger, it says "Expected a function name"
Because there's no "UnitKill()" function. It's "KillUnit()" or "Test()" :p
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
Because there's no "UnitKill()" function. It's "KillUnit()" or "Test()" :p
How there's no function when I just created them LOL.
Don't tell me you're talking about call KillUnit(whichUnit) ? Hell no ;p

  • Actions
    • Custom script: call ExecuteFunc("UnitKill")
    • Custom script: endfunction
    • Custom script: function Test takes nothing returns nothing
    • Custom script: call KillUnit(udg_u)
    • Custom script: endfunction
    • Custom script: function UnitKill takes nothing returns nothing
    • Custom script: call Test()
And yeah I tried both UnitKill() and Test(), both gives me the same error message.
 
It is because WE parses triggers in a weird order. Just enclose it in a library. Requires vJASS:
  • Custom script: call ExecuteFunc("UnitKill")
  • Custom script: endfunction
  • Custom script: library RandomLib
  • Custom script: function Test takes nothing returns nothing
  • Custom script: call KillUnit(udg_u)
  • Custom script: endfunction
  • Custom script: endlibrary
  • Custom script: function UnitKill takes nothing returns nothing
  • Custom script: call Test()
Unless you mean you want to call the "UnitKill" function. But idk why you would want to do that if it just calls "Test()". If you do want to call UnitKill, then you have to put that within the library, then change the initialization function. It really depends on what you are trying to do. For example:
  • Custom script: endfunction
  • Custom script: library RandomLib
  • Custom script: function Test takes nothing returns nothing
  • Custom script: call KillUnit(udg_u)
  • Custom script: endfunction
  • Custom script: function UnitKill takes nothing returns nothing
  • Custom script: call Test()
  • Custom script: endfunction
  • Custom script: endlibrary
  • Custom script: function DummyFunctionForCompilingProperly takes nothing returns nothing
That will work too if you want to call UnitKill() for whatever reason. The reason that the dummy function is there at the bottom is because the trigger wraps the code around a function, so if you declare the endfunction at the top of the script, you have to make sure there is a function at the bottom that is open so that it will compile without errors. Good luck.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
It is because WE parses triggers in a weird order. Just enclose it in a library. Requires vJASS:
But when we import Unit Indexer system by Bribe into our map, we don't even need vJASS ?
Did that system uses custom functions too ?
I bet it would.

So, how did he do ?
Setup custom function inside GUI Trigger without the needs of library (vJASS) ?
 
Level 1
Joined
Aug 29, 2012
Messages
3
Map title before you start anything, even in the "map initialization, so this is the best point to any custom functions When women buy Chain strap shoulder bag, they buy women bags at cwmalls.com no matter how many they maybe just as long as it can feed their fancy. Choose best Black chain strap bag can only happen at limited time though and not always.
 
Last edited:
Level 33
Joined
Mar 27, 2008
Messages
8,035
Map title before you start anything, even in the "map initialization, so this is the best point to any custom functions
Do you mean I should save the map first (giving it a name), only then I can create custom function inside GUI Trigger ?
I already did that, but the error keeps saying "expected a function name" when I call the function such as call UnitKill(), as if UnitKill function does not exist.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
I decided to install JNGP to my WE, I have tested it (the same code arrangement), it can compile very wonderfully.

But when it comes to Normal WE, you can't compile it (perhaps JNGP has a special function which compiles GUI Trigger properly).

Although I can't test the map in my JNGP WE, it says "wehack.lua:437: attempt to index global 'rtc_enabled' (a nil value)"

Anyone knows how to solve this ?

It's quite painful for me to create the function inside JNGP and test it inside normal WE, lol.

Another method, yeah the original one, is to create those functions inside map header, but my aim is to let users copy triggers and directly custom functions created for them, it's easy.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
try disabling reinventing the craft
the thing is as stated that normal WE parses the code in some shitty way while JNGP parses triggers in orders they are put in as triggers
also yes Bribes GFDDS has custom functions but they are not made to be called from other triggers

edit: you could do executefunc and input test as it doesnt take anything but it is quite slow function call
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Do you mean I should save the map first (giving it a name), only then I can create custom function inside GUI Trigger ?
I already did that, but the error keeps saying "expected a function name" when I call the function such as call UnitKill(), as if UnitKill function does not exist.

Not at all. Just create the custom functions in the Map Initialization trigger (as GUI) or in the Map Header (The Map icon with Map Name over the first trigger)
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Triggers are made of EventFunctions, ActionFunctions, and ConditionsFunctions.

In the map header you can do pretty much... everything. You can store any kind of function there (Events, Actions, Conditions, Globals, etc.) and use them in all the triggers below.

Example:
If you have several triggers with the same condition, you can create the condition in the header, and add that same condition to all the triggers you need, instead of creating the same condition several times.
 
Level 10
Joined
Jun 6, 2007
Messages
392
This should clarify it a bit:
  • trigger
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: call ExecuteFunc("Init")
      • Custom script: endfunction
      • Custom script: function Kill takes unit u returns nothing
      • Custom script: call KillUnit (u)
      • Custom script: endfunction
      • -------- first trigger --------
      • Custom script: function Actions takes nothing returns nothing
      • Set u = (Random unit from (Units currently selected by Player 1 (Red)))
      • Custom script: call Kill(udg_u)
      • Custom script: endfunction
      • -------- second trigger --------
      • Custom script: function LoopActions takes nothing returns nothing
      • Custom script: set udg_u = GetEnumUnit()
      • Custom script: call Kill(udg_u)
      • Custom script: endfunction
      • Custom script: function SecondTriggerActions takes nothing returns nothing
      • Custom script: call ForGroupBJ( GetUnitsSelectedAll(Player(0)), function LoopActions )
      • Custom script: endfunction
      • -------- initializing triggers --------
      • Custom script: function Init takes nothing returns nothing
      • Custom script: local trigger t1 = CreateTrigger()
      • Custom script: local trigger t2 = CreateTrigger()
      • Custom script: call TriggerRegisterPlayerChatEvent( t1, Player(0), "kill one", true )
      • Custom script: call TriggerAddAction( t1, function Actions)
      • Custom script: call TriggerRegisterPlayerChatEvent( t2, Player(0), "kill all", true )
      • Custom script: call TriggerAddAction( t2, function SecondTriggerActions)
Be careful when using loop gui functions. They often create separate functions in the beginning of the trigger, and you can't call functions before they are declared. In this example, using the gui alternative of " Custom script: call ForGroupBJ( GetUnitsSelectedAll(Player(0)), function LoopActions )" would lead to a compile error.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
For those who gave suggestions "hey you should create custom function inside map header", that's why I name this thread Custom Function Inside GUI Trigger.

@Erkki2
Thanks, you solved it :)
I can now create Custom Function inside GUI Trigger without JNGP.
Also yeah, as stated by edo, you should null the trigger.
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Just one obvious yet not mentioned thing: You can use common GUI actions within the custom scripts to create a function blabla takes nothing returns nothing and the respective endfunction.
 
Status
Not open for further replies.
Top