• Check out the results of the Techtree Contest #19!
  • 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.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

local variable

Status
Not open for further replies.
Level 7
Joined
Jul 4, 2007
Messages
249
I want to make a local variable override the global variables tempUnit2 and tempInt but it doesn't seem to be working. I've googled how it's supposed to be done and I can't tell what I'm doing wrong.

  • Actions
    • Custom script: local unit udg_tempUnit2 = GetTrainedUnit()
    • Custom script: local integer udg_tempInt = GetConvertedPlayerId(GetOwningPlayer(udg_tempUnit2))
  • // Also tried this
    • // Custom script: local unit udg_tempUnit2 or just local udg_tempUnit2
    • // Custom script: local integer udg_tempInt or just local udg_tempInt
  • // Along with
    • // Custom script: set udg_tempUnit2 = GetTrainedUnit()
    • // Custom script: set udg_tempInt = GetConvertedPlayerId(GetOwningPlayer(udg_tempUnit2))
    • -------- Rally --------
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • (Players_Rally_Barracks[tempInt] is hidden) Equal to False
      • Then - Actions
        • Unit - Order tempUnit2 to Stop.
        • Wait 0.00 seconds
        • Set VariableSet tempPosition = (Position of Players_Rally_Barracks[tempInt])
        • Unit - Order tempUnit2 to Attack-Move To tempPosition
        • Custom script: call RemoveLocation( udg_tempPosition )
      • Else - Actions
 
GetConvertedPlayerId() in Jass (aka Custom Script) returns values starting from 0. So if the trained unit belongs to Player 1 (Red) it will return the value 0.
1 = Player 2's id, 2 = Player 3's id, etc...

Edit: I was thinking of GetPlayerId(), Converted will start at 1.

Also, you can't reference Shadowed globals in a Conditions block. This is the major issue with them (nothing can ever be so simple when it comes to wc3).

A solution is to use another variable, this one not Shadowed, and set it to be equal to your Shadowed value. Like:
  • Set Variable tempIntNew = tempInt
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
    • (Players_Rally_Barracks[tempIntNew] is hidden) Equal to False
This new variable won't be Shadowed and is safe to reference inside the Conditions of your If Then Else.

You could also just do this since there's no Wait at this stage in the trigger:
  • (Players_Rally_Barracks[Player number of (Owner of Trained unit)] is hidden) Equal to False
Don't forget to null udg_tempUnit2 at the very end of the trigger to avoid a Unit leak. Do this after the If Then Else, since it needs to happen regardless.
  • Custom script: set udg_tempUnit2 = null
 
Last edited:
GetConvertedPlayerId() in Jass (aka Custom Script) returns values starting from 0. So if the trained unit belongs to Player 1 (Red) it will return the value 0.
1 = Player 2's id, 2 = Player 3's id, etc...

Also, you can't reference Shadowed globals in a Conditions block. This is the major issue with them (nothing can ever be so simple when it comes to wc3).

A solution is to use another variable, this one not Shadowed, and set it to be equal to your Shadowed value. Like:
  • Set Variable tempIntNew = tempInt
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
    • (Players_Rally_Barracks[tempIntNew] is hidden) Equal to False
This new variable won't be Shadowed and is safe to reference inside the Conditions of your If Then Else.

You could also just do this since there's no Wait at this stage in the trigger:
  • (Players_Rally_Barracks[Player number of (Owner of Trained unit)] is hidden) Equal to False
Don't forget to null udg_tempUnit2 at the very end of the trigger to avoid a Unit leak. Do this after the If Then Else, since it needs to happen regardless.
  • Custom script: set udg_tempUnit2 = null
I see, so the conditions are in a separate function. How odd.

Do the unit variable leak because it's a local variable? I've never heard about unit leaks before
 
GetConvertedPlayerId() in Jass (aka Custom Script) returns values starting from 0. So if the trained unit belongs to Player 1 (Red) it will return the value 0.
1 = Player 2's id, 2 = Player 3's id, etc...
That's not true GetConvertedPlayerId() returns values starting from 1, using this function with with the player 1 returns 1, the function that starts from 0 is GetPlayerId()
Do the unit variable leak because it's a local variable? I've never heard about unit leaks before
Yes, you should look the links that @Uncle has below him.
 
Shouldn't this keep the local variable through the if statement?
  • recruited
    • Events
      • Unit - A unit Finishes training a unit
    • Conditions
    • Actions
      • Custom script: local unit udg_tempUnit2 = GetTrainedUnit()
      • Custom script: local integer udg_tempInt = GetConvertedPlayerId(GetOwningPlayer(udg_tempUnit2))
      • -------- Rally --------
      • Custom script: if ( IsUnitHiddenBJ(udg_Players_Rally_Barracks[udg_tempInt]) == false ) then
      • Unit - Order tempUnit2 to Stop.
      • Wait 0.00 seconds
      • Unit - Order tempUnit2 to Attack-Move To Players_Rally_Barracks_pos[tempInt]
      • Custom script: endif
      • Custom script: set udg_tempUnit2 = null
Because it doesn't
 
No, it's still a Shadowed variable which doesn't work in Conditions. But at this point you're practically writing in Jass so you might as well forget the Shadowing technique and just do the whole trigger in Custom Script.

  • Actions
    • Custom script: local unit u = GetTrainedUnit()
    • Custom script: local integer id = GetConvertedPlayerId(GetOwningPlayer(u))
    • Custom script: if ( IsUnitHidden(udg_Players_Rally_Barracks[id]) == false ) then
    • Custom script: call IssueImmediateOrder(u, "stop")
    • Wait 0.00 seconds
    • Custom script: call IssuePointOrderLoc(u, "attack", udg_Players_Rally_Barracks_pos[id])
    • Custom script: endif
    • Custom script: set u = null
I believe "attack" is the same thing as Attack-Move.

Also, my mistake on the Id thing, I mix them up sometimes.
 
Last edited:
It turns out that the last trigger I posted works with the variables, there was just an error with the position variable which is irrelevant to this matter. Thanks for your replies guys, much appreciated.
 
Status
Not open for further replies.
Back
Top