• 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 optimizing this function

Status
Not open for further replies.
Level 4
Joined
Jul 24, 2008
Messages
108
Ok, I just got my script to work and im all happy about it, however, purplepoot just pointed out that my way of doing things are very inefficient and leaks a ton.

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
        call IssueImmediateOrderBJ( u, "stop" )
        set loc = GetUnitLoc(GroupPickRandomUnit(enemies))
        call IssuePointOrderLocBJ(u , "attack" , loc )
        call PingMinimapLocForForce( GetPlayersAll(), loc, 1 )
        call GroupRemoveUnit(g, u)
        set loc = null
    endloop
    call DestroyGroup(g)
    call DestroyGroup(enemies)
    call RemoveLocation(loc)
    set g = null
    set enemies = null
    set loc = null
endfunction

So my question is this now, why is it so bad to use BJ functions?
What locations and groups are being leaked? I nulled and destroyed the 3 i'm using. I don't know if this is that inefficient and im just over worrying about it, but my map desynced earlier today and I think this was the cause of it, or it was just comcast cause vent dced :O
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
So my question is this now, why is it so bad to use BJ functions?
Extra overhead for no advantage.

What locations and groups are being leaked? I nulled and destroyed the 3 i'm using.
You have to destroy the object every time you change the pointer, not just at the end.

Optimized script:

JASS:
function TrueFilter takes nothing returns boolean
    return true //to do with weird behaviour with GroupEnums
endfunction

function kodoattacktarget takes nothing returns nothing
    local group g = CreateGroup()
    local group g2 = CreateGroup()
    local integer i
    local unit u
    local unit r
    call GroupEnumUnitsOfPlayer(g,Player(10),Filter(function TrueFilter))
    loop
        set i = GetRandomInt(0,11)
        exitwhen IsPlayerAlly(Player(0),Player(i))
    endloop
    call GroupEnumUnitsOfPlayer(g2,Player(i),Filter(function TrueFilter))
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        set r = GroupPickRandomUnit(g2)
        call IssuePointOrder(u,"attack",GetUnitX(r),GetUnitY(r))
        call PingMinimap(GetUnitX(r),GetUnitY(r),1)
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    call DestroyGroup(g2)
    set r = null
    set g = null
    set g2 = null
endfunction
 
Level 4
Joined
Jul 24, 2008
Messages
108
thanks for the fast reply, ok i understand everything that was changed except for this part

JASS:
 call GroupEnumUnitsOfPlayer(g,Player(10),Filter(function TrueFilter))
    loop
        set i = GetRandomInt(0,11)
        exitwhen IsPlayerAlly(Player(0),Player(i))
    endloop
    call GroupEnumUnitsOfPlayer(g2,Player(i),Filter(function TrueFilter))

I am not certain on what this actually acomplishes. Sorry I don't have all the functions yet memorized on what they do, what does the GroupEnumUnitsOfPlayer do?
 
Level 4
Joined
Jul 24, 2008
Messages
108
Yay, thanks purple, i think i got a fully optimized script. I edited it because it didn't do exactly as I wanted but i think its still leak free :)

JASS:
function TrueFilter takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) != 'eC06' //Jail unit
endfunction


function kodoattacktarget takes nothing returns nothing
    local group g = CreateGroup()
    local group g2 = CreateGroup()
    local integer i
    local unit u
    local unit r
    call GroupEnumUnitsOfPlayer(g,Player(10),Filter(function TrueFilter))
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        loop
            set i = GetRandomInt(0,9)
            if CountUnitsInGroup(GetUnitsOfPlayerAndTypeId(Player(i), 'uC01')) == 1 then 
//repeats loop until player has an active runner, avoid having kodos attack a player with no units
                exitwhen IsPlayerSlotState(Player(i),PLAYER_SLOT_STATE_PLAYING)
            else
        
            endif
            
        endloop
        call GroupEnumUnitsOfPlayer(g2,Player(i),Filter(function TrueFilter))
        set r = GroupPickRandomUnit(g2)
        call IssuePointOrder(u,"attack",GetUnitX(r),GetUnitY(r))
        call PingMinimap(GetUnitX(r),GetUnitY(r),1)
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    call DestroyGroup(g2)
    set r = null
    set g = null
    set g2 = null
endfunction
 
Level 4
Joined
Jul 24, 2008
Messages
108
.....omg how whats the point of these flippin functions if they all are bad and leak!! what is the equivalent that doesn't leak than? is there a list or something like that where i can look up whether or not it leaks? im tired of asking you guys all these stupid questions :( (you guys as in purplepoot and elandor (sp?)) I'm trying my best, but while i was testing the map with my function, i noticed it did start to lag the last 5-10 minutes, so i could definatly tell it was leaking, but it wasn't leaking that much where it caused desyncs! (everyone except 1 person stayed until the end! but it was his dsl that kicked him out). Thanks for all your help purple and everyone else, you are making this easy for me to learn
 
Level 4
Joined
Jul 24, 2008
Messages
108
Ok, i think i got this all fixed, sorry for poor formatting, this 1.2ghz and 128mb ram computer im using atm doesn't got no editing text software :p

JASS:
function TrueFilter takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) != 'eC06' //Jail unit
endfunction


function kodoattacktarget takes nothing returns nothing
    local group g = CreateGroup()
    local group g2 = CreateGroup()
    local integer i
    local unit u
    local unit r
    local unit t
    call GroupEnumUnitsOfPlayer(g,Player(10),Filter(function TrueFilter))
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        loop
            set i = GetRandomInt(0,9)
   set t = GetUnitsOfPlayerAndTypeId(i), 'uC01')
            if CountUnitsInGroup(t) == 1 then
//repeats loop until player has an active runner, avoid having kodos attack a player with no units
                set t = null
                call DestroyGroup(t)
                exitwhen IsPlayerSlotState(Player(i),PLAYER_SLOT_STATE_PLAYING)
            else
                set t = null 
                call DestroyGroup(t)

            endif

        endloop
        call GroupEnumUnitsOfPlayer(g2,Player(i),Filter(function TrueFilter))
        set r = GroupPickRandomUnit(g2)
        call IssuePointOrder(u,"attack",GetUnitX(r),GetUnitY(r))
        call PingMinimap(GetUnitX(r),GetUnitY(r),1)
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    call DestroyGroup(g2)
    set r = null
    set g = null
    set g2 = null
endfunction

So all i did was instead of having the GetPlayerAndUnitsOfTypeId inside the condition, i set it to group t then ran group t inside the condition, then reguardless of what happened in the if/then statement, it nulled and destroyed t, and would either do it again, or go on?

Please tell me thats correct :)
 
Status
Not open for further replies.
Top