• 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] Help with spawning

Status
Not open for further replies.
Level 3
Joined
Jul 15, 2007
Messages
36
Help with spawning (Edited: Now Check for leaks)

Dear all,

I am trying to make some clean spawning triggers. The goal of the trigger is:

==========={EDIT}===========

Thank-you for looking at this; I have solved my endless loop problems, and made the following code. I wonder if I've accurately cleaned up all the leaks?

JASS:
function DoSpawn takes nothing returns nothing
    local integer i = 0
    local integer count = 0
    local location loc = null
    loop
        exitwhen i == 8
        set count = udg_Level1[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[0] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00I', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level2[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[1] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00J', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level3[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[2] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00K', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level4[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[3] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00L', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level5[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[4] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00M', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level6[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[5] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00N', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level7[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[6] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00O', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level8[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[7] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00P', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level9[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[8] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00Q', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level10[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[9] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00R', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level11[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[10] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00S', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level12[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[11] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00T', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level13[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[12] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00U', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level14[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[13] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00V', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set count = udg_Level15[i]
        set udg_Income[i] = udg_Income[i] + ( udg_SpawnIncome[14] * count )
        loop
            exitwhen count == 0
            set loc = GetRandomLocInRect(udg_SpawnRect[i])
            call CreateUnitAtLoc(Player(i), 'n00W', loc, 90.00)
            call RemoveLocation(loc)
            set count = count - 1
        endloop
        set i = i + 1
    endloop
    call updateIncome()
endfunction

Thank-you
 
Last edited:
Level 8
Joined
Jul 23, 2005
Messages
329
Either or will do...

RemoveLocation will destroy the handle

setting it to null will set the handle to nothing (I.E. Now its pointing nowhere)
 
Level 11
Joined
Feb 18, 2004
Messages
394
you mean
JASS:
call RemoveLocation(loc)
i ain't too good at jass but i know that this applies to locations

All the locations are removed. but you have to change the value of a local, or it will not decrease the refrence count on the handle, resulting in a memory leak. Thus, you set its value to null. This may also be a problem with globals, but who cares? their value is eventually changed anyway, solving the problem. But locals go out of scope at the end of the function, creating a problem. (basically, blizzard forgot to take one off the refrence counter for every local variable when a function ends.)

Oh, and this only effects handles. Strings leak anyway, and native types don't have refrence counters, or need them.
 
Level 5
Joined
Jul 24, 2007
Messages
199
Can somebody explain why not loop it all in 15-times cycle, with unit-type being set to previous+1? Unit-type ID is an integer, isn't it? (2dimensional array AxB is made from 1dimensional like Array[i*A+j], where 1<=i<B and 1<=j<A)
And... how does udg_MyArray[0] works? Is there such an element?

And the main question.
:eekani:WHAT DOES IT ALL DO?:eek:
 
Level 3
Joined
Jul 15, 2007
Messages
36
Ty all to the replies..


As to putting an extra loop around, and having an array of stored spawn unit-id's, I believe that wouldn't work. This is because I'm essentially using a matrix of "udg_LevelX", with X being from 1 - 15, and each of those storing values in positions 0 - 7 (i.e. one for each player for each level of creep. AFAIK War3 doesn't support tru matracies without going into structs/gamecaches and other stuff too complicated for me at the moment.

Additional con - I would have to define the units elsewhere, in an array, taking up all the space that an array does. Shorter code, not sure it would be more efficient memory - wise. At the moment, I'm sticking to something very similar to above (with the leak mentioned fixed)

As to what does it do?

It spawns a certain number of creeps when triggered, for each player. The number of creeps each player each level is defined elsewhere (a function counting the types of a particular structure owned by that player). It also adds to the income of the player (stored in udg_Income) based on the value of that level of spawn (stored in udg_SpawnIncome).

The income is given in a seperate function, running off a timer.
The spawn function is run off a timer event.
The creeps are issued a move-order when they are created in the region, by a seperate "unit enters region" event.


Ty Ty again for all the help. Project is paused while I work on
Map Development Mini-Contest #1, which I'm trialling things I want in the map this is for.
 
Status
Not open for further replies.
Top