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

Keyboard Event + Unit Selection

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

How do I make this trigger in JASS?

1. a player selects a unit (of a pre-determined type, e.g. 'hfoo')
2. the player then presses the ESC key

3. do some action if 1,2 are true
 
JASS:
scope Test initializer init

globals
    private constant integer UNIT_TYPE = 'hfoo'
endglobals

function Selection takes nothing returns boolean
    if GetUnitTypeId(GetTriggerUnit()) == UNIT_TYPE then
        call ForceUICancelBJ(GetTriggerPlayer())
        call BJDebugMsg("A " + GetObjectName(UNIT_TYPE) + " has been selected.")
        // some more actions...
    endif
    return false
endfunction

function init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 0
    loop
        call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_SELECTED, null)
        set i = i + 1
        exitwhen i > 11
    endloop
    call TriggerAddCondition(t, Filter(function Selection))
endfunction

endscope
Something like this?
 
Hi,

How do I make this trigger in JASS?

1. a player selects a unit (of a pre-determined type, e.g. 'hfoo')
2. the player then presses the ESC key

3. do some action if 1,2 are true

I would first register when ESC is pressed (EVENT_PLAYER_END_CINEMATIC), and then I would GroupEnumUnitsSelected an see if they have that unit-type selected. It is better IMO, and easier.
 
It just enumerates through the units selected by a player and adds them to a group. You can also supply a filter to limit what units will be contained in the group. This is a rough skeleton of what the code would look like:
JASS:
library SelectEscape initializer Init
    
    globals
        private group selected = CreateGroup()
    endglobals

    private function FilterGroup takes nothing returns boolean
        /* ensure that the selected unit(s) have an id of 'hfoo' */
        return GetUnitTypeId(GetFilterUnit()) == 'hfoo'
    endfunction

    private function Escape takes nothing returns boolean
        /* enumerate the units selected by the player */
        call GroupEnumUnitsSelected(selected, GetTriggerPlayer(), Filter(function FilterGroup))
        if FirstOfGroup(selected) != null then 
            /* one or more of the selected units follow the condition */
        endif
        return false
    endfunction 

    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        loop
            exitwhen i == 12
            call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_END_CINEMATIC)
            set i = i + 1
        endloop
        call TriggerAddCondition(t, Condition(function Escape))
    endfunction

endlibrary
 
Status
Not open for further replies.
Top