• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Which is better in filtering group??

Status
Not open for further replies.
Level 22
Joined
Feb 6, 2014
Messages
2,466
Which is better? qwerty1 or qwerty2?

EDIT: By better, I meant faster. qwerty2 seems slower because it will pick all units and evaluate if it is an enemy but I'm not sure because I don't know how group filter works. Also, does the Filter(code) leak?

JASS:
scope qwerty1 initializer OnInit
    
    globals
        private player filterPlayer
    endglobals
    
    private function FilterUnits takes nothing returns boolean
        return IsUnitEnemy(GetFilterUnit(), filterPlayer)
    endfunction

    private function Cast takes nothing returns nothing
        local player p = GetTriggerPlayer()
        local unit u
        local group g = CreateGroup()
        set filterPlayer = p
        call GroupEnumUnitsInRange(g, 0, 0, 1500, Filter(function FilterUnits))
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            call GroupRemoveUnit(g, u)
            //do actions here
            call KillUnit(u)
            call PingMinimap(GetUnitX(u), GetUnitY(u), 1.)
        endloop
        call DestroyGroup(g)
        set p = null
        set g = null
    endfunction
    
    private function OnInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterPlayerChatEvent(t, Player(0), "qwerty1", true)
        call TriggerAddAction(t, function Cast)
    endfunction
endscope

scope qwerty2 initializer OnInit
    
    private function FilterUnits takes unit u, player p returns boolean
        return IsUnitEnemy(u, p)
    endfunction

    private function Cast takes nothing returns nothing
        local player p = GetTriggerPlayer()
        local unit u
        local group g = CreateGroup()
        call GroupEnumUnitsInRange(g, 0, 0, 1500, null)
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            call GroupRemoveUnit(g, u)
            if FilterUnits(u,p) then
                //do actions here
                call KillUnit(u)
                call PingMinimap(GetUnitX(u), GetUnitY(u), 1.)
            endif
        endloop
        call DestroyGroup(g)
        set p = null
        set g = null
    endfunction
    
    private function OnInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterPlayerChatEvent(t, Player(0), "qwerty2", true)
        call TriggerAddAction(t, function Cast)
    endfunction
endscope
 
Last edited:
Level 19
Joined
Dec 12, 2010
Messages
2,069
speaking logically
in case of Filter game use native C++-built techniques, quickly passing through units, passing them to filter
in case of manual control you call FirstOfGroup, set local variable to point onto unit, then check, then release unit
second one will have dozen times more empty circles.

human won't notice any differences tho between both variants, but Filter definitely faster than manually go through group
 
Status
Not open for further replies.
Top