• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Some questions

Status
Not open for further replies.
Level 9
Joined
Jul 4, 2007
Messages
130
Hi there,

1) does that leak:
JASS:
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.
 

Bannar

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

You manipulate boolean conditions using keywords: 'not', ==, !=, 'or', 'and'.
Egzample:
JASS:
    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 ')'.
JASS:
    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 28
Joined
Jan 26, 2007
Messages
4,789
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.

In JASS:
JASS:
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.
JASS:
if condition1 and condition2 then
    // Actions
endif

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

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

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

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,180
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.

JASS:
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
endfunction
 
Status
Not open for further replies.
Top