1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. We have recently started the 16th edition of the Mini Mapping Contest. The theme is mini RPG. Do check it out and have fun.
    Dismiss Notice
  4. Dismiss Notice
  5. The Highway to Hell has been laid open. Come along and participate in the 5th Special Effect Contest.
    Dismiss Notice
  6. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Local Variables

Discussion in 'JASS/AI Scripts Tutorials' started by jonadrian619, May 19, 2007.

  1. jonadrian619

    jonadrian619

    Joined:
    Oct 24, 2006
    Messages:
    121
    Resources:
    4
    Maps:
    1
    Tutorials:
    3
    Resources:
    4
    jonadrian619's step by step Tutorial about Local Variables.

    This is my second tutorial about JASS. This tutorial is for newbies who want to learn about local variables and even some topics about JASS..

    Table of Contents:
    1) Introduction
    2) Creating Local Variables
    3) Storing data in a Local Variable
    4) Using stored data
    5) Removing Memory Leaks
    6) Conclusion

    To know more about JASS click here.

    1) Introduction
    Local variables are variables that are declared in functions. Functions are parts of the script which execute a number of actions... (click 'here' above for more details) Local variables, unlike globals, allow several copies of the trigger to fire correctly.

    The only bad thing about them is that they can ONLY be used inside the function they were declared in.. The example below is wrong:

    Code (vJASS):
    function CreateEffect takes nothing returns nothing
        local unit u
        set u = GetTriggerUnit()
    endfunction

    function RemoveUnit takes nothing returns nothing
        call RemoveUnit(u, true)
    endfunction


    Local variables also play an important role for triggered spells, most especially the spells that need dummy units to take effect.

    This paragraph tells you about syntax errors. Syntax Errors are lines of script code or custom scripts that contain variables, functions or any word in the script that is incorrect, invalid or don't exist at all. You should be more careful when using WE- No Limits tool in TFT versions 1.07 or any version below 1.18, as the new GUI actions in WE no-limits will cause script errors. In this case, creating a 'Lightning Effect' action in WE no limits and you have TFT v1.07 will cause a syntax error telling that 'Lightning Effects' does not exist. To solve this problem install the latest patch.


    2) Creating Local Variables
    You need knowledge about JASS triggering before doing this stuff. Local variables can only be declared by using the Custom Script action.

    To start, go to the Trigger Editor, then Create a new trigger or use an existing trigger. Create all the necessary events and conditions. Once finished, create a Custom Script action.

    Global variables can be created in the Variable Editor, setting it's types and initial value and it's name. Local variables are declared in this manner:

    local variable_type = variable_name

    All variable types:
    Code (vJASS):
    type event              extends     handle  // a reference to an event registration
    type player             extends     handle  // a single player reference
    type widget             extends     handle  // an interactive game object with life
    type unit               extends     widget  // a single unit reference
    type destructable       extends     widget
    type item               extends     widget
    type ability            extends     handle
    type buff               extends     ability
    type force              extends     handle
    type group              extends     handle
    type trigger            extends     handle
    type triggercondition   extends     handle
    type triggeraction      extends     handle
    type timer              extends     handle
    type location           extends     handle
    type region             extends     handle
    type rect               extends     handle
    type boolexpr           extends     handle
    type sound              extends     handle
    type conditionfunc      extends     boolexpr
    type filterfunc         extends     boolexpr
    type unitpool           extends     handle
    type itempool           extends     handle
    type race               extends     handle
    type alliancetype       extends     handle
    type racepreference     extends     handle
    type gamestate          extends     handle
    type igamestate         extends     gamestate
    type fgamestate         extends     gamestate
    type playerstate        extends     handle
    type playerscore        extends     handle
    type playergameresult   extends     handle
    type unitstate          extends     handle
    type aidifficulty       extends     handle
    type eventid            extends     handle
    type gameevent          extends     eventid
    type playerevent        extends     eventid
    type playerunitevent    extends     eventid
    type unitevent          extends     eventid
    type limitop            extends     eventid
    type widgetevent        extends     eventid
    type dialogevent        extends     eventid
    type unittype           extends     handle
    type gamespeed          extends     handle
    type gamedifficulty     extends     handle
    type gametype           extends     handle
    type mapflag            extends     handle
    type mapvisibility      extends     handle
    type mapsetting         extends     handle
    type mapdensity         extends     handle
    type mapcontrol         extends     handle
    type playerslotstate    extends     handle
    type volumegroup        extends     handle
    type camerafield        extends     handle
    type camerasetup        extends     handle
    type playercolor        extends     handle
    type placement          extends     handle
    type startlocprio       extends     handle
    type raritycontrol      extends     handle
    type blendmode          extends     handle
    type texmapflags        extends     handle
    type effect             extends     handle
    type effecttype         extends     handle
    type weathereffect      extends     handle
    type terraindeformation extends     handle
    type fogstate           extends     handle
    type fogmodifier        extends     handle
    type dialog             extends     handle
    type button             extends     handle
    type quest              extends     handle
    type questitem          extends     handle
    type defeatcondition    extends     handle
    type timerdialog        extends     handle
    type leaderboard        extends     handle
    type multiboard         extends     handle
    type multiboarditem     extends     handle
    type trackable          extends     handle
    type gamecache          extends     handle
    type version            extends     handle
    type itemtype           extends     handle
    type texttag            extends     handle
    type attacktype         extends     handle
    type damagetype         extends     handle
    type weapontype         extends     handle
    type soundtype          extends     handle
    type lightning          extends     handle
    type pathingtype        extends     handle
    type image              extends     handle
    type ubersplat          extends     handle


    Here are hard to understand types and their GUI versions
    group is Unit Group in GUI
    force is Player Group in GUI
    destructable is Destructible in GUI
    effect is Special Effect in GUI
    location is Point in GUI
    rect is Region in GUI

    Local variables must be declared at the beginning of the function before any actions are executed, as it could cause sytnax errors.

    3) Storing Data inside a Local Variable
    Storing data in a local variable is similar to storing data in a global variable. The 'Set Variable' action stores data in a global variable. However, storing data in a local variable should be done in custom script:

    Code (vJASS):
    set local variable name = value


    Examples: set Nova_Caster = GetTriggerUnit() - [In GUI: Set Nova_Caster = (Triggering Unit)]

    You must store data only to previously declared local variables. Also, store only data that the local variable can store. For example, you cannot store a unit into an integer local variable.


    4) Using Stored Data
    Once you've stored data inside a local variable, you can create any action to use the stored data. The trigger below will give an example.

    • local var test
      • Events
        • Player - Player 1 (Red) types a chat message containing - test as An Exact Match
      • Conditions
      • Actions
        • Custom script: local player Player_ONE
        • Custom script: set Player_ONE = GetTriggerPlayer()


    When using a local variable's data make sure you use JASS or Custom Script to create the actions. GUI actions only use the data stored in a global variable. Take note that you'll create only actions that use the type of data stored in a local variable. Creating a unit using the data stored in an integer variable will cause syntax errors.


    5) Removing Memory Leaks
    Take note that variables leak. They will not be removed from the memory and it'll remain there. That's the main cause of in-game lag. If you've got a bunch of triggers, triggers that leak, it could cause lag and even game freezing (worst lag) when not removed for a long time. Below are ways of nullifying local variables and removing leaks.

    a) Nullifying Local Variables after use
    Nullifying local variables gets them out of the memory by emptying it. By setting a 'null' value to a local variable it means that it's empty. Below is an example of nullifying 2 local variables. Take note, that handles leak and must be nulled.

    • Nullify Test
      • Events
        • Unit - A unit Dies
      • Conditions
      • Actions
        • Custom script: local unit Exploding_Unit
        • Custom script: local string Test
        • Custom script: set Exploding_Unit = GetTriggerUnit()
        • Custom script: set Test = "The nullify test worked!!"
        • Custom script: call TriggerSleepAction( 1.00 )
        • Custom script: call RemoveUnit( Exploding_Unit )
        • Custom script: set Exploding_Unit = null
        • Custom script: call DisplayTimedTextToForce( GetPlayersAll(), 5.00, Test )
        • Custom script: call TriggerSleepAction( 6.00 )
        • Custom script: set Test = null


    The unit variable was declared, then when the 'GetTriggerUnit' was stored, it received a null value through the 'set' action. To nullify the string variable, let the string show off a bit, maybe 5 seconds, then add a 6 second wait action after that. Then nullify the variable.
    The trigger above showed an example that the contents of the local variables must be destroyed first then nullified.
    You must never nullify local variables BEFORE you destroyed what they contained, or else you'll never be able to remove that. An example of that error is like this:

    Code (vJASS):
    Custom script:   set Exploding_Unit = null

    Code (vJASS):
    Custom script:   call RemoveUnit( Exploding_Unit )


    NOTE: You must pause timers and destroy their windows before you nullify them.

    b) Destroying objects that cause memory leaks
    This part is not related to this tutorial. However it gives newbies some tips to destroy leaks.

    Points, Unit Groups, Player Groups, Special Effects, Lightning Effects, Floating Text, and Countdown Timers also leak.

    Here are the actions needed to remove each of them. In order: Timer, Special Effect, Lightning Effect, Trigger, Point, Unit Group, Floating Text

    Code (vJASS):
    Custom script:    call DestroyTimer (name)
            Custom script:    call DestroyEffect (name)
            Custom script:    call DestroyLightning (name)
            Custom script:    call DestroyTrigger (name)
            Custom script:    call RemoveLocation (name)
            Custom script:    call DestroyGroup (name)
            Custom script:    call DestroyTextTag (name)


    Here are additional lines to destroy handles. (Thanks to Diablo-DK)

    Code (vJASS):
    call DestroyBoolExpr()
    call DestroyCondition()
    call DestroyDefeatCondition()
    call DestroyFilter()
    call DestroyFogModifier()
    call DestroyForce()
    call DestroyImage()
    call DestroyItemPool()
    call DestroyLeaderboard()
    call DestroyMultiboard()
    call DestroyQuest()
    call DestroyTimerDialog()
    call DestroyTrigger()
    call DestroyUbersplat()
    call DestroyUnitPool()
    call RemoveDestructable()
    call RemoveItem()
    call RemoveLocation()
    call RemoveRect()
    call RemoveRegion()
    call RemoveUnit()
    call RemoveWeatherEffect()
    call TriggerRemoveAction()
    call TriggerRemoveCondition()


    To remove a specific leaking object, an example is shown below (Point leak):
    • Set Temp_Point = (Center of (Playable map area))
      • Unit - Create 1 Hydra for Player 1 (Red) at Temp_Point facing Default building facing degrees
      • Custom script: call RemoveLocation ( udg_Temp_Point )


    6) Conclusion
    That's all I can show you because I'm only good at GUI triggering and know few things about JASS like local variables, native functions and leaks. This is a newbie's tutorial to understanding local variables and the ways of destroying memory leaks. Learn the contents of this tutorial. There are a group of JASS tutorials in this site. Pick one of them to learn more about JASS.

    Please make comments if it's enough for you or you need something more advanced.
     
    Last edited by a moderator: Jun 27, 2007
  2. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    First of all use jass tags when you write jass text:
    Code (vJASS):

    local unit u
     

    this is done by wrapping [.jass] and [./jass] around your jass text(without the " . ")

    That is not quite correct, only handles leak and have to be nulled.
    All variable types are handle except these:
    Code (vJASS):

    real, boolean, integer, code, string
     

    The above variable types does not have to be nulled.
    Strings does leak yes, a little but they still can't be nulled.

    Is this supposed to leak?

    Also, here's a longer list of functions to destroy handles
    Code (vJASS):

    DestroyBoolExpr()
    DestroyCondition()
    DestroyDefeatCondition()
    DestroyEffect()
    DestroyFilter()
    DestroyFogModifier()
    DestroyForce()
    DestroyGroup()
    DestroyImage()
    DestroyItemPool()
    DestroyLeaderboard()
    DestroyLightning()
    DestroyMultiboard()
    DestroyQuest()
    DestroyTextTag()
    DestroyTimer()
    DestroyTimerDialog()
    DestroyTrigger()
    DestroyUbersplat()
    DestroyUnitPool()
    RemoveDestructable()
    RemoveItem()
    RemoveLocation()
    RemoveRect()
    RemoveRegion()
    RemoveUnit()
    RemoveWeatherEffect()
    TriggerRemoveAction()
    TriggerRemoveCondition()
     


    Also include that timers should be paused before destroyed to avoid bugs
    like this:
    Code (vJASS):

    call PauseTimer(timer)
    call DestroyTimer(timer)
     

    and maybe also that nulling timers can bug.
     
  3. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    I've personally never had troubles with nulling timers.

    As to otherwise --

    -It's bad practice to put Underscores ("Spaces") in Local Variable names
    -You usually, by programming traditions, start variables (non-constant) with lowercaseletters, constants with UPPERCASELETTERSALLTHEWAYTHROUGH, and functions with CapitalizedWords
    -Strings can be nulled but it doesn't stop their leak (sorta like what Diablo-DK said)
    -Your 'hydra' leaks.
    -set is lowercase, not uppercase
    -BJ functions are bad practice
    -Destructable is destructable in JASS, not doodad
    -You may want to describe what a function is to these 'newbies' before you refer to them
    -Local variables are overwritten... theyre just not accessible from outside an instance of a function
    -When you comment on the '2 units dying at the same time', that's not possible for warcraft -- triggers without waits don't run at the same nanosecond
    -Please unattach your signature from the tutorial
    -You can initialize local variables, eg
    Code (vJASS):
    local player p1 = GetTriggerPlayer()

    -Script Errors, as you call them, are called Syntax Errors

    and diablo, only Periodic timers need pausing
     
  4. jonadrian619

    jonadrian619

    Joined:
    Oct 24, 2006
    Messages:
    121
    Resources:
    4
    Maps:
    1
    Tutorials:
    3
    Resources:
    4
    All problems fixed. It's just because this site has no buttons on how to make tags, unlike TH.net or wc3campaigns, in this site you have to type [] tags manually. I just got used to it...

    Thanks for looking out for the errors...
     
  5. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    You can wrap tags around text with buttons. You just have to click Go Advanced.
    Btw, why do you use code tags and quote tags instead of jass tags and trigger tags?
     
  6. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    I remember replying to a thread like this... Anyways, those are not all of the local variables. These are all of the local variables:
    Code (vJASS):
    //============================================================================
    // Native types. All native functions take extended handle types when
    // possible to help prevent passing bad values to native functions
    //
    type event              extends     handle  // a reference to an event registration
    type player             extends     handle  // a single player reference
    type widget             extends     handle  // an interactive game object with life
    type unit               extends     widget  // a single unit reference
    type destructable       extends     widget
    type item               extends     widget
    type ability            extends     handle
    type buff               extends     ability
    type force              extends     handle
    type group              extends     handle
    type trigger            extends     handle
    type triggercondition   extends     handle
    type triggeraction      extends     handle
    type timer              extends     handle
    type location           extends     handle
    type region             extends     handle
    type rect               extends     handle
    type boolexpr           extends     handle
    type sound              extends     handle
    type conditionfunc      extends     boolexpr
    type filterfunc         extends     boolexpr
    type unitpool           extends     handle
    type itempool           extends     handle
    type race               extends     handle
    type alliancetype       extends     handle
    type racepreference     extends     handle
    type gamestate          extends     handle
    type igamestate         extends     gamestate
    type fgamestate         extends     gamestate
    type playerstate        extends     handle
    type playerscore        extends     handle
    type playergameresult   extends     handle
    type unitstate          extends     handle
    type aidifficulty       extends     handle

    type eventid            extends     handle
    type gameevent          extends     eventid
    type playerevent        extends     eventid
    type playerunitevent    extends     eventid
    type unitevent          extends     eventid
    type limitop            extends     eventid
    type widgetevent        extends     eventid
    type dialogevent        extends     eventid
    type unittype           extends     handle

    type gamespeed          extends     handle
    type gamedifficulty     extends     handle
    type gametype           extends     handle
    type mapflag            extends     handle
    type mapvisibility      extends     handle
    type mapsetting         extends     handle
    type mapdensity         extends     handle
    type mapcontrol         extends     handle
    type playerslotstate    extends     handle
    type volumegroup        extends     handle
    type camerafield        extends     handle
    type camerasetup        extends     handle
    type playercolor        extends     handle
    type placement          extends     handle
    type startlocprio       extends     handle
    type raritycontrol      extends     handle
    type blendmode          extends     handle
    type texmapflags        extends     handle
    type effect             extends     handle
    type effecttype         extends     handle
    type weathereffect      extends     handle
    type terraindeformation extends     handle
    type fogstate           extends     handle
    type fogmodifier        extends     handle
    type dialog             extends     handle
    type button             extends     handle
    type quest              extends     handle
    type questitem          extends     handle
    type defeatcondition    extends     handle
    type timerdialog        extends     handle
    type leaderboard        extends     handle
    type multiboard         extends     handle
    type multiboarditem     extends     handle
    type trackable          extends     handle
    type gamecache          extends     handle
    type version            extends     handle
    type itemtype           extends     handle
    type texttag            extends     handle
    type attacktype         extends     handle
    type damagetype         extends     handle
    type weapontype         extends     handle
    type soundtype          extends     handle
    type lightning          extends     handle
    type pathingtype        extends     handle
    type image              extends     handle
    type ubersplat          extends     handle
     
  7. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,161
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    They're variable types in general, not just locals ><
     
  8. Wolverabid

    Wolverabid

    Joined:
    Oct 23, 2006
    Messages:
    8,302
    Resources:
    5
    Tutorials:
    5
    Resources:
    5
    Tutorial Approved

    ~ Thread moved to JASS/AI Scripts Tutorials.