Optimize Jass Trigger

bastanien

New Member
Reaction score
0
Hello
any idea of how i can make this better/faster/more awesome?
and does it leak ?

JASS:
function Trig_InitTrig_Move_Conditions takes nothing returns boolean
    if ( not ( CountUnitsInGroup(udg_SpellGroup) > 0 ) ) then
        return false
    endif
    return true
endfunction

function PickedUnits takes nothing returns nothing

    local real posX 
	local real posY 
    local location pos
	local location pos2

    
	set udg_UnitValue = GetUnitUserData(GetEnumUnit())         // set custom value of picked unit for MUI 

	if(udg_Distance[udg_UnitValue] > 0) then // when distance is less than 0 remove projectile
	
	set udg_Distance[udg_UnitValue] = udg_Distance[udg_UnitValue]-4 // distance the projectile travels
	
	set pos = GetUnitLoc(udg_SpellUnit[udg_UnitValue]) // position of spellunit
	
	set pos2 = PolarProjectionBJ(pos,25,udg_TargetAngle[udg_UnitValue])       /// offset spellunit to move it
		
	call SetUnitX(udg_SpellUnit[udg_UnitValue],GetLocationX(pos2))         // move the unit X
	call SetUnitY(udg_SpellUnit[udg_UnitValue],GetLocationY(pos2))         // move the unit Y
	
	call RemoveLocation(pos)
	call RemoveLocation(pos2)
	else
	
	call KillUnit(udg_SpellUnit[udg_UnitValue])
	call RemoveUnit(udg_SpellUnit[udg_UnitValue])
	
	endif
	
	
	endfunction

function Trig_Move_Actions takes nothing returns nothing

    call ForGroupBJ( udg_SpellGroup, function PickedUnits )

endfunction

//===========================================================================
function InitTrig_Move takes nothing returns nothing
    set gg_trg_Move = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Move, 0.035 )
    call TriggerAddCondition( gg_trg_Move, Condition( function Trig_InitTrig_Move_Conditions ) )
	call TriggerAddAction( gg_trg_Move, function Trig_Move_Actions )
  endfunction
 
Reaction score
341
Learn to indent. Port it to vJASS. Remove BJ's. Don't use locations.

And yes it leaked.

JASS:
function Trig_InitTrig_Move_Conditions takes nothing returns boolean
    return CountUnitsInGroup(udg_SpellGroup) > 0
endfunction

function PickedUnits takes nothing returns nothing
    local real posX 
    local real posY 
    local real x
    local real y = GetLocationY(source) + dist * Sin(angle * bj_DEGTORAD)
    
    set udg_UnitValue = GetUnitUserData(GetEnumUnit())
    if(udg_Distance[udg_UnitValue] > 0) then
        set udg_Distance[udg_UnitValue] = udg_Distance[udg_UnitValue]-4
        set x = GetUnitX(udg_SpellUnit[udg_UnitValue]) + 25 * Cos(udg_TargetAngle[udg_UnitValue] * bj_DEGTORAD)
        set y = GetUnitY(udg_SpellUnit[udg_UnitValue]) + 25 * Sin(udg_TargetAngle[udg_UnitValue] * bj_DEGTORAD)
        call SetUnitX(udg_SpellUnit[udg_UnitValue], x)
        call SetUnitY(udg_SpellUnit[udg_UnitValue], y)
        call RemoveLocation(pos)
        call RemoveLocation(pos2)
    else
        call KillUnit(udg_SpellUnit[udg_UnitValue])
        call RemoveUnit(udg_SpellUnit[udg_UnitValue])
    endif
endfunction

function Trig_Move_Actions takes nothing returns nothing
    call ForGroup(udg_SpellGroup, function PickedUnits)
endfunction

//===========================================================================
function InitTrig_Move takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEvent(t, 0.035, true)
    call TriggerAddCondition(t, Condition(function Trig_InitTrig_Move_Conditions))
    call TriggerAddAction(t, function Trig_Move_Actions)
  endfunction
 

Tyrulan

Ultra Cool Member
Reaction score
37
On a similar note, what's better. GetLastCreatedItem() or using bj_lastcreated..etc.
 

WolfieeifloW

WEHZ Helper
Reaction score
372
"bj_..." only works if you use a BJ function to create the item.

If you use the native you'd have to do "set item = CreateItem..."
Then use "item" to reference the created item.
 

Tyrulan

Ultra Cool Member
Reaction score
37
Then a line to null the item variable? You can't destroy it, that would destroy the item.

JASS:
   
local item lastItem
set lastItem = CreateItem(itemId, this.casterX, this.casterY)  
call UnitAddItem( this.caster, lastItem)                       
set lastItem = null
 

Tyrulan

Ultra Cool Member
Reaction score
37
So which is preferable? Using bj_last.. or local item?? Does it even matter?
 

Azlier

Old World Ghost
Reaction score
461
bj_last... is faster, but messier. Use it if you want to, it can't hurt anything. Unless you use waits and this variable elsewhere.
 

Tyrulan

Ultra Cool Member
Reaction score
37
Ahhhh yes that makes complete sense! Thank you Azlier. Answered my question perfectly. [+rep] :D

