HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Simple Question - Function

11-08-2005, 10:48 AM#1
d4rkarch0n
Hello guys!
Well, i'm starting with JASS. I'm already a programmer, so the only trouble i'm having is the need to search a native function to everything i think about doin'... kinda frustrating anyway, i'd like to have some basic explanations, if someone could help... really simple

I created a trigger, and before typing anything I converted it to custom text. Somethings were already written, u guys know it well:

Collapse JASS:
function Trig_ItemCopy_Actions takes nothing returns nothing
endfunction

//===========================================================================
function InitTrig_ItemCopy takes nothing returns nothing
    set gg_trg_ItemCopy = CreateTrigger(  )
    call TriggerAddAction( gg_trg_ItemCopy, function Trig_ItemCopy_Actions )
endfunction

- Questions
1. Why the function above the "====" line is empty? What is it for?
2. What this "TriggerAddAction" does exactly (I can imagine but... better be sure) with those parameters?

I created the code above. It's intended to transfer the items from one unit (givingUnit) to another (receivingUnit).

Collapse JASS:
function TransferItems takes unit givingUnit, unit receivingUnit returns nothing
    local slotcounter = 1
    local item transferitem = null
    loop
    exitwhen slotcounter > 6
        transferitem = UnitItemInSlotBJ(givingUnit, slotcounter)
    if transferitem != null then
       UnitAddItem(receivingUnit, transferitem)
       UnitRemoveItem(givingUnit, transferitem)
    endif
    set slotcounter = slotcounter + 1
    endloop
endfunction

3. How do I add this function to the trigger I converted? Simply copy-and-paste above the last line there or is there something else?

4. How do I invoke this function from another trigger? Does this "another" trigger need to be in custom text as well?

Thank u for any help guys, very very much
11-08-2005, 12:47 PM#2
Mezzer
Right, well think of triggers as objects that have a event, conditions and actions (The GUI interpretation). The event is litteraly an in-game event (predefined by Blizzard) which runs the rest of the trigger once it fires. The Conditions and Actions are both functions, where Conditions returns a boolean value and Actions does whatever it contains when the trigger fires and the Conditions are met. Thus the TriggerAddAction method will add an action (the block of code you defined somewhere prior to adding it, above the "//===" for example) to a trigger that will be run whenever that trigger fires. Btw, in JASS, anything that follows "//" is considered a comment to the end of that line. And if you want to add a function to a trigger, you cant make it takes any params, so you need to define functions that will do STUFF when called inside your trigger. I hope this came out rationally
11-08-2005, 06:15 PM#3
Anitarf
1. The function is empty because that function is the trigger's actions. You didn't specify any in GUI, so after conversion, you get an empty actions function.

2. TriggerAddAction specifies which function is to run when the trigger's event fires. The function that is to be the action has to be of "takes nothing returns nothing" format. You can use the trigger's event responses in that function and any other function it calls.

3. You can run functions in more ways. The most common is calling them, like: call FunctionName(parameter1, parameter2...) In your case, you would need to pass 2 parameters, the giving and receiving unit. Functions that return a value can be used whereever that value can be used, for example, the function CreateUnit() returns a unit (the one it creates), so you can do this: set unitVariable = CreateUnit(...insert parameters for createunit here, can't remember exactly how they go)

If you call a function, the function where you call it from will wait until the called function finishes what it has to do. If you want the called function to start as a seperate thread, and the calling function to continiue it's execution independantly, you can do that with the function ExecuteFunc(). It takes the name of the function you want to execute as a string parameter. The executed function must be of the "takes nothing returns nothing" format.

4. The only way to call custom functions is through JASS. But you don't need to convert the whole trigger; you can use a special custom script action that allows you to include a JASS line into a GUI trigger (the action is in the general category). Note that if you want to call a function, it must come before the calling function in the map script. If you want to be able to call it from multiple triggers, then you must put your function in the map header (because that's the only part that definitely gets written into the map script first when compiling, the order in which triggers are written is not determined). You get to the map header by clicking on the map name in the trigger editor's treeview of all categories and triggers.

Note, when using ExecuteFunc(), the function that you execute doesn't need to be before the executing function.
11-09-2005, 12:00 AM#4
d4rkarch0n
Thank u very much guys! I'll come up with some more doubts inna few haha, thanx a lot!