Tutorial Local Variable Tutorial

jonadrian619

-___-
Reaction score
240
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..:)

Read it and youll become better at Local variables and Jass too. If you don't like it go somewhere else..

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


1) Introduction
Local variables are variables that are declared in functions. Local variables, unlike globals, don't overwrite themselves and can carry out many things at the same time.

For example, when a unit dies, it will explode, creating a simple special effect. What IF 2 units of that type died at the same time. Of course if you use global variables the other dying unit will not explode, and instead the other one who died later will. Using local variables can solve this problem.

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 script errors. Script 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 script error telling that 'Lightning Effects' does not exist. To solve this problem install the latest patch.


2) Creating your own Local Variables
You need knowledge about JASS triggering before doing this stuff. Local variables are variables that are created using only custom scripts.

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 with extra examples:
Code:
                local unit Nova_Caster
                local group Monster_Unit_Group
                local player Owner_of_Firewall_Caster
                local integer Player_INDEX
                local boolean True_or_False
                local force PlayerGroup_One
                local real Hero_HP
                local trigger Map_Init
                local location Position_of_Character
                local item Short_Sword
                local doodad Destructable_Gate
                local timer REVIVEHERO_TIMER
                local effect Explosion
                local lightning ManaBurn
                local rect PlayableMapArea

Here are hard to understand types and their GUI versions
group is Unit Group in GUI
force is Player Group in GUI
doodad 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 script 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:
set local variable name = value

Examples:
Code:
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.

Code:
local var test
    Events
        Player - Player 1 (Red) types a chat message containing - changecolor as An Exact Match
    Conditions
    Actions
        Custom script:   local player Player_ONE
        Custom script:   set Player_ONE = GetTriggerPlayer()
        Custom script:   call SetPlayerColorBJ( Player_ONE, PLAYER_COLOR_BLUE, true )

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 script errors.


5) Removing Memory LeaksTake 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. One unit and string. Strings also leak unless removed and nullified.

Code:
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:
        Custom script:   set Exploding_Unit = null
        Custom script:   call RemoveUnit( Exploding_Unit )

b) Destroying objects tha 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:
        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)
To remove a specific leaking object, an example is shown below (Point leak):
Code:
        Unit - Create 1 Hydra for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
        Set Temp_Point = (Center of (Playable map area))
        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 except local variables 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 the User Submitted Tutorials. Pick one of them to learn more about JASS.

Please make comments if it's enough for you or you need something more advanced.
 
I

IKilledKEnny

Guest
> Local variables are variables that are declared in functions. Local variables, unlike globals, don't overwrite themselves and can carry out many things at the same time.

Not presicely, simply local variables are created every time a function runs, and are created especially for it. Thus, local variables are just like global variables, simply local variables are creates when a function runs, and they will be used by it only.

> For example, when a unit dies, it will explode, creating a simple special effect. What IF 2 units of that type died at the same time. Of course if you use global variables the other dying unit will not explode, and instead the other one who died later will. Using local variables can solve this problem.

The most common problem is waits used inside triggers which need to be referred to several units, the odds that 2 units will die in the exact same milisecond is very small.

> local variable_type = variable_name

Not precise.

local VariableType variable_name = Expression.

> doodad is Destructible in GUI

As far as I know, it's the other way around, at least that's what JASSCraft says.

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

Will. :p

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

You may store data inside a local in the line it was created, you don't what to set the data to it in a whole new line.

> Nullifying Local Variables after use

Strings, booleans, integers and reals can't be nulled. You may null handles only, and those are the only things that need to be nulled. Also, you should null only after you have destroyed the variable.

> Timer, Special Effect, Lightning Effect, Trigger, Point, Unit Group, Floating Text

You forgot few things as BoolExprs etc.

All in all, well explained, although you could have cleared few things, or state some information earlier.
 

Hero

─║╣ero─
Reaction score
250
> WE no limits

I believe you mean WEU (World Editor Unlimited)?

Nope he is right...there is a WE NO Limits it removes limits on doodads and what not
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
All variable types with extra examples:
Code:
                local unit Nova_Caster
                local group Monster_Unit_Group
                local player Owner_of_Firewall_Caster
                local integer Player_INDEX
                local boolean True_or_False
                local force PlayerGroup_One
                local real Hero_HP
                local trigger Map_Init
                local location Position_of_Character
                local item Short_Sword
                local doodad Destructable_Gate
                local timer REVIVEHERO_TIMER
                local effect Explosion
                local lightning ManaBurn
                local rect PlayableMapArea

This is good, but this part quoted is wrong. Those are not all of the types, there is also:
  • ability
  • attacktype
  • buff
  • boolexpr
  • camerasetup
And much, much more. It would take a while to put all of the locals. Still, this is a good and informative tutorial, +rep
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top