• 🏆 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] Event Spacebar

Status
Not open for further replies.
Hey everybody, I am having a little trouble, I want to make an event if a player presses the spacebar. I have been looking up the code and have got somewhat lost...
First thing that happens is a trigger gets the even registered.

JASS:
    call TriggerRegisterPlayerKeyEventBJ( NameOfTrigger, SomePlayer, bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT )

All looks fine here I just want to change the key_left to a spacebar... But that is not supported in the WC3 Framework so we need to add something for it.

When we look into what the TriggerRegisterPlayerKeyEventBJ is we get this...

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


Pretty much all the bj_KEYEVENTKEY_Whatever is just preset numbers corresponding to the keys that blizzard set, the key press\depress is the same, as seen here...


JASS:
    constant integer   bj_KEYEVENTKEY_DOWN         = 2
    constant integer   bj_KEYEVENTKEY_LEFT         = 0
    constant integer   bj_KEYEVENTKEY_RIGHT        = 1
    constant integer   bj_KEYEVENTKEY_UP           = 3

    constant integer   bj_KEYEVENTTYPE_DEPRESS     = 0
    constant integer   bj_KEYEVENTTYPE_RELEASE     = 1


That is all pretty basic but that is a pretty closed system. I understand how making another function that allows for a fifth key could work, but lets look back at the register function, or a piece of it anyway.


JASS:
 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)


What is this EVENT_PLAYER_ARROW_LEFT_DOWN? This is actually an event, and this is about where I get lost. How can I add a key if I do not understand what these "playerevent"s are?


JASS:
    constant playerevent        EVENT_PLAYER_ARROW_LEFT_UP              = ConvertPlayerEvent(262)


What exactly is a ConvertPlayerEvent?


JASS:
constant native ConvertPlayerEvent          takes integer i returns playerevent
That is relatively useless...


As you can see it does seem very closed...


So does anyone know how to work around this?
Worst comes to worst I could have players use the escape key but that seems somewhat uncomfortable.
 
Level 6
Joined
May 7, 2009
Messages
228
It's impossible add keys because they are hardcoded into the actual game by Blizzard.
The only keys for which you can detect both pressing and releasing are the arrow keys.

If you only want to detect a keypress and don't care about key release, you can detect the space bar with EVENT_PLAYER_END_CINEMATIC.
 
Level 9
Joined
Jul 3, 2008
Messages
495
They may be one work arround way. I always thought about it but never tested it. So anyone with enough free time to test this, please do so. But this may only work if your camera is "locked"/panned to an unit.

There is a trigger that set a space point in your map (dont remember the name). When you press space the camera will jump to that point.

I don't have WE open!, and the name of the triggers may not be correct

Event:
  • Every 0.04 seconds
Action:
JASS:
local real cx = GetUnitX(udg_unit)
local real cy = GetUnitY(udg_unit)
call SetSpacePointXY(cx-10,cy)
if GetCameraX == cx-10 and GetCameraY == cy then
call DisplayTextMessage(Player(0),0,0,"SPACE BAR DETECTED!!!")
endif
call SetCameraPositionX(cx)
call SetCameraPositionY(cy)

This may work with bugs. But maybe its possible to detect in some kind of this way. I need to be tested
 
Level 8
Joined
Aug 4, 2006
Messages
357
i feel like a genius :D maybe i'll post this in the spell section or something.
requires: vJASS (but i can make a regular jass version if you want)
JASS:
scope spacebarDetect initializer Init

    globals
        private real spacebarX = 0.
        private real spacebarY = 0.
        private real camX = 0
        private real camY = 0
    endglobals

    private function Actions takes nothing returns nothing
        set camX = GetCameraTargetPositionX()
        set camY = GetCameraTargetPositionY()
        if camX == spacebarX and camY == spacebarY then
            call BJDebugMsg("SPACE BAR DETECTED!!!")
            set camX = camX-.1
            call SetCameraPosition(camX, camY)
        endif
        set spacebarX = camX+.1
        set spacebarY = camY
        call SetCameraQuickPosition(spacebarX, spacebarY)
    endfunction

    private function Init takes nothing returns nothing
        local trigger trig = CreateTrigger(  )
        call SetCameraQuickPosition(spacebarX, spacebarY)
        call TriggerRegisterTimerEvent( trig, 0.05, true )
        call TriggerAddAction( trig, function Actions )
    endfunction

endscope
this should work for any camera system, whether locking or not.
EDIT: does not work consistently if the camera is moving. Decreasing the time interval of the periodic event does not help to detect spacebar presses while camera is moving. Anyone have a solution?
 
Level 8
Joined
Aug 4, 2006
Messages
357
You do? Who got the idea. But I think it will be very buggy. May try script it when i come home
Haha, alright I admit it was your idea. Well, you're right; it is very buggy. It only detects spacebar if the camera is still and is not locked on a unit.

Big Dub: It seems there is no good work around. I suggest you use the arrow keys or a dummy ability hotkey to get the event you want. It's much easier than trying to get the spacebar to work. I don't think players will care how big of a button it is. Sorry.
 
Status
Not open for further replies.
Top