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

[JASS] Adding Spawned Units to Group

Status
Not open for further replies.
Level 2
Joined
Dec 1, 2008
Messages
7
I have a small problem when tryign to assign creating Units to unit groups, after the first created unit, the script stops. No further Units are created / and the first is not assigned to a Unit group.

I tried setting each unit to a local variable u and then use the variable to add the unit to the group, with the same result.
All units are spawned properly if the GroupAddUnit are taken out, but obviously they are then not added to a group.

JASS:
    local group SpawnedGroup
    local integer x = GetRandomInt(2, 3)
    local integer y = GetRandomInt(0, 1)
    local location TmpUnitPoint = GetRandomLocInRect(udg_PirateSpawn[x])
    
    call CreateUnitAtLoc( Player(11), 'h00O', TmpUnitPoint, bj_UNIT_FACING )
    call GroupAddUnit(SpawnedGroup, bj_lastCreatedUnit)
    call CreateUnitAtLoc( Player(11), 'h00O', TmpUnitPoint, bj_UNIT_FACING )
    call GroupAddUnit(SpawnedGroup, bj_lastCreatedUnit )
    call CreateUnitAtLoc( Player(11), 'h00O', TmpUnitPoint, bj_UNIT_FACING )
    call GroupAddUnit(SpawnedGroup, bj_lastCreatedUnit )
    set TmpUnitPoint = GetRandomLocInRect(udg_PirateSpawn[y])
    call GroupPointOrderLocBJ( SpawnedGroup, "patrol", TmpUnitPoint  )
    set TmpUnitPoint = null
    set SpawnedGroup = null

thanks in advance.
 
Level 14
Joined
Nov 23, 2008
Messages
187
You are adding null unit to null group. It happens because you add bj_lastCreatedUnit, which is used in function CreateUnitAtLocSaveLast. It is a BJ function, that's why variable has bj_ prefix. Also, you have not created SpawnedGroup group.

To improve code, get rid of BJ functions and potential leaks.

JASS:
    // you should create group before using it
    local group SpawnedGroup = CreateGroup()

    // inlined value of x
    local location TmpUnitPoint = GetRandomLocInRect(udg_PirateSpawn[GetRandomInt(2, 3)])

    call GroupAddUnit(SpawnedGroup, CreateUnitAtLoc(Player(11), 'h00O', TmpUnitPoint, bj_UNIT_FACING))
    call GroupAddUnit(SpawnedGroup, CreateUnitAtLoc(Player(11), 'h00O', TmpUnitPoint, bj_UNIT_FACING))
    call GroupAddUnit(SpawnedGroup, CreateUnitAtLoc(Player(11), 'h00O', TmpUnitPoint, bj_UNIT_FACING))
    
    // Removing previous location
    // before assigning new location to variable
    call RemoveLocation(TmpUnitPoint)
    
    // inlined value of y
    set TmpUnitPoint = GetRandomLocInRect(udg_PirateSpawn[GetRandomInt(0, 1)])

    // changed GroupPointOrderLocBJ to GroupPointOrderLoc
    call GroupPointOrderLoc(SpawnedGroup, "patrol", TmpUnitPoint)
    
    // just nulling location / group is not enough
    // you should delete it after use
    call RemoveLocation(TmpUnitPoint)
    call DestroyGroup(SpawnedGroup)
    set TmpUnitPoint = null
    set SpawnedGroup = null

Now the code is fairly, but "correctly" optimized. Further improvement consists of replacing locations with coordinates (x and y), and having single group (instead of destroying and creating new ones) to operate units.
 
Status
Not open for further replies.
Top