Easy for you, impossible for me

warden13

New Member
Reaction score
32
I am Jassing for two days. That makes me a big noob in Jass. So I coded a spell, that creates units as a line and adds them to a unit group. This is ok.
But I want to do this:
If a unit in unit group dies, kill all units in unit group. And clean the leaks and memory. Thanks...

JASS:
function Trig_ET_Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A000'
endfunction

function filtre takes nothing returns boolean
    return ( GetDestructableTypeId(GetFilterDestructable()) == 'ATtr' )
endfunction

function Trig_ET_Actions takes nothing returns nothing
    local unit u
    local player p
    local group g = CreateGroup()
    local destructable t1
    local destructable t2
    local location loc0
    local location loc1
    local location loc2
    local real x
    local real y
    local real dis
    local integer mc
    local integer count
    local rect area
    local unit m
set count = 0
set u = GetSpellAbilityUnit()
set p = GetOwningPlayer(u)
set t1 = GetSpellTargetDestructable()
set loc0 = GetUnitLoc(u)
set area = RectFromCenterSizeBJ(loc0, 300.00, 300.00)
set t2 = RandomDestructableInRectBJ(area, Condition(function filtre))
set loc1 = GetDestructableLoc(t1)
set loc2 = GetDestructableLoc(t2)
set x = GetDestructableX(t2)
set y = GetDestructableY(t2)
set dis = DistanceBetweenPoints(loc1, loc2)
set mc = R2I(dis/30)
set abp = bj_RADTODEG * Atan2(GetLocationY(loc1) - GetLocationY(loc2), GetLocationX(loc1) - GetLocationX(loc2))


loop
    exitwhen count > mc
    set x = x + 30 * Cos(abp * bj_DEGTORAD)
    set y = y + 30 * Sin(abp * bj_DEGTORAD)
    set m = CreateUnit(p, 'n001', x, y, 0)
    call GroupAddUnit(g, m)
    set count = count + 1
endloop
endfunction

function InitTrig_ET takes nothing returns nothing
    set gg_trg_ET = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_ET, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( gg_trg_ET, Condition( function Trig_ET_Conditions ) )
    call TriggerAddAction( gg_trg_ET, function Trig_ET_Actions )
endfunction
 

Exide

I am amazingly focused right now!
Reaction score
448
JASS:

set abp = bj_RADTODEG * Atan2(GetLocationY(loc1) - GetLocationY(loc2), GetLocationX(loc1) - GetLocationX(loc2))


-You don't have a local variable named abp.

>If a unit in unit group dies, kill all units in unit group. And clean the leaks and memory.
You should have another trigger checking if *unit* dies, then do ...
 

Exide

I am amazingly focused right now!
Reaction score
448
-local real abp fixed.

-But my group (g) will get removed outside of trigger. Right?

Yes, you will need a global variable.
 

warden13

New Member
Reaction score
32
Can you code it for me? I don't know how to use globals. So I will learn how to use them as well.
 

Exide

I am amazingly focused right now!
Reaction score
448
Global variables are "GUI variables", also known as User Defined Globals (udg).
For example
Trigger:
  • custom script call RemoveLocation(udg_temppoint)


Just change your local variable into a global one. Go into Trigger Editor, click on the Variables Editor (X icon), create a new 'unit group - variable', name it something like 'DeathGroup' or whatever.
Then change this line in your JASS code:

JASS:

    call GroupAddUnit(g, m)


into:

JASS:

    call GroupAddUnit(udg_DeathGroup, m)


Now all units that this trigger creates will be added into that group, and you can do whatever you want with them, in another trigger (for example killing them.)



