[JASS] Some questions

Level 9
Jul 4, 2007
Hi there,

1) does that leak:
set tempu = RandomUnit(...) 
set newu = AnotherRandomUnit(...)
set tempu = newu // in fact here i dont know if i should null tempu before i assign another value to it.
set newu = null
set tempu = null

2) How to do multiple conditions in a "if" with booleans, since And() only works for boolexpr.


Code Reviewer
Level 26
Mar 19, 2008
You should have nulled that varuable before setting it to new unit.

You manipulate boolean conditions using keywords: 'not', ==, !=, 'or', 'and'.
    local unit u = GetFilterUnit()
    local boolean b = (not (GetWidgetLife(u) < 0.405)) and IsUnitEnemy(u, GetOwningPlayer(LEADER)) and IsUnitType(u, UNIT_TYPE_STRUCTURE) == false
If you want to group conditions, for egzample one has to be always true to conditinue the function but second requires only one from group to actually return true - make sure you dont forget about '(' and ')'.
    local unit u = GetFilterUnit()
    local boolean b = (not (GetWidgetLife(u) < 0.405)) and (IsUnitEnemy(u, GetOwningPlayer(LEADER)) or IsUnitType(u, UNIT_TYPE_STRUCTURE) == false)
Level 27
Jan 26, 2007
You should have nulled that varuable before setting it to new unit.
Hm, I don't think you have to do that. I may be incorrect though, but this is why I think you don't have to:
You null locals because otherwise the value they hold won't be gone (variables that hold a value take up memory). If you assign it to a different value, then it won't remember the first piece of memory (and thus assigning the variable to a different value will 'clear' the variable first).
Because it automatically clears the variable when setting (okay, okay, it doesn't literally 'automatically clear' it, it's just logical that it does), there's no need to worry.

local unit u = unit1

set u = unit2 // The value of "unit1" is NOT stored in "u" anymore.

set u = null // NOW we need to null, because "u" still refers to "unit2".
With actual memory leaks, this is obviously different.
But with nulling: ONLY null them at the end of the trigger (and not all variables need to be nulled).

Different conditions: Spinnaker is correct.
if condition1 and condition2 then
    // Actions

Additionally, you don't need to write " == true", nor " == false" for booleans.

This works:
local boolean bool1 = true
local boolean bool2 = false

if bool1 and not bool2 then
    // Returns true
(Just saying...).

Dr Super Good

Spell Reviewer
Level 62
Jan 18, 2005
You only need to null local handles at the end of a function. This is because WC3 has a bug with defined locals (not argument locals) so it does not decriment the handle reference counter when they are terminated at the end of a function. Further more you only ever need to null handles which need to have their handleID recycled.

function LEAKLESS takes unit u, unit k returns unit
local player p = Player(0) //Players never get deallocated thus no need to null this
local unit u2 = GetTriggerUnit() //Units do get deallocated thus this must be nulled
local boolean b = false //This is a boolean, nulling it would be retarded
local string s = "" //This is a string, as they always leak for each unique one there is no need to null them.
set u = u2 //so we can return something
set u2 = null //Otherwise the handle index counter would leak
return u