Most JASS scripts will have an entry point that determines the flow of the code. It is quite similar to GUI:
-
Example
-
Events
-
Conditions
-
Actions
-
Unit - Kill (Killing unit)
In the trigger above, there is a natural flow to the trigger. First, the events must fire ->
A unit dies.
Then the actions are ran -> kill the attacker.
When this is converted to JASS, the events first need to be registered. You've done that in
function d
, but where is
d
called? How does the editor know that it should call
d
first?
If you convert a trigger from GUI to JASS, you'll notice that all the events are registered in
InitTrig_<Trigger Name>
. This is called an
initializer. For every trigger, the editor will try to run a function following that format when the game is setting up (map initialization), and that is usually the time that triggers/events are created. So if you want to get your code working, create a new trigger (e.g. named "Test") and Edit -> Convert to Custom Text. Remove all the code. Copy and paste your code in. Rename your "d" function appropriately. For example, if the trigger is named "Test", you'll want to name it "InitTrig_Test". Save the map, and try it out!
If you are using
JassNewGenPack, then things are a little easier. You can simply enclose it in a scope or library, and tell it which function is your initializer. Here is some sample code:
JASS:
scope Test initializer d
function Msg takes string s returns nothing
call DisplayTextToForce( GetPlayersAll(), s )
endfunction
function ShowMessage takes nothing returns nothing
call Msg("This is a message!")
endfunction
function ShowSum takes integer a, integer b returns nothing
local integer r=a+b
call Msg("r= "+I2S(r))
endfunction
function Trig_JASS_test_Actions takes nothing returns nothing
call ShowMessage()
call ShowSum(33,45)
endfunction
function d takes nothing returns nothing
local trigger z = CreateTrigger()
call TriggerRegisterTimerEventPeriodic(z, 2)
call TriggerAddAction(z, function Trig_JASS_test_Actions)
endfunction
endscope
Notice the "initializer d" part. This tells the editor to run the function "d" on initialization. Remember,
this code will only compile if you have JassNewGenPack (or JassHelper).