EDIT: I tried to optimize your trigger a little. (Getting rid of most of the BJ's, and point variables, using coordinates instead.) Try using this trigger:

JASS:

function Trig_ET_Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A000'
endfunction

function filter_func takes nothing returns boolean
    return (GetDestructableTypeId(GetFilterDestructable()) == 'ATtr')
endfunction

function Trig_ET_Actions takes nothing returns nothing
    local unit u = GetSpellAbilityUnit()
    local player p = GetOwningPlayer(u)
    local group g = CreateGroup()
    local destructable t1 = GetSpellTargetDestructable()
    local destructable t2
    local real array x
    local real array y
    local real dis
    local integer mc
    local integer i = 0
    local rect area
    local unit m
    local real abp


    set x[1] = GetUnitX(u)
    set y[1] = GetUnitY(u)

    set area = Rect(x[1] - 300*0.5, y[1] - 300*0.5, x[1] + 300*0.5, y[1] + 300*0.5)
    set t2 = RandomDestructableInRectBJ(area, Condition(function filter_func))
    
    set x[2] = GetDestructableX(t1)
    set y[2] = GetDestructableY(t1)
    set x[3] = GetDestructableX(t2)
    set y[3] = GetDestructableY(t2)
    set x[4] = x[3] - x[2]
    set y[4] = y[3] - y[2]
    set dis = SquareRoot(x[4] * x[4] + y[4] * y[4])
    
    set mc = R2I(dis/30)
    set abp = bj_RADTODEG * Atan2(y[2] - y[3], x[2] - x[3])
    
    loop
        exitwhen i > mc
        set x[5] = x[3] + 30 * Cos(abp * bj_DEGTORAD)
        set y[5] = y[3] + 30 * Sin(abp * bj_DEGTORAD)
        set m = CreateUnit(p, 'n001', x[5], y[5], 270)
        call GroupAddUnit(g, m)
        set i = i + 1
    endloop
    
    set area = null
    set g = null
    set u = null
    set m = null
    set p = null
endfunction

function InitTrig_ET takes nothing returns nothing
    local trigger ET = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( ET, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( ET, Condition( function Trig_ET_Conditions ) )
    call TriggerAddAction( ET, function Trig_ET_Actions )
endfunction


-But save your old trigger to a notepad first, not sure if I miscalcuated something and screwed it up. :p
EDIT #2: Oh, and you should always null local variables that extends handle at the end of the function. (Such as player, unit, group, point (or location), etc.)
If you decide on using a global variable for the unit group in this trigger you don't need the local unit group 'g', obviously (so make sure you get rid of it.)
 

warden13

New Member
Reaction score
32
First part is working good, thanks.But second part, killing all units in group when one of them dies part is not seems to work.
I want to make it MUI, so I don't want to use global variable. Anyway, is there a function that takes the group of unit. Anything like that : GetGroupofUnit(dying unit)

And Is there a problem with these lines? If yes, how can i fix it? I heard BJs are bad :D

JASS:
set mana[1] = GetUnitStateSwap(UNIT_STATE_MANA, u)
set mana[2] = mana[1] + 200
call SetUnitManaBJ(u, mana[2])
 

Exide

I am amazingly focused right now!
Reaction score
448
Yes, BJ's are "bad". Because they call a function, when you can really just get rid of the 'BJ', and do the same thing.
For example:

JASS:

function SetUnitManaBJ takes unit whichUnit, real newValue returns nothing
    call SetUnitState(whichUnit, UNIT_STATE_MANA, RMaxBJ(0,newValue))
endfunction


or:

JASS:

function GetUnitStateSwap takes unitstate whichState, unit whichUnit returns real
    return GetUnitState(whichUnit, whichState)
endfunction


or:

JASS:

function SetUnitLifeBJ takes unit whichUnit, real newValue returns nothing
    call SetUnitState(whichUnit, UNIT_STATE_LIFE, RMaxBJ(0,newValue))
endfunction


If you don't already have it, I suggest you get JassCraft, when triggering with JASS. It's awesome. =)


In your example, you could do this:




For your unit group problem you could do something like this:

Trigger:
  • Untitled Trigger 001
    • Events
      • Unit - A unit Dies
    • Conditions
      • ((Triggering unit) is in DeathGroup) Equal to True
    • Actions
      • Unit Group - Pick every unit in DeathGroup and do (Actions)
        • Loop - Actions
          • Unit - Kill (Picked unit)


Or

Trigger:
  • Untitled Trigger 001
    • Events
      • Unit - A unit Dies
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ((Triggering unit) is in DeathGroup[1]) Equal to True
        • Then - Actions
          • Unit Group - Pick every unit in DeathGroup[1] and do (Actions)
            • Loop - Actions
              • Unit - Kill (Picked unit)
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ((Triggering unit) is in DeathGroup[2]) Equal to True
        • Then - Actions
          • Unit Group - Pick every unit in DeathGroup[2] and do (Actions)
            • Loop - Actions
              • Unit - Kill (Picked unit)
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ((Triggering unit) is in DeathGroup[3]) Equal to True
        • Then - Actions
          • Unit Group - Pick every unit in DeathGroup[3] and do (Actions)
            • Loop - Actions
              • Unit - Kill (Picked unit)
        • Else - Actions


-Where you use a Unit Group Array in your JASS trigger like this:

JASS:

        call GroupAddUnit(udg_DeathGroup[GetPlayerId(p) +1], m)




You can also change:

JASS:


into:

JASS:

    local unit u = GetTriggerUnit()


it doesn't really matter.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top