Triggers - Memory Leaks and Custom Scripts

emjlr3

Change can be a good thing
Reaction score
395
~~emjlr3's Memory Leak / Custom Script Guide~~

I have seen this going around a lot lately, people asking for help with memory leaks and custom scripts. This tutorial is to provide knowledge of the basic memory leaks and ways to clean them up using custom scripts, for those of us who are not yet intone enough with JASS to make all our triggers using such. This also should provide us with a quick answer to all those posts about this, so we can just send people in the direction of this thread, and tell them to read. Let us begin:

Let me start by introducing everyone to the not so common terms I will be using:

Memory Leak:
the leakage of handle objects, or in other words, basically whenever you create something or reference something, other than integers the game loses it’s location, thus causing a leaked piece of memory. Now granted these are cleaned at the end of the game, but if enough pile up during play without being removed, it can cause serious lag to the players, ranging from a little here and there, to complete un-playability, neither of which is wanted.

Jass:
the scripting language used for scripting Maps and AI files in Blizzard Entertainment's Warcraft III game. Most people make triggers in GUI format, the premade template triggers that are in the World Editor. However, the actual language of these is that of JASS, which can give you almost free reign over every aspect of the game.

Custom Script:
this is an action in the World Editor Trigger Editor which allows you to type one line of JASS script as opposed to using a template GUI script. This is what we will be using to clean up most of our memory leaks, since Blizzard put in the ability to remove these leaks, but neglected to add them to basic GUI.


Now many different things can cause memory leaks, these include:

Special Effects
Groups
Points
Units
Regions
Forces
Lightning Effects
Floating Text
Countdown Timers
(There may be more which I have forgotten, but these are the ones that need to be dealt with the most, since they are most commonly used.)


Now I will show you examples of each of the most common, with memory leaks, and then after cleaning them up:

Unit Groups


Code:
[COLOR=DarkRed]Unit Group Bad[/COLOR]
    Events
    Conditions
    Actions
        Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
            Loop - Actions
                Unit - Kill (Picked unit)

Now this will leak memory for every unit picked, which, depending on how many times this is ran, can add up heavily.

Code:
[COLOR=DarkOrange]Unit Group Good[/COLOR]
    Events
    Conditions
    Actions
        Set Temp_Group = (Units in (Playable map area))
        Unit Group - Pick every unit in Temp_Group and do (Actions)
            Loop - Actions
                Unit - Kill (Picked unit)
        Custom script:   call DestroyGroup (udg_Temp_Group)

This cleans up all of your leaks. We first set our unit group to a unit group variable, pick all the units in this variable, do our actions, and then destroy the unit group variable, using a Custom Script. Remember that when referencing a variable in JASS, you most use underlines for spaces and put ‘udg’ before the Variable name.


Points

Code:
[COLOR=DarkRed]Point Bad[/COLOR]
    Events
    Conditions
    Actions
        Unit - Create 1 Footman for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees

The unit created at the region leaks. This is not a major leak, but depending on the amount of units created, and how often, it can add up.

Code:
[COLOR=DarkOrange]Point Good[/COLOR]
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Unit - Create 1 Footman for Player 1 (Red) at Temp_Point facing Default building facing degrees
        Custom script:   call RemoveLocation (udg_Temp_Point)

By setting our position that we want the unit to be created at, creating it at the variable, then removing it, we remove all leaks.


Special Effects

Code:
[COLOR=DarkRed]Special Effect Bad[/COLOR]
    Events
    Conditions
    Actions
        Special Effect - Create a special effect at (Center of (Playable map area)) using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl

This introduces a new leak, the special effect leak, and has one we just went over, the point leak. We need to remove both of these.

Code:
[COLOR=DarkOrange]Special Effect Good 1[/COLOR]
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl
        Special Effect - Destroy (Last created special effect)
        Custom script:   call RemoveLocation (udg_Temp_Point)

In the same fashion as before, we remove our leaks. Special effects are one of the only things you can remove through GUI functions, and no custom script is needed. Whenever you create a special effect, you should always destroy it, even if it looks as though it destroys itself, such as in this case with Thunder Clap. Now some special effects, if destroyed directly after being made, never show at all. In that case you have to add a wait in, then destroy the special effect at a later time.

Code:
[COLOR=DarkOrange]Special Effect Good 2[/COLOR]
    Events
    Conditions
    Actions
        For each (Integer A) from 1 to 10, do (Actions)
            Loop - Actions
                Set Temp_Point = (Center of (Playable map area))
                Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Undead\UnholyAura\UnholyAura.mdl
                Set Temp_SFX[(Integer A)] = (Last created special effect)
                Custom script:   call RemoveLocation (udg_Temp_Point)
        Wait 2.00 seconds
        For each (Integer A) from 1 to 10, do (Actions)
            Loop - Actions
                Special Effect - Destroy Temp_SFX[(Integer A)]

This is an example if you wanted to have a wait, and create many special effects at once. This stores each into a special effect variable array, and destroys them all at a later time.

Units

The last major leak is that of units. Many people like to use dummy units to create neat triggered spells and such. If these units are not removed, they can just pile up all around the map, leading to some major lag. We use our example from before:

Code:
[COLOR=DarkRed]Units Bad[/COLOR]
    Events
    Conditions
    Actions
        Unit - Create 1 Dummy Caster for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees

There is a very easy way to remove them after they are no longer needed. Remember to fix your other point leak too.

Code:
[COLOR=DarkOrange]Units Good[/COLOR]
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Unit - Create 1 Dummy Caster for Player 1 (Red) at Temp_Point facing Default building facing degrees
        Custom script:   call RemoveLocation (udg_Temp_Point)
        Unit - Add a 2.00 second Generic expiration timer to (Last created unit)

