need some help with spawn units trigger

afisakov

You can change this now in User CP.
Reaction score
37
I am quiet new to Jass and need some help getting the syntax right. I am trying to make a function that can conveniently create groups of units at a specified location without leaking and need some help with the syntax.
The trigger I am trying to make is the third one, first 2 just context.

I am trying to fix leaks on a map that spawns units like this
Code:
call CreateNUnitsAtLocFacingLocBJ($C,'nzom',Player(5),GetRectCenter(MB),GetUnitLoc(ev[0]))
call CreateNUnitsAtLocFacingLocBJ(3,'n006',Player(5),GetRectCenter(MB),GetUnitLoc(ev[0]))
call CreateNUnitsAtLocFacingLocBJ($A,'nzom',Player(5),GetRectCenter(pB),GetUnitLoc(ev[0]))
call CreateNUnitsAtLocFacingLocBJ(5,'n006',Player(5),GetRectCenter(pB),GetUnitLoc(ev[0]))
call CreateNUnitsAtLocFacingLocBJ($A,'nzom',Player(5),GetRectCenter(PB),GetUnitLoc(ev[0]))
call CreateNUnitsAtLocFacingLocBJ(5,'n006',Player(5),GetRectCenter(PB),GetUnitLoc(ev[0]))
Which I am pretty sure creates a large number of point and unit group leaks

my first attempt at a fix looked like
Code:
function spn0062 takes nothing returns nothing
set bj_forLoopBIndex=1
set bj_forLoopBIndexEnd=spa
loop
exitwhen bj_forLoopBIndex>bj_forLoopBIndexEnd
call CreateNUnitsAtLoc(1,'n006',Player($C),hx,90.)
call IssuePointOrderByIdLoc(bj_lastCreatedUnit,$D000F,gx)
set bj_forLoopBIndex=bj_forLoopBIndex+1
endloop
endfunction
...
set hx=GetRandomLocInRect(MB)
set gx=GetUnitLoc(ev[0])
set spa=3
call TriggerExecute (spn006m)
call RemoveLocation (hx)
set hx=GetRectCenter(pB)
it worked but was tedious since it required making a new trigger for every unit type

now I am hoping to make a more comprehensive trigger to handle all the unit types and be easier to use
I am hoping to make a trigger of the type:
Code:
function spnu takes integer spa,unittype spu,location hx,location gx returns nothing
set bj_forLoopBIndex=1
set bj_forLoopBIndexEnd=spa
loop
exitwhen bj_forLoopBIndex>bj_forLoopBIndexEnd
call CreateNUnitsAtLoc(1,spu,Player($C),hx,90.)
call IssuePointOrderByIdLoc(bj_lastCreatedUnit,$D000F,gx)
set bj_forLoopBIndex=bj_forLoopBIndex+1
endloop
endfunction
...
function main...
set spawnu=CreateTrigger()
call TriggerAddAction(spnawnu,function spnu)
however I do not know if I made this right nor do I know how to call on a trigger like this.
would I use a line like
Code:
call TriggerExecute (spawnu(10,'n006',GetRandomLocInRect(MB),GetRandomLocInRect(MC)))
? or how would I call on this? also do I remove the points in the spawn function after the endloop, or in the trigger that called on it?
Please explain how to properly implement a trigger/function like this.
 

afisakov

You can change this now in User CP.
Reaction score
37
Wow, 50 views and no responses.
Is my post too long or is it that hard a problem to solve?

The abridged version:
I want to make a function that takes a location, unit type, and integer;
and then spawns the specified number of units of that type at the location without leaking

I made an attempt to create the function but don't know how to call on it and am not sure if I did it properly.

If it is not possible to make a function/trigger like this please let me know so I can stop monitoring this thread.
 

afisakov

You can change this now in User CP.
Reaction score
37
I am getting really tired of it duplicating all my posts...
I know I lag a bit on this site but it shouldn't be this bad.
btw, can a mod delete this post.
 

Tyrulan

Ultra Cool Member
Reaction score
37
Hi afisakov,

Unfortunately a lot of the active members on this site no longer monitor the Wc3 JASS forums, so help with more difficult problems is few and far between. But I will do what I can.

It sounds like you want to make a wrapper around the CreateNUnitsAtLocFacingLocBJ. This means that your wrapper function will take the same parameters, plus the additional parameters that you need, such as unit id.

Also, when using built in BJ functions, they clean up leaks pretty well, except they leave one or two for you to handle.

Following the code we see that CreateNUnitsAtLocFacingLocBJ calls on this code:

JASS:
function CreateNUnitsAtLoc takes integer count, integer unitId, player whichPlayer, location loc, real face returns group
    call GroupClear(bj_lastCreatedGroup)
    loop
        set count = count - 1
        exitwhen count < 0
        call CreateUnitAtLocSaveLast(whichPlayer, unitId, loc, face)
        call GroupAddUnit(bj_lastCreatedGroup, bj_lastCreatedUnit)
    endloop
    return bj_lastCreatedGroup
endfunction

You will need to set the bj_lastCreatedUnit to null and use the GroupClear function on bj_lastCreatedGroup.

Executing a trigger manually suggests to me that you've already received an event elsewhere, and would like to call upon a more global function to use frequently. I recommend writing your wrapper function in the global function space provided in the World Editor.
 

Tyrulan

Ultra Cool Member
Reaction score
37
Continuing,

Typically, one nullifies variables in the calling function, and not inside the function they were passed to.

If I were to write some code like so:
JASS:
local integer x = 15
call SomeFuncitonThatNullifiesX(x)
call SomeFunctionThatUsesX(x)

Of course our integer x would be null, and unusable in "SomeFunctionThatUsesX." It makes more sense to modify the above to look like so:
JASS:
local integer x = 15
call SomeFuncitonThatUses(x)
call SomeOtherFunctionThatUsesX(x)
set x = null

I hope this make things more clear for you.

Let me know if you need more help with this.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top