• 🏆 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!

local variables

Status
Not open for further replies.
Level 17
Joined
Mar 21, 2011
Messages
1,597
Hi,
if i create a trigger with locals on the top of the function and with a EVENT_PLAYER_UNIT_DEATH event for example, how would i initialize the locals?

JASS:
local unit u = GetTriggerUnit()
local real x = GetUnitX(u)
...
if IsUnitType(....)

endif

set u = null

return false



OR


JASS:
local unit u
local real x
...
if IsUnitType(....)

set u = GetTriggerUnit()
set x = GetUnitX(u)
...

endif

set u = null

return false

wouldnt the second solution be better? because i only need to set it up if it is a certain unit type.


JASS:
function Trig_Bear_Trap_Conditions takes nothing returns boolean
    
    local unit u = GetTriggerUnit()
    local real x = GetUnitX(u)
    local real y = GetUnitY(u)
    local player p = GetTriggerPlayer()
    local unit d
    local item it
    local group g = CreateGroup()
    local unit l
    
    if GetUnitTypeId(u) == 'n00A'then
        set d = CreateUnit(p, 'h00Q', x, y, 0.00)
        set it = CreateItem('I007', x, y)
        call UnitAddItem(d, it)
        call UnitUseItem(d, it)
        call UnitApplyTimedLife(d, 'BTLF', 1.00)
        call GroupEnumUnitsInRange(g, x, y, 140.00, null)
        loop
            set l = FirstOfGroup(g)
            exitwhen l == null
            if (GetUnitState(l, UNIT_STATE_LIFE) > 0) and (IsUnitEnemy(l, p)) and (IsUnitType(l, UNIT_TYPE_UNDEAD) == false) then
                call IssueTargetOrder(d, "purge", l)
                call IssueTargetOrder(d, "faeriefire", l)
                call UnitDamageTarget(u, l, 100.00, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS)
            endif
            call GroupRemoveUnit(g, l)
        endloop
    endif
    
    call DestroyGroup(g)
    set u = null
    set p = null
    set d = null
    set it = null
    set g = null
    set l = null
    
    return false
endfunction

//===========================================================================
function InitTrig_Bear_Trap takes nothing returns nothing

    local integer index
    set index = 0
    set gg_trg_Bear_Trap = CreateTrigger(  )
    loop
        call TriggerRegisterPlayerUnitEvent(gg_trg_Bear_Trap, Player(index), EVENT_PLAYER_UNIT_DEATH, null)
        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
    call TriggerAddCondition( gg_trg_Bear_Trap, Condition( function Trig_Bear_Trap_Conditions ) )

endfunction
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
The second is indeed more efficient, but you can use any and all as long as you null agents and destroy instances.

In your case (probably), this would be the how I write it:
JASS:
local unit u = GetTriggerUnit()
local real x

if IsUnitType(u, ...)
     set x = GetUnitX(u)
endif

set u = null

return false
(Usefull code btw.)

In your Trig_Bear_Trap_Conditions function, you will not use x, y, p and g if you dont have a unit of that unit type.
So you should (especially in agent cases) not create/initialize them before you really need them.

The same in reverse, if your unit is not of that specific type, you will not have to null the player, the item, unit d, unit l or the group.
So you should null those inside the if/then block.
Also, unit l will never be null at the end of the script because you only exit the loop when l is null... and you dont use it anywhere else.

I may be wrong but a player might not be an agent... dunno if you have to null it.
 
Status
Not open for further replies.
Top