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

[Solved] Can I call functions in other triggers?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
  • IncomeTimer
    • Events
      • Time - incomeTimer expires
    • Conditions
    • Actions
      • -------- Expected a function name?! --------
      • Custom script: call GiveIncome()
JASS:
function PickAllTowns takes nothing returns nothing

  local integer pID = GetPlayerId(GetOwningPlayer(GetEnumUnit()))
  local integer uID = GetUnitUserData(GetEnumUnit())

      // Adds Global resources from Picked Town to Owning Player

    set udg_playerResources[pID]      = udg_playerResources[pID]      + udg_baseResourceGold   [uID + 2000] 
    set udg_playerResources[pID + 15] = udg_playerResources[pID + 15] + udg_baseResourceLumber [uID + 2000] 
    set udg_playerResources[pID + 30] = udg_playerResources[pID + 30] + udg_baseResourceIron   [uID + 2000]


      // Adds Local resources from picked Town to Stock and Player Count  
      // Check for breaches and adds appropriate amount if breached

      if udg_baseResourceRecruits [uID]      + udg_baseResourceRecruits [uID + 2000] < udg_baseResourceRecruits       [uID + 1000] then

          set udg_playerResources [pID + 45] = udg_playerResources      [pID         + 45] + udg_baseResourceRecruits [uID + 2000] 
          set udg_baseResourceRecruits [uID] = udg_baseResourceRecruits [uID]        + udg_baseResourceRecruits       [uID + 2000]

        else //Cap was Breached
 
          set udg_playerResources      [pID + 45] = udg_playerResources  [pID + 45] + (udg_baseResourceRecruits [uID + 1000] - udg_baseResourceRecruits [uID]) 
          set udg_baseResourceRecruits [uID]      = udg_baseResourceRecruits [uID + 1000]

      endif 

      if udg_baseResourceFood     [uID]      + udg_baseResourceFood  [uID + 2000] < udg_baseResourceFood [uID + 1000] then

          set udg_playerResources [pID + 60] = udg_playerResources   [pID + 60]   + udg_baseResourceFood [uID + 2000] 
          set udg_baseResourceFood[uID]      = udg_baseResourceFood  [uID]        + udg_baseResourceFood [uID + 2000]

        else //Cap was Breached

          set udg_playerResources  [pID + 60] = udg_playerResources [pID + 60] + (udg_baseResourceFood [uID + 1000] - udg_baseResourceFood [uID]) 
          set udg_baseResourceFood [uID]      = udg_baseResourceFood    [uID + 1000]

      endif 
    
      if udg_baseResourceWeapons  [uID     ] + udg_baseResourceWeapons  [uID + 2000] < udg_baseResourceWeapons  [uID + 1000] then

          set udg_playerResources      [pID + 75] = udg_playerResources      [pID + 75] + udg_baseResourceWeapons  [uID + 2000]
          set udg_baseResourceWeapons  [uID]      = udg_baseResourceWeapons  [uID]      + udg_baseResourceWeapons  [uID + 2000]

        else  //Cap was Breached 

          set udg_playerResources     [pID + 75] = udg_playerResources [pID + 75] + (udg_baseResourceWeapons [uID + 1000] - udg_baseResourceWeapons [uID]) 
          set udg_baseResourceWeapons [uID]      = udg_baseResourceWeapons [uID + 1000] 

      endif
endfunction

function GiveIncome takes nothing returns nothing
  local integer i = 0

    call BJDebugMsg("Income Recived...")
    call ForGroup( udg_townGroup, function PickAllTowns )

      // Update Multiboard if a Base is currently selected
    loop
      exitwhen i > 10
        if IsUnitInGroup(udg_playerSelectedBase[i], udg_townGroup) == true or IsUnitInGroup(udg_playerSelectedBase[i], udg_campGroup) == true then
          call BJDebugMsg("A multiboard selection was found!")
          //set udg_transferredData_pID = i 
            //call TriggerExecute( gg_trg_UpdateMultiboardValues ) 
            call UpdateMultiboardValues(i) //Expected a function name?!
        endif
      set i = i + 1
    endloop
endfunction

//===========================================================================
function InitTrig_GiveIncome takes nothing returns nothing
endfunction

JASS:
function UpdateMultiboardValues takes integer pID returns nothing

  local unit u           = udg_playerSelectedBase[pID]
  local integer uID      = GetUnitUserData(u)
  local integer pID2     = GetPlayerId(GetOwningPlayer(u))
  

    if IsUnitInGroup(u, udg_townGroup) == true and IsUnitInGroup(u, udg_campGroup) != true then

        // Town is in current Selection

        if IsPlayerEnemy(ConvertedPlayer(pID+1), GetOwningPlayer(u)) != true or udg_baseRevealed[uID] == true then
        
        // Friendly Town or revealed is in current selection

          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 1 ,1 ), (I2S(udg_baseResourceRecruits [uID]) +  " / " + I2S(udg_baseResourceRecruits [uID + 1000]))) 
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 2 ,1 ), (I2S(udg_baseResourceFood     [uID]) +  " / " + I2S(udg_baseResourceFood     [uID + 1000])))
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 3 ,1 ), (I2S(udg_baseResourceWeapons  [uID]) +  " / " + I2S(udg_baseResourceWeapons  [uID + 1000])))
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 6 ,1 ), I2S(udg_baseResourceGold     [uID + 2000])) 
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 7 ,1 ), I2S(udg_baseResourceLumber   [uID + 2000])) 
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 8 ,1 ), I2S(udg_baseResourceIron     [uID + 2000])) 
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 9 ,1 ), I2S(udg_baseResourceRecruits [uID + 2000])) 
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 10,1 ), I2S(udg_baseResourceFood     [uID + 2000])) 
          call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 11,1 ), I2S(udg_baseResourceWeapons  [uID + 2000]))

          if IsPlayerEnemy(ConvertedPlayer(pID+1), GetOwningPlayer(u)) != true then

            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 14,1 ), I2S(udg_playerResources[pID2     ]))  // Gold 
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 15,1 ), I2S(udg_playerResources[pID2 + 15]))  // Lumber
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 16,1 ), I2S(udg_playerResources[pID2 + 30]))  // Iron
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 17,1 ), I2S(udg_playerResources[pID2 + 45]))  // Recruits
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 18,1 ), I2S(udg_playerResources[pID2 + 60]))  // Food
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 19,1 ), I2S(udg_playerResources[pID2 + 75]))  // Weapons

          endif

          else

            // Not equal to friendly town or revealed

          endif


      else

        // Camp is in current Selection

          if IsPlayerEnemy(ConvertedPlayer(pID+1), GetOwningPlayer(u)) != true then

            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 6 ,1 ), I2S(udg_playerResources[pID2     ]))  // Gold 
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 7 ,1 ), I2S(udg_playerResources[pID2 + 15]))  // Lumber
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 8 ,1 ), I2S(udg_playerResources[pID2 + 30]))  // Iron
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 9 ,1 ), I2S(udg_playerResources[pID2 + 45]))  // Recruits
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 10 ,1 ), I2S(udg_playerResources[pID2 + 60]))  // Food
            call MultiboardSetItemValue( MultiboardGetItem( udg_multiboard[pID], 11,1 ), I2S(udg_playerResources[pID2 + 75]))  // Weapons

          endif

    endif

 // if (udg_baseRevealed[uID] == true or IsPlayerEnemy(p, GetOwningPlayer(u)) != true) and IsUnitInGroup(u, udg_townGroup) == true then

endfunction

//===========================================================================
function InitTrig_UpdateMultiboardValues takes nothing returns nothing
endfunction



I saw them doing something like this in Blizzard Bomber and Commander not working for me however..
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Yes, but the function has to be declared before it is called.

The best place to declare the function would be in the map's header.

This isn't needed:
JASS:
//===========================================================================
function InitTrig_GiveIncome takes nothing returns nothing
endfunction

How is that done?

  • Init
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: call InitGiveIncome()
      • Custom script: call InitUpdateMultiboardValues()
Also on a sidenote, is there a function for returning item text or values? Like this:
JASS:
call DebugMsg(MultiboardGetItemValue(m,1,1))
(apart from it not working..)
 
Last edited:
Level 25
Joined
Jul 10, 2006
Messages
3,315
No, if the function isn't triggered by any event, it isn't needed.

Somewhere in your header you have
JASS:
function myfunc takes nothing returns nothing
    //do things
endfunction

Then in a trigger later on, you simply
  • Custom script: call myfunc()
That's it, it doesn't need to be initialized.

On the side note: no. Store everything using variables, refer to the variables when you want the values.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
No, if the function isn't triggered by any event, it isn't needed.

Somewhere in your header you have
JASS:
function myfunc takes nothing returns nothing
    //do things
endfunction

Then in a trigger later on, you simply
  • Custom script: call myfunc()
That's it, it doesn't need to be initialized.

On the side note: no. Store everything using variables, refer to the variables when you want the values.
Bless you it works, but if I want to export it from the header to a trigger, don't like massive rows of code. ;> Then I have to use variables and run trigger? NVM, got it, thanks!

Why does this work:
  • player chat Copy Copy
    • Events
      • Player - Player 1 (Red) types a chat message containing test3 as An exact match
    • Conditions
    • Actions
      • Custom script: call MyFunc3()
JASS:
function MyFunc3 takes nothing returns nothing
    call BJDebugMsg("Function 3")
endfunction
//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
endfunction

And this don't:
  • Untitled Trigger 003
    • Events
      • Player - Player 1 (Red) types a chat message containing create boards as An exact match
    • Conditions
    • Actions
      • Custom script: call createMultiboards()
JASS:
function createMultiboards takes nothing returns nothing
  local integer i = 1
  local integer boardTypes = 5
  local integer x = 12
  local player p
  

    loop
      exitwhen i > boardTypes * x 
        set p = Player(0)
          if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER then
            set udg_mBoard[i] = CreateMultiboard()
              if i < 1 + x then
                call BJDebugMsg("Multiboard Created[" + I2S(i) + "]" + "for" + GetPlayerName(p) + "type 1")
              endif
              if i < 1 + 2 * x then
                call BJDebugMsg("Multiboard Created[" + I2S(i) + "]" + "for" + GetPlayerName(p) + "type 2")
              endif
              if i < 1 + 3 * x then
                call BJDebugMsg("Multiboard Created[" + I2S(i) + "]" + "for" + GetPlayerName(p) + "type 3")
              endif
              if i < 1 + 4 * x then
                call BJDebugMsg("Multiboard Created[" + I2S(i) + "]" + "for" + GetPlayerName(p) + "type 4")
              endif
              if i < 1 + 5 * x then
                call BJDebugMsg("Multiboard Created[" + I2S(i) + "]" + "for" + GetPlayerName(p) + "type 5")
              endif
          endif
        set i = i + 1 
    endloop 

endfunction

//===========================================================================
function InitTrig_CreateMultiboard takes nothing returns nothing
endfunction

The only difference are the names... :vw_wtf:
 
Last edited:
Level 25
Joined
Jul 10, 2006
Messages
3,315
but if I want to export it from the header to a trigger, don't like massive rows of code. ;> Then I have to use variables and run trigger?

I'm going to answer this anyway in case this comes up in search;

You can put all that code in a trigger (replacing all default text), but this trigger must be above where you call it in the trigger list.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Make sure the above trigger contains a function called "makeBoard" that takes nothing.

If you use JNGP you can declare JASS functions inside a library which places them above all non-library functions.

The function exists.

JASS:
function makeBoard takes nothing returns nothing
  local force AllPlayers = GetPlayersAll()
    call ForForce(AllPlayers, function PickedPlayers)
  call DestroyForce(AllPlayers)
endfunction      
 
//===========================================================================
function InitTrig_CreateBoard takes nothing returns nothing
endfunction

I just wanted to create free functions inside triggers without using the header. ;>
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
I found the solution: You copy the trigger that is supposed to be last in the map code, remove it and then paste i in. with jngp though, dunno about normal editor.

This also worked fine:

JASS:
function Above takes nothing returns nothing 
    call BJDebugMsg("This is above another function...")
endfunction



function Start takes nothing returns nothing
    call ExecuteFunc("InAnotherTrigger")
    call ExecuteFunc("Above")
    call ExecuteFunc("Below")
    
endfunction

function Below takes nothing returns nothing
    call BJDebugMsg("This is below another function")
endfunction


//===========================================================================
function InitTrig_aaa takes nothing returns nothing
    set gg_trg_aaa = CreateTrigger(  )
    call TriggerAddAction( gg_trg_aaa, function Start )
endfunction
 
Last edited:
Status
Not open for further replies.
Top