I suppose one final question then, should I null the bj_ after use then?
 

Azlier

Old World Ghost
Reaction score
461
No. You don't have to null global variables. the bj_ type variables are globals.
 

bastanien

New Member
Reaction score
0
Learn to indent. Port it to vJASS. Remove BJ's. Don't use locations.

And yes it leaked.

JASS:
function Trig_InitTrig_Move_Conditions takes nothing returns boolean
    return CountUnitsInGroup(udg_SpellGroup) > 0
endfunction

function PickedUnits takes nothing returns nothing
    local real posX 
    local real posY 
    local real x
    local real y = GetLocationY(source) + dist * Sin(angle * bj_DEGTORAD)
    
    set udg_UnitValue = GetUnitUserData(GetEnumUnit())
    if(udg_Distance[udg_UnitValue] > 0) then
        set udg_Distance[udg_UnitValue] = udg_Distance[udg_UnitValue]-4
        set x = GetUnitX(udg_SpellUnit[udg_UnitValue]) + 25 * Cos(udg_TargetAngle[udg_UnitValue] * bj_DEGTORAD)
        set y = GetUnitY(udg_SpellUnit[udg_UnitValue]) + 25 * Sin(udg_TargetAngle[udg_UnitValue] * bj_DEGTORAD)
        call SetUnitX(udg_SpellUnit[udg_UnitValue], x)
        call SetUnitY(udg_SpellUnit[udg_UnitValue], y)
        call RemoveLocation(pos)
        call RemoveLocation(pos2)
    else
        call KillUnit(udg_SpellUnit[udg_UnitValue])
        call RemoveUnit(udg_SpellUnit[udg_UnitValue])
    endif
endfunction

function Trig_Move_Actions takes nothing returns nothing
    call ForGroup(udg_SpellGroup, function PickedUnits)
endfunction

//===========================================================================
function InitTrig_Move takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEvent(t, 0.035, true)
    call TriggerAddCondition(t, Condition(function Trig_InitTrig_Move_Conditions))
    call TriggerAddAction(t, function Trig_Move_Actions)
  endfunction

Ok thx, only used jass for a day now so :eek:

Btw would you mind explaning "Sin(angle * bj_DEGTORAD)" ??
 

bastanien

New Member
Reaction score
0
i'm having rly bad memory leak problems , dont know what's causing it

reals and integers doesn't leak ???

JASS:
function Trig_Cast_Actions takes nothing returns nothing

local real x
local real y
local integer r
local integer g
local integer b
local integer spell
local texttag text

if(udg_CustomValue < 100) then
set udg_CustomValue = udg_CustomValue+1
else
set udg_CustomValue=1
endif

set udg_Caster[udg_CustomValue] = GetTriggerUnit()
set udg_Distance[udg_CustomValue] = 200
set udg_TargetAngle[udg_CustomValue] =  AngleBetweenPoints(GetUnitLoc(udg_Caster[udg_CustomValue]),GetSpellTargetLoc()  )

    if (GetAbilityName(GetSpellAbilityId())=="FireBolt") then // color floating text red when casting firebolt
	set r = 255 
	set g = 0 
	set b = 0
	set spell = 'n001'
	elseif(GetAbilityName(GetSpellAbilityId())=="FrostBolt") then // color floating text blue when casting frostbolt
	set r = 0 
	set g = 0 
	set b = 255
	set spell = 'n000'
	elseif(GetAbilityName(GetSpellAbilityId())=="Cannonball") then  // color floating text brown when casting Missile
	set r = 140
	set g = 120 
	set b = 100
	set spell = 'n002'
	endif	

	set x = GetUnitX(GetTriggerUnit())
	set y = GetUnitY(GetTriggerUnit())
   
    set udg_SpellUnit[udg_CustomValue] = CreateUnit(GetOwningPlayer(GetTriggerUnit()), spell , x , y, udg_TargetAngle[udg_CustomValue])  // create the spellunit    
	call GroupAddUnit(udg_SpellGroup, udg_SpellUnit[udg_CustomValue])	// add unit to spellgroup for Move Trigger
	call SetUnitUserData(udg_SpellUnit[udg_CustomValue], udg_CustomValue )  // change custom value of unit for MUI	
	
	    set text = CreateTextTag()
	    call SetTextTagText(text,GetAbilityName(GetSpellAbilityId()),0.020)          // show what spell is casted by casting unit   
	    call SetTextTagPos(text,x,y,10)
        call SetTextTagColor(text,r,g,b,255)           
	    call SetTextTagVelocity(text,0.025,0.05)
        call SetTextTagPermanent(text, false)
        call SetTextTagLifespan(text,1.5)
        call SetTextTagFadepoint(text,1)   	
	    set text = null

endfunction

//===========================================================================
function InitTrig_Cast takes nothing returns nothing
    
	set gg_trg_Cast = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Cast, function Trig_Cast_Actions )
    call TriggerRegisterAnyUnitEventBJ(gg_trg_Cast,EVENT_PLAYER_UNIT_SPELL_EFFECT)  // any unit casts a spell
	
endfunction
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Lolz.. I think u make it GUI then convert it then optimized it... It is bad. Throw it into the spell trigger is better.
 
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