• 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.

[vJASS] Selection Utils

Status
Not open for further replies.
Hey peeps, I just wanna post a WIP of a vJass utility I am developing, I'm looking for critiques/problems with it. So here it is :3
It's meant to be GUI friendly with REAL events and udg variables.
JASS:
/* 
boolean        udg_AlreadySelected  determines on the selection event if a unit is already selected.
group array    udg_Selected         is used for getting the group of units a player has selected based on their GUI player ID. (GetPlayerId()+1)
integer array  udg_SelectedNum      is used for getting the number of units a player has selected based on their GUI player ID. (GetPlayerId()+1)
real           udg_SelectEvent      is used for REAL trigger variables events. 1.00 is selection, 2.00 is deselection.
player         udg_SelectingPlayer  is used with udg_SelectEvent to get the selecting player.
unit           udg_SelectUnit       is used with udg_SelectEvent to get the selected unit.
*/
library SelectionUtils initializer init

//Function detecting selection, fires selection event
    private function Select takes nothing returns boolean
        local unit u=GetTriggerUnit()
        local player p=GetTriggerPlayer()
        local integer i=GetPlayerId(p)+1
        set udg_AlreadySelected=false
        if not IsUnitInGroup(u,udg_Selected[i]) then
            call GroupAddUnit(udg_Selected[i],u)
            set udg_SelectedNum[i]=udg_SelectedNum[i]+1
        else
            set udg_AlreadySelected=true
        endif
        set udg_SelectingPlayer=p
        set udg_SelectUnit=u
        set udg_SelectEvent=1
        set udg_SelectEvent=0
        set u=null
        set p=null
        return false
    endfunction

//Function detecting unselection, fires unselect event
    private function Unselect takes nothing returns boolean
        local unit u=GetTriggerUnit()
        local player p=GetTriggerPlayer()
        local integer i=GetPlayerId(p)+1
        call GroupRemoveUnit(udg_Selected[i],u)
        set udg_SelectedNum[i]=udg_SelectedNum[i]-1
        set udg_SelectingPlayer=p
        set udg_SelectUnit=u
        set udg_SelectEvent=2
        set udg_SelectEvent=0
        set u=null
        set p=null
        return false
    endfunction

//Function that should be coupled with an indexer to catch removed units and unselect them
    public function EndSelect takes unit u returns nothing
        local integer i=0
        loop
            exitwhen i>=12
            set i=i+1
            if IsUnitInGroup(u,udg_Selected[i]) then
                call GroupRemoveUnit(udg_Selected[i],u)
                set udg_SelectedNum[i]=udg_SelectedNum[i]-1
                set udg_SelectingPlayer=Player(i-1)
                set udg_SelectUnit=u
                set udg_SelectEvent=2
                set udg_SelectEvent=0
            endif
        endloop
    endfunction
    
//Function detects unit death and unselects selected units
    private function Death takes nothing returns boolean
        call EndSelect(GetTriggerUnit())
        return false
    endfunction

    private function init takes nothing returns nothing
        local trigger t1=CreateTrigger()
        local trigger t2=CreateTrigger()
        local player p
        local integer i=0
        call TriggerAddCondition(t1, Condition(function Death))
        call TriggerRegisterAnyUnitEventBJ(t1,EVENT_PLAYER_UNIT_DEATH)
        set t1=CreateTrigger()
        loop
            exitwhen i>11
            set p=Player(i)
            if GetPlayerController(p) == MAP_CONTROL_USER and GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING then
                set udg_Selected[i+1]=CreateGroup()
                call TriggerRegisterPlayerUnitEvent(t1, p, EVENT_PLAYER_UNIT_SELECTED, null)
                call TriggerAddCondition(t1, Condition(function Select))
                call TriggerRegisterPlayerUnitEvent(t2, p, EVENT_PLAYER_UNIT_DESELECTED, null)
                call TriggerAddCondition(t2, Condition(function Unselect))
            endif
            set i=i+1
        endloop
        set t1=null
        set t2=null
        set p=null
    endfunction

endlibrary
Fixed:
JASS:
//Function that should be coupled with an indexer to catch removed units and unselect them
    public function EndSelect takes unit u returns nothing
        local integer i=0
        loop
            exitwhen i>=12
            set i=i+1
            if IsUnitInGroup(u,udg_Selected[i]) then
                call GroupRemoveUnit(udg_Selected[i],u)
                set udg_SelectedNum[i]=udg_SelectedNum[i]-1
                set udg_SelectingPlayer=Player(i-1)
                set udg_SelectUnit=u
                set udg_SelectEvent=2
                set udg_SelectEvent=0
            endif
        endloop
        set u=null
    endfunction
->
JASS:
//Function that should be coupled with an indexer to catch removed units and unselect them
    public function EndSelect takes unit u returns nothing
        local integer i=0
        loop
            exitwhen i>=12
            set i=i+1
            if IsUnitInGroup(u,udg_Selected[i]) then
                call GroupRemoveUnit(udg_Selected[i],u)
                set udg_SelectedNum[i]=udg_SelectedNum[i]-1
                set udg_SelectingPlayer=Player(i-1)
                set udg_SelectUnit=u
                set udg_SelectEvent=2
                set udg_SelectEvent=0
            endif
        endloop
    endfunction
 
Last edited:
Status
Not open for further replies.
Top