• 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] First Attempt at JASS, need help

Status
Not open for further replies.
Level 4
Joined
Jul 24, 2008
Messages
108
Ok, this is my first attempt at doing anything in jass. Basically what i want to do is order all units owned by Player 11 (darkgreen) and order each units to attack the location of a random unit owned by the other force ( allies of red )

Im using jasscraft and the debug errors are just confusing me and i cannot figure it out. Could someone take a look at this code?

JASS:
function kodoattacktarget takes nothing returns nothing     
local group origgroup = ForGroupBJ(GetUnitsOfPlayerAll(Player(10)))
local group g = CreateGroup()
call GroupAddGroup(origgroup, g)
loop
    local unit u = FirstOfGroup(g)
    exitwhen u == null
    local unit k =  GroupPickRandomUnit(GetUnitsOfPlayerAll(ForcePickRandomPlayer(GetPlayersAllies(Player(0)))))
    call IssuePointOrderLocBJ(u , attack , GetUnitLoc(k) ) 
    set unit k = null
    call GroupRemoveUnit(g, u)
endloop
call DestroyGroup(g)
endfunction
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
JASS:
local group origgroup = ForGroupBJ(GetUnitsOfPlayerAll(Player(10)))
must be
JASS:
local group origgroup =GetUnitsOfPlayerAll(Player(10))
since ForGroupBJ is a function that executes another function for all units in a certain group.

You're also leaking quite a lot.
First of all: origgroup is never destroyed. It is in fact never even used other than to add the group to group g, so instead, just use this:
JASS:
local group g = GetUnitsOfPlayerAll(Player(10))
loop
    ...
Next, we have the loop. Everytime you loop through it you leak a point and a group:
GetUnitsOfPlayerAll(ForcePickRandomPlayer(GetPlayersAllies(Player(0))))
GetUnitLoc(k)

You're also declaring a local unit inside a loop. ALL local variables must be declared at the TOP of the function, no other code is allowed to be written before a local declaration.

Proposed fix:
JASS:
function kodoattacktarget takes nothing returns nothing
    local group g = GetUnitsOfPlayerAll(Player(10))
    local group enemies = GetUnitsOfPlayerAll(ForcePickRandomPlayer(GetPlayersAllies(Player(0))))
    local unit u
    local location loc

    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        set loc = GetUnitLoc(GroupPickRandomUnit(enemies))
        call IssuePointOrderLocBJ(u , "attack" , loc )
        call GroupRemoveUnit(g, u)
        call RemoveLocation(loc)
    endloop
    call DestroyGroup(g)
    call DestroyGroup(enemies)
    set g = null
    set enemies = null
    set loc = null
endfunction
 
Last edited:
Level 4
Joined
Jul 24, 2008
Messages
108
no errors from world edit except for this one

JASS:
 call IssuePointOrderLocBJ(u , attack , loc )

It says "expected a name" for something in this line, which i do not get. But thanks so much for your help +rep

EDIT: NVM i forgot the quotes
 
Level 4
Joined
Jul 24, 2008
Messages
108
Aww, i love you all. you guys are so helpful. Everything works, and i don't think there are any leaks!. thanks everyone
 
Status
Not open for further replies.
Top