• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Are function params also pointers?

Status
Not open for further replies.
JASS:
function set_unit takes unit u returns nothing
    call SetUnitUserData(u,1)
endfunction
Should unit u die, will its handle index be recycled or does the function parameter still point to it?

Also, not so much relevant, but I don't want to do a double-post - can I set a parameter within a function like this?:

JASS:
function new_trig takes trigger t returns trigger
    set t = CreateTrigger( )
    return t
endfunction
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Well it is a pointer but the memory references are recycled I believe.

Bribe said:
Should unit u die, will its handle index be recycled or does the function parameter still point to it?

The actual handle reference of the unit is not recycled until the unit has been removed from the game, at which point any variable references to that unit would become null.

Bribe said:
Also, not so much relevant, but I don't want to do a double-post - can I set a parameter within a function like this?:

Yes.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Well no it isn't set to null automatically, but I think that is what it will return anyway. The value still must be set to null in order for the handle reference to be recycled.
 
Well no it isn't set to null automatically, but I think that is what it will return anyway. The value still must be set to null in order for the handle reference to be recycled.

^This is what I believe as well.

It really just depends on whether or not it is permanent. If it is permanent, you shouldn't have to worry about nulling the pointers. (although, in essentially all cases [except for possible random extremely rare retarded cases] it shouldn't matter if you do null it when you don't need to)

That's why sometimes it isn't needed to null the pointers, since its id never needs to be recycled. (TimerUtils -> no need to null timers, they are recycled, Groups -> recycled as well, just a global, triggers -> don't need to, except for dynamic triggers, and those sometimes have recycling methods as well, players -> never destroyed, no need to null)

Anyway, setting it to null might result in a minimal speed loss, but it doesn't really take up as much as you might think. According to my benchmarks, setting a variable to null averages at about 16% faster than DoNothing(). =)
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Parameters do not need to be set to null. The reason for that being the memory necessary for function parameters is probably allocated already, and constantly recycled. I'm not entirely sure there may be a more correct answer than this.
 
I think the outcome of what this means is that it would be more efficient to utilize:

JASS:
function foo takes unit u,unit v,unit w,unit x returns nothing
    // effects
endfunction

Than to try:

JASS:
function bar takes nothing returns nothing
    local unit u
    local unit v
    local unit w
    local unit x
    // effects
    set u = null
    set v = null
    set w = null
    set x = null
endfunction

Because that way you don't have to null so many variables.
 
Status
Not open for further replies.
Top