• 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] Multi Instance Key press event

Status
Not open for further replies.
Level 9
Joined
Sep 8, 2004
Messages
633
Howdy! I'm using the key press event for a camera system in my map. Now it has to do this for each player, using a global boolean.

JASS:
//TESH.scrollpos=1
//TESH.alwaysfold=0
function Trig_RotateLeft_Actions takes nothing returns nothing
  set udg_Boo_Camsys_LeftPressed[1] = true
endfunction

//===========================================================================
function InitTrig_RotateLeft takes nothing returns nothing
    set gg_trg_RotateLeft = CreateTrigger(  )
    call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeft, Player(0), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
    call TriggerAddAction( gg_trg_RotateLeft, function Trig_RotateLeft_Actions)
endfunction

Naturally, I can start copying and pasting the trigger over and over and over. However, I would prefer to do it a more efficient way. Is there any? If so, please tell me!


EDIT:
Okay, after some tinkering, I've made this; opinions?
JASS:
function Trig_RotateLeftAll_Actions takes nothing returns nothing
    local integer Index
	set Index = 1
    loop
        exitwhen Index > 5
		if ( GetTriggerPlayer() == udg_Player[Index] ) then
            set udg_Boo_Camsys_LeftPressed[Index] = true
        endif
        set Index = Index + 1
    endloop
endfunction

//===========================================================================
function InitTrig_RotateLeftAll takes nothing returns nothing
    set gg_trg_RotateLeftAll = CreateTrigger(  )
    call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeftAll, Player(0), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
    call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeftAll, Player(1), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
    call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeftAll, Player(2), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
    call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeftAll, Player(3), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
    call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeftAll, Player(4), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
    call TriggerAddAction( gg_trg_RotateLeftAll, function Trig_RotateLeftAll_Actions )
endfunction
 
Last edited:
Level 40
Joined
Dec 14, 2005
Messages
10,532
Rawr.

JASS:
function TriggerRegisterPlayerKeyEventBJ takes trigger trig, player whichPlayer, integer keType, integer keKey returns event
    if (keType == bj_KEYEVENTTYPE_DEPRESS) then
        // Depress event - find out what key
        if (keKey == bj_KEYEVENTKEY_LEFT) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_LEFT_DOWN)
        elseif (keKey == bj_KEYEVENTKEY_RIGHT) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_RIGHT_DOWN)
        elseif (keKey == bj_KEYEVENTKEY_DOWN) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_DOWN_DOWN)
        elseif (keKey == bj_KEYEVENTKEY_UP) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_UP_DOWN)
        else
            // Unrecognized key - ignore the request and return failure.
            return null
        endif
    elseif (keType == bj_KEYEVENTTYPE_RELEASE) then
        // Release event - find out what key
        if (keKey == bj_KEYEVENTKEY_LEFT) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_LEFT_UP)
        elseif (keKey == bj_KEYEVENTKEY_RIGHT) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_RIGHT_UP)
        elseif (keKey == bj_KEYEVENTKEY_DOWN) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_DOWN_UP)
        elseif (keKey == bj_KEYEVENTKEY_UP) then
            return TriggerRegisterPlayerEvent(trig, whichPlayer, EVENT_PLAYER_ARROW_UP_UP)
        else
            // Unrecognized key - ignore the request and return failure.
            return null
        endif
    else
        // Unrecognized type - ignore the request and return failure.
        return null
    endif
endfunction

You could also add a loop in the events for cleanliness and otherwise sanity.
 
Level 9
Joined
Sep 8, 2004
Messages
633
I've added the loop, with some help from Nestharus I optimised it to this;

JASS:
function Trig_RotateLeft_Actions takes nothing returns nothing
	set udg_Boo_Camsys_LeftPressed[GetConvertedPlayerId(GetTriggerPlayer())] = true
endfunction

//===========================================================================
function InitTrig_RotateLeft takes nothing returns nothing
	local integer Index
	set Index = 0
	set gg_trg_RotateLeft = CreateTrigger()
	call TriggerAddAction( gg_trg_RotateLeft, function Trig_RotateLeft_Actions )
	loop
		exitwhen Index > 4
		call TriggerRegisterPlayerKeyEventBJ( gg_trg_RotateLeft, Player(Index), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )
		set Index = Index + 1
	endloop
endfunction

Thanks though!
 
Status
Not open for further replies.
Top