This adds an expiration timer to the unit, so it is destroyed after 2 seconds. This is usually enough time for it to do what it needs to, but you can change it for your personal needs.

Other Possibilities

That pretty much covers all the major leaks problems people have in their maps. Here are a few more custom script functions you can use to remove other leaks that sometimes occur:

Code:
Custom script: call DestroyForce( udg_Your_Variable ) //Player Group

Code:
Custom script:   call RemoveRect(udg_Your_Variable) //Region

Code:
Custom script: call DestroyLightning( udg_Your_Variable ) //Lightning Effect

Code:
Custom script: call DestroyTextTag( udg_Your_Variable ) //Floating Text

This is one of the only things that can also be easily destroyed in GUI. There ar two way of doing this, each are equally effective.

Code:
Floating Text - Change the lifespan of (Last created floating text) to 5.00 seconds
Floating Text - Destroy (Last created floating text)

The first simply adds a destroy timer to the text tag, and it will be destroyed in 5 seconds, the second is a manual destroy you can do yourself.


Code:
Custom script: call DestroyTimer( udg_Your_Variable ) //Countdown Timer

and lastly, one good Custom Script to use is that when you have a trigger that will only run once, then is useless. In those cases, put this at the end of the trigger. It can greatly reduce lag in your maps.

Code:
Custom script:   call DestroyTrigger( GetTriggeringTrigger() )

Final Trigger


Let us put all our newly acquired knowledge together for one final leak less trigger.

Code:
[COLOR=Sienna]Final Good Trigger[/COLOR]
    Events
        Unit - A unit Starts the effect of an ability
    Conditions
        (Ability being cast) Equal to Animate Dead
    Actions
        Set Temp_Point = (Position of (Casting unit))
        Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl
        Special Effect - Destroy (Last created special effect)
        Set Temp_Group = (Units within 512.00 of Temp_Point matching (((Matching unit) is A structure) Equal to False))
        Custom script:   call RemoveLocation (udg_Temp_Point)
        Unit Group - Pick every unit in Temp_Group and do (Actions)
            Loop - Actions
                Set Temp_Point = (Position of (Picked unit))
                Unit - Create 1 Dummy Caster for (Owner of (Casting unit)) at Temp_Point facing Default building facing degrees
                Unit - Order (Last created unit) to Human Priest - Inner Fire (Picked unit)
                Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                Custom script:   call RemoveLocation (udg_Temp_Point)
        Custom script:   call DestroyGroup (udg_Temp_Group)

You have completed my tutorial. I hope you have learned a thing or two, and happy mapping!! <3 <3

Feel free to visit http://jass.sourceforge.net/index.shtml for a list of all JASS functions known, some JASS tools, and a very good JASS manual.

v 1.03

Possibly More To Come...
 

mixmax2

RedArmyGangsta
Reaction score
31
wow, YOU are the helpful hulk, THANKS man this deserves the tutorial section!


+rep

EDIT- nvm i need to spread it out it seems.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
From 0 to 2 memory leaks tutorials, this is amazing.


Just 1 thing:
Floating Text. You dont need the DestroyTexttag thing, there is alot of function in that is better if you want to time the duration of a texttag. All of the functions is in the GUI to. I think they are called "Set Lifespan Of Text" or something like that. As long as you dont se the texttag to be permanent you dont need to destroy it yourself.



Here is the link to the other tutorial btw:
http://www.thehelper.net/forums/showthread.php?t=27242
 
D

DoctorPepper

Guest
Man, this tutorial is like its made of gold. It is actualy the best and the most usefull thing i ever red about W3. I had 3 triggers that pick all uits on the playable area, check its condition and adding a special to this picked unit every SECOND OF THE GAMETIME. What would it be on BNet if i didnt read this...
 

emjlr3

Change can be a good thing
Reaction score
395
DoctorPepper said:
Man, this tutorial is like its made of gold. It is actualy the best and the most usefull thing i ever red about W3. I had 3 triggers that pick all uits on the playable area, check its condition and adding a special to this picked unit every SECOND OF THE GAMETIME. What would it be on BNet if i didnt read this...

ty for your praise, I'm glad I was able to help
 

XXXconanXXX

Cocktails anyone?
Reaction score
284
A very fine tutorial. It's blunt and that structure is very nice and easy to read.

The only thing I'd have to criticise is what phyrex1an said, so I won't restate it.

Overall, good job!
 

emjlr3

Change can be a good thing
Reaction score
395
XXXconanXXX said:
A very fine tutorial. It's blunt and that structure is very nice and easy to read.

The only thing I'd have to criticise is what phyrex1an said, so I won't restate it.

Overall, good job!

ty, I'll see what I can do
 

emjlr3

Change can be a good thing
Reaction score
395
a few updates here and there, and also added about GUI destroying Floating Texts, ty for the ratings!
 
D

Dino.pl

Guest
Bumping a 5-months old thread?
uareanoob said:
umm what about arrays?
Anyway, this tutorial is on the second place for google search
Code:
memory leaks warcraft
 

emjlr3

Change can be a good thing
Reaction score
395
wow, check me out, number 2 heh

anywho, arrays are cleaned jsut like anything else, just with a [#] at the end
 

emjlr3

Change can be a good thing
Reaction score
395
glad to hear this is still getting some attention
 

emjlr3

Change can be a good thing
Reaction score
395
yes leaks. many many leaks...did u even read this?

ur main prob is position leaks mate
 
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