• 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.

[JASS] question and answer; a custom tutorial

Status
Not open for further replies.
Hi! I just started to learn JASS because it seems much more smart to me. So i am reading this JASS: Moving From GUI to Jass, the Start - The Helper Forums.

(tutorial)


It is not very easy to comrehend.

As a result i decided to ask the community some questions to different questions i have when i read the tutorial...

Here is my first question:

I convert this trigger into JASS:

  • KillUnit
    • Events
      • Unit - A unit is being attacked
    • Conditions
      • (Unit-type of (Triggering unit)) equal to Soldier
    • Actions
      • Unit - Explode (Triggering unit)

result:

JASS:
function Trig_KillUnit_Conditions takes nothing returns boolean
    if ( not ( GetUnitTypeId(GetTriggerUnit()) == 'hfoo' ) ) then
        return false
    endif
    return true
endfunction

function Trig_KillUnit_Actions takes nothing returns nothing
    call ExplodeUnitBJ( GetTriggerUnit() )
endfunction

//===========================================================================
function InitTrig_KillUnit takes nothing returns nothing
    set gg_trg_KillUnit = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_KillUnit, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_KillUnit, Condition( function Trig_KillUnit_Conditions ) )
    call TriggerAddAction( gg_trg_KillUnit, function Trig_KillUnit_Actions )
endfunction


we have 3 functions there... i just want to know exactly and comprehensable what each function stands for and what they do.
thx very much
 
Level 22
Joined
Jun 24, 2008
Messages
3,050
Well the First function is The conditions.
Second is Actions
And below the ====== There is the Event(Init.)

The init creates a trigger (setting it) and then calls 3 lines.
First line is the what will cause the event to trigger (In this case a unit is attacked)
Second is That it should check the condition
And third is wich action it should execute.

Edit:
Oh yea, you should download the NewGen Jasspack for sure.
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
JASS:
function Trig_KillUnit_Conditions takes nothing returns boolean
    if ( not ( GetUnitTypeId(GetTriggerUnit()) == 'hfoo' ) ) then
        return false
    endif
    return true
endfunction

function Trig_KillUnit_Actions takes nothing returns nothing
    call ExplodeUnitBJ( GetTriggerUnit() )
endfunction

//===========================================================================
function InitTrig_KillUnit takes nothing returns nothing
    set gg_trg_KillUnit = CreateTrigger( )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_KillUnit, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_KillUnit, Condition( function Trig_KillUnit_Conditions ) )
    call TriggerAddAction( gg_trg_KillUnit, function Trig_KillUnit_Actions )
endfunction

I know it already was explained, but I will go in some more detail.
I presume that you know the logic behind GUI :p

First thing are functions.
JASS:
function a takes nothing returns nothing
endfunction
"function" is a keyword that specifies you are going to declare a function.
a is the name
takes is keyword followed by the arguments of the function, I will say something about them later. In this case, it takes nothing.
returns is almost the same as takes, but for the outgoing data.

The function InitTrig_KillUnit if placed in a trigger with name KillUnit is called at the map initialization. ( name = "InitTrig_" + trigger name).

set gg_trg_KillUnit = CreateTrigger( )
set is the standard acquisition operator.
gg_trg_KillUnit is the name of a variable, in this case it is pre-declared, when you created the trigger "KillUnit".
CreateTrigger() initializes the trigger.

call TriggerRegisterAnyUnitEventBJ( gg_trg_KillUnit, EVENT_PLAYER_UNIT_ATTACKED )
call is the standard keyword for calling functions. Functions calls are done with their name and () inside the () are all the parameters the function takes.
The function TriggerRegisterAnyUnitEventBJ adds an event to a trigger.
It has two parameters - ( gg_trg_KillUnit, EVENT_PLAYER_UNIT_ATTACKED )
first one - gg_trg_KillUnit is a trigger - this trigger, second one is some event.

TriggerAddCondition( gg_trg_KillUnit, Condition( function Trig_KillUnit_Conditions ) )
Again as the above, but it ads a condition to the trigger and the second parameter is Condition( function Trig_KillUnit_Conditions )
"function Trig_KillUnit_Conditions" is a code. Meaning it specifies a function.
Condition is a function that takes as parametes a single code. The function that has been stored in the code must return a boolean. Condition () converts the standard function to a condition(this explained you will need much later). So basically when the trigger fires - the event has occurred, the condition function is called, if it returns true the trigger will be executed, if it returns false, nothing will happen.

TriggerAddAction( gg_trg_KillUnit, function Trig_KillUnit_Actions )
Same as above, but it specifies the function that is going to be called when the trigger is executed. And since this is not a condition it does not the Condition function.

Next is the condtion
JASS:
function Trig_KillUnit_Conditions takes nothing returns boolean
    if ( not ( GetUnitTypeId(GetTriggerUnit()) == 'hfoo' ) ) then
        return false
    endif
    return true
endfunction
This is very messy, as it is converted GUI :D
I will rearrange it a little bit
JASS:
    if (  GetUnitTypeId(GetTriggerUnit()) == 'hfoo' )  then
        return true
    endif
    return false

JASS:
if (A_boolean_expression) then
  //actions
endif
is a if/else block
it can be:
JASS:
if (A_boolean_expression) then
elseif (Another_boolean_expression)then
else
endif

GetUnitTypeId(GetTriggerUnit()) == 'hfoo' is a boolean expression( because of the ==). It will return true if the thing on the left side is equal to the thing on the right side. Operators for boolean expressions are "==" "<" ">" ">=" "<=" "!=".
GetUnitTypeId(GetTriggerUnit())
GetUnitTypeId is function that takes a unit and returns its type id.
GetTriggerUnit() is a function that returns the unit that has fired the trigger.

And then for the last:
JASS:
function Trig_KillUnit_Actions takes nothing returns nothing
    call ExplodeUnitBJ( GetTriggerUnit() )
endfunction

If the condition function returns true, this function is called.
It contains a single line:
call ExplodeUnitBJ( GetTriggerUnit() )
ExplodeUnitBJ is a function that takes a unit and does something to it (makes it explode).

Well that is about everything explained, but here something more for functions:
JASS:
function abv takes integer a,integer b,integer c returns real
return I2R(a+b)/c
endfunction
When specifying a parameter you specify its type and a name for it.
Multiple parameters are separated by a coma.
When specifying the return data, you specify only the type.
I2R converts from integer to real.
So this function is called like this:
set a=abv(1,9,4)

Making a equal to 2.5

Here is an example for variables:
JASS:
function a takes nothing returns nothing
local integer a
local integer b=10
set a=b
endfunction
declaration is done by using the keyword local, a type, a unique name.
All variable declarations must be made before any other action.
Lets declare a handle:
local location loc[icode=jass] Now we have a location, but it is kinda of empty. So lets correct that: [icode=jass]set loc = Location (50,-20)
Lets erase it ^^
call RemoveLocation(loc)
and null it
set loc = null
Timer and group:
JASS:
local timer t = CreateTimer()
local group g= CreateGroup()
call DestroyTimer(t)
set t=null
call DestroyGroup(g)
set g=null

Now you have to learn how to clean leaks, there are more leaks with jass than with GUI :D

I recommend getting Jass New Gen Pack
It makes jass look cleaner and has the control + click on a function function which shows some details about the function.
My advice to you, for now continue to convert from gui so that you can learn different functions. At some point start adding your own code. Then start writing it from scratch :)
 
Level 8
Joined
Sep 25, 2007
Messages
382
this help me understand some things...

but , can someone tell me the use of the timer , i know it in GUI but still cant figure how to set it or make it work in JASS...
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Why the hell are you asking a question here? You didn't open this thread.
Nonetheless, I'll be nice and simply give you an example.

JASS:
function bla takes nothing returns nothing
endfunction

function bli takes nothing returns nothing
    local timer t = CreateTimer()

    //              timer, timeout, periodic, callback
    call TimerStart(t,     2,       false,    function bla)
endfunction
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
JASS:
function b takes nothing returns nothing
    local timer t=GetExpiredTimer()
    call BJDebugMsg("tic")
    call PauseTimer(t)
    call DestroyTimer(t)
    set t=null
endfunction
function a takes nothing returns nothing
local timer t=CreateTimer()
call TimerStart(t,10,false,function b)
set t=null
endfunction
TimerStart:
First, is the timer
Than the timeout in seconds
Than whether it is periodic
Then a function to call.
The function or callback must take nothing.
To use something in the timer callback you must use global variables or gamecache or an attachment system.
 
Last edited:
first, a big thx at all of u!
second @ monhcopim:

i made this thread for questios and answers to general JASS ;), because the tutorials u find on different sides are a little bit hard to understand and u cant ask the tutorial about more detailed descriptions ;)

third point:

some more questions

first, the code 'hfoo' describes the soldier...
do i have to know every damn single code for every unit if i want to use them correctly? or do programs like jassnewgen or jasscraft help me out at this point?

seconds thing u said that I2R converts an integer to a real, same with R2I converting a real to an integer or S2I converting a String to Integer??

and the next question:

Can u make me a pure Jass trigger similar to my example above, wich only kills the unit and not explode it, or answer me if there is a better way to make triggers than with three functions, one doing the actions, one the conditions and the last one calling the other two functions when an event occurs...

+rep to spiwn for his detailed help
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
some more questions

first, the code 'hfoo' describes the soldier...
do i have to know every damn single code for every unit if i want to use them correctly? or do programs like jassnewgen or jasscraft help me out at this point?
In normal WE or any other WE, go to the object editor and press Ctrl + D. That will toggle the display of the raw codes of objects. In JNGP you can specify the raw codes of newly created stuff.
seconds thing u said that I2R converts an integer to a real, same with R2I converting a real to an integer or S2I converting a String to Integer??

For those, yes. But not all types can be casted. For instance there is no B2I :p (b as in boolean) And you are not obliged to use I2R, WE casts it automatically(i.e. R2S( 1) is valid).

and the next question:

Can u make me a pure Jass trigger similar to my example above, wich only kills the unit and not explode it, or answer me if there is a better way to make triggers than with three functions, one doing the actions, one the conditions and the last one calling the other two functions when an event occurs...

+rep to spiwn for his detailed help

Instead of call ExplodeUnitBJ(GetTriggerUnit()) - call KillUnit(GetTriggerUnit()) :p

Well, depends on the trigger. But generally conditions are faster than actions. So if you take what is in the actions and put it in the condition, you will get a faster trigger:
JASS:
function Trig_KillUnit_Conditions takes nothing returns boolean
    if GetUnitTypeId(GetTriggerUnit()) == 'hfoo'  then
        call KillUnit(GetTriggerUnit())
    endif
    return false
endfunction

//===========================================================================
function InitTrig_KillUnit takes nothing returns nothing
    set gg_trg_KillUnit = CreateTrigger( )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_KillUnit, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_KillUnit, Condition( function Trig_KillUnit_Conditions ) )
endfunction

Thinking about it now, I reckon actions' function might have been made only because of GUI.
P.s. Ah, yes you can't use waits(triggersleepactions) in a condition.
 
Last edited:
Level 12
Joined
Apr 27, 2008
Messages
1,228
I get it too. I think it is a bug with the JNGP. But it has caused me no problems so far.
 
look at my newest post to the area knockback problem if u have the time to do so:

http://www.hiveworkshop.com/forums/f269/area-knockback-why-does-trigger-not-work-107403/#post913280


would help me a lot

maybe u can give me some advertises what to change in the jass script, cause it was converted form GUI
would be nice to here some stuff about better ways to trgger this
maybe improving to a smooth slide would be nice, since now it moves instantly from one point to another cause there is no wait
 
Last edited:
Status
Not open for further replies.
Top