Optimizing arrow press control

Prozix

New Member
Reaction score
7
There seems to be a little delay when pressing the arrow keys, I would like to know if there is a way to optimize the code I wrote.

JASS:
scope ArrowControl initializer Init
    //I don't really know how to write a library and I don't know if this is worth it, that is why this is written in a scope. 
    //what does it do?
    //  you can use the variables UP, DOWN etc. in a conditional branch to do something is a key is held
    //it does this for the player numbers 1 - MAXPLAYERS but you can change it if you want to do it for specific player numbers only which I doubt. 
    //I wish credits wouldn&#039;t go to me, because I think this could have been written a lot better (nicer to look at) and maybe even more efficient, but they do <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile    :)" loading="lazy" data-shortname=":)" /> 
    globals
        private constant integer MAXPLAYERS = 4
        boolean array UP
        boolean array DOWN
        boolean array LEFT
        boolean array RIGHT
    endglobals
    //! textmacro PressArrowFunc takes KEY
    private function PressArrow$KEY$ takes nothing returns nothing
        set $KEY$[GetPlayerId(GetEventDetectingPlayer())] = true
    endfunction
    //! endtextmacro
    //! runtextmacro PressArrowFunc(&quot;UP&quot;)
    //! runtextmacro PressArrowFunc(&quot;DOWN&quot;)
    //! runtextmacro PressArrowFunc(&quot;LEFT&quot;)
    //! runtextmacro PressArrowFunc(&quot;RIGHT&quot;)
    
    //! textmacro ReleaseArrowFunc takes KEY
    private function ReleaseArrow$KEY$ takes nothing returns nothing
        set $KEY$[GetPlayerId(GetEventDetectingPlayer())] = false
    endfunction
    //! endtextmacro
    //! runtextmacro ReleaseArrowFunc(&quot;UP&quot;)
    //! runtextmacro ReleaseArrowFunc(&quot;DOWN&quot;)
    //! runtextmacro ReleaseArrowFunc(&quot;LEFT&quot;)
    //! runtextmacro ReleaseArrowFunc(&quot;RIGHT&quot;)
    
    private function Init takes nothing returns nothing
        local integer i = 0
        local trigger PressUP = CreateTrigger()
        local trigger PressDOWN = CreateTrigger()
        local trigger PressLEFT = CreateTrigger()
        local trigger PressRIGHT = CreateTrigger()
        
        local trigger ReleaseUP = CreateTrigger()
        local trigger ReleaseDOWN = CreateTrigger()
        local trigger ReleaseLEFT = CreateTrigger()
        local trigger ReleaseRIGHT = CreateTrigger()
        loop
            call TriggerRegisterPlayerKeyEventBJ(PressUP, Player(i), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_UP)
            call TriggerRegisterPlayerKeyEventBJ(PressDOWN, Player(i), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_DOWN)
            call TriggerRegisterPlayerKeyEventBJ(PressLEFT, Player(i), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_LEFT)
            call TriggerRegisterPlayerKeyEventBJ(PressRIGHT, Player(i), bj_KEYEVENTTYPE_DEPRESS, bj_KEYEVENTKEY_RIGHT)
            
            call TriggerRegisterPlayerKeyEventBJ(ReleaseUP, Player(i), bj_KEYEVENTTYPE_RELEASE, bj_KEYEVENTKEY_UP)
            call TriggerRegisterPlayerKeyEventBJ(ReleaseDOWN, Player(i), bj_KEYEVENTTYPE_RELEASE, bj_KEYEVENTKEY_DOWN)
            call TriggerRegisterPlayerKeyEventBJ(ReleaseLEFT, Player(i), bj_KEYEVENTTYPE_RELEASE, bj_KEYEVENTKEY_LEFT)
            call TriggerRegisterPlayerKeyEventBJ(ReleaseRIGHT, Player(i), bj_KEYEVENTTYPE_RELEASE, bj_KEYEVENTKEY_RIGHT)
            set i = i + 1
            exitwhen i == MAXPLAYERS
        endloop
        call TriggerAddAction(PressUP, function PressArrowUP)
        call TriggerAddAction(PressDOWN, function PressArrowDOWN)
        call TriggerAddAction(PressLEFT, function PressArrowLEFT)
        call TriggerAddAction(PressRIGHT, function PressArrowRIGHT)
        
        call TriggerAddAction(ReleaseUP, function ReleaseArrowUP)
        call TriggerAddAction(ReleaseDOWN, function ReleaseArrowDOWN)
        call TriggerAddAction(ReleaseLEFT, function ReleaseArrowLEFT)
        call TriggerAddAction(ReleaseRIGHT, function ReleaseArrowRIGHT)
    endfunction
endscope


Should I make a little snippet topic in the tutorial section in case someone want's to use this?
 

Nestharus

o-o
Reaction score
84
No way to get rid of the delay unless you make it local by comparing current window target location with previous window target location.

This is something I showed one of my friend's once and now he's desperate to try and get the 0 delay local arrow keys sync'd in multi player. Local arrow keys are very useful in single player games though ^_-.
 

Prozix

New Member
Reaction score
7
How would I do this? I don't really understand the window target location part... Have never heard of windows in Warcraft Jass.
 

Prozix

New Member
Reaction score
7
But if you can change the angle of the camera in your map, both the X and Y can change by pressing the left arrow key for example. I wonder if doing what you are suggesting really is faster then detecting the presses trough the trigger event. Thanks anyway for your replies :)
 

SerraAvenger

Cuz I can
Reaction score
234
But if you can change the angle of the camera in your map, both the X and Y can change by pressing the left arrow key for example. I wonder if doing what you are suggesting really is faster then detecting the presses trough the trigger event. Thanks anyway for your replies :)

Faster != less delayed.
The delay is on a low software level that we cannot reach. It is part of the underlying software architecture (Wc3). Using Nesthaurus' way might need more CPU cycles, but will not be delayed since the change in cameraposition will happen instantly, while the JASS event listeners will fire later.
 

ZugZugZealot

New Member
Reaction score
33
-FROM-

-TO-


It will only speed things up on the initialization end. Normally I wouldn't be elitest about such a thing, but since it's a snippet, I'd bring up the BJ reduction.
 

Prozix

New Member
Reaction score
7
Faster != less delayed.
The delay is on a low software level that we cannot reach. It is part of the underlying software architecture (Wc3). Using Nesthaurus' way might need more CPU cycles, but will not be delayed since the change in cameraposition will happen instantly, while the JASS event listeners will fire later.
Understood

and

aha thank you ZugZugZealot.

Too bad my game is multi player and the players can change their camera angle...
 

Prozix

New Member
Reaction score
7
Guess i'll have to explain.

Imagine you press the left arrow key, what happens is that the camera location is set to another position. Because the camera angle is the default (I think it would be 270 degrees) angle, only the X coordinate will change:
Code:
cos(camera angle-270) = x/distance --> cos(270-270) = 1 so distance = x
sin(camera angle-270) = y/distance --> sin(270-270) = 0 so y = 0
We could use that to register a keypress without doing fancy computer intensive calculations.

When the camera angle is turned clockwise by 45 degrees, pressing the left arrow key changes the x coordinate by
Code:
distance * cos(camera angle-270) = x = 0.707106781 * distance
distance * sin(camera angle-270) = y = 0.707106781 * distance

using something like:
JASS:
if xLast &gt; xNew then
    set LEFT[playerId] = true
    set RIGHT[playerId] = false
elseif xLast &lt; xNew then
    set LEFT[playerId] = false
    set RIGHT[playerId] = true
elseif xLast == xNew then
    set LEFT[playerId] = false
    set RIGHT[playerId] = false
endif

will not work properly anymore.

(hope i didn't misunderstood what you were trying to say :banghead:)
 

SerraAvenger

Cuz I can
Reaction score
234
Are you sure about this?
I have no wc3 here so I cannot test it, but afai remeber the camera will still move alongside x and y axis, instead of to the "relative" left/right/up/down (relative to the camera angle).
 

Prozix

New Member
Reaction score
7
Are you sure about this?
I have no wc3 here so I cannot test it, but afai remeber the camera will still move alongside x and y axis, instead of to the "relative" left/right/up/down (relative to the camera angle).

Hmmp good question, I don't know but it would be a little confusing if your camera angle is rotated (trough triggers) by 180 degrees. Pressing the arrows would do the exact inverse of what you are used to:eek:

I've got to go now, maybe i'll test it later.
 

Prozix

New Member
Reaction score
7
So.. i tested it xD. And yes: rotating the camera doesn't take away the ability to move it with the arrow keys in the appropriate directions.

I also came to the conclusion that detecting arrow presses with x/y coordinates can't be done when you lock the position of the camera to a unit. You need the camera to be moved with the keys to check if you have pressed the keys. But when the camera is locked...

So, if you want to check for keypresses without locking and/or rotating the camera: this works very nicely
JASS:

scope ArrowKeys initializer Init
struct KEY
    integer UP
    integer DOWN
    integer LEFT
    integer RIGHT
endstruct

struct CAMINFO
    real x
    real y
    static method create takes nothing returns thistype
        local thistype s = thistype.allocate() 
        set s.x = GetCameraTargetPositionX()
        set s.y = GetCameraTargetPositionY()
        return s
    endmethod
endstruct

globals
    private constant integer NPLAYERS = 12
    KEY array key[NPLAYERS]
    CAMINFO array caminfo[NPLAYERS]
endglobals
//set angle = GetCameraField(CAMERA_FIELD_ROTATION)

//two dimensional
private function Actions takes nothing returns nothing
    local integer i = 0
    local real newx
    local real newy

    loop
        if GetLocalPlayer() == Player(i) then
            //x: left and right
            set newx = GetCameraTargetPositionX()
            if newx &lt; caminfo<i>.x then
                set key<i>.LEFT = 1
            elseif newx &gt; caminfo<i>.x then
                set key<i>.RIGHT = 1
            else
                set key<i>.LEFT = 0
                set key<i>.RIGHT = 0
            endif
            //y: up and down
            set newy = GetCameraTargetPositionY()
            if newy &lt; caminfo<i>.y then
                set key<i>.DOWN = 1
            elseif newy &gt; caminfo<i>.y then
                set key<i>.UP = 1
            else
                set key<i>.DOWN = 0
                set key<i>.UP  = 0
            endif

            set caminfo<i>.x = newx
            set caminfo<i>.y = newy
            
            call ClearTextMessages()
            call BJDebugMsg(I2S(key<i>.UP) + &quot; &quot; + I2S(key<i>.DOWN) + &quot; &quot; + I2S(key<i>.LEFT) + &quot; &quot; + I2S(key<i>.RIGHT))
        endif
        set i = i + 1
        exitwhen i&gt;=NPLAYERS
    endloop
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i
    call TriggerRegisterTimerEvent(t, .03, true)
    call TriggerAddAction(t, function Actions)
    
    set i = 0
    loop
        set key<i> = key<i>.create()
        set caminfo<i> = caminfo<i>.create()
        set i = i+1
        exitwhen i&gt;=NPLAYERS
    endloop
endfunction
endscope
</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>


Anyway, if anyone else knows a method to register arrow key events accurately. Please share it with us ^^
 

quraji

zap
Reaction score
144
Unfortunately this method will only work with one player. [ljass]GetCameraTargetPositionX()[/ljass] and [ljass]GetCameraTargetPositionY()[/ljass] are asynchronous (they return different values for each player). They way you are using it will cause a desync (setting a global to a different value for each player).
 

Nestharus

o-o
Reaction score
84
Unfortunately this method will only work with one player. [ljass]GetCameraTargetPositionX()[/ljass] and [ljass]GetCameraTargetPositionY()[/ljass] are asynchronous (they return different values for each player). They way you are using it will cause a desync (setting a global to a different value for each player).

Unfortunately, we all already know that and it was covered at the very start of the thread ; )

Furthermore, this wouldn't desync.

Although, you should do
[ljass]call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "..."))[/ljass]

instead of BJDebugMsg

[ljass]call TriggerAddAction(t, function Actions)[/ljass]

should be a condition
 

quraji

zap
Reaction score
144
>Unfortunately, we all already know that and it was covered at the very start of the thread ; )

Okay, didn't feel like reading the whole thing.

>Furthermore, this wouldn't desync.

:confused:


>Although, you should do
>[ljass]call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "..."))[/ljass]
>instead of BJDebugMsg

Who cares? I think it's a test script...if it were a real one it wouldn't be there or would at least be [ljass]debug[/ljass].

>[ljass]call TriggerAddAction(t, function Actions)[/ljass]
>should be a condition

Nothing wrong with an action..
 

Nestharus

o-o
Reaction score
84
If you actually read the JASS code in the test script, you'd understand why it wouldn't desync -.-. He doesn't actually place any sync req'd handles or manipulate them based on the desync'd data ..

I'm the one who actually pointed out the method he's currently using : P

No way to get rid of the delay unless you make it local by comparing current window target location with previous window target location.

This is something I showed one of my friend's once and now he's desperate to try and get the 0 delay local arrow keys sync'd in multi player. Local arrow keys are very useful in single player games though ^_-.

JASS:

constant native GetCameraTargetPositionX    takes nothing returns real
constant native GetCameraTargetPositionY    takes nothing returns real
constant native GetCameraTargetPositionZ    takes nothing returns real
 

quraji

zap
Reaction score
144
If you actually read the JASS code in the test script, you'd understand why it wouldn't desync -.-. He doesn't actually place any sync req'd handles or manipulate them based on the desync'd data

Fair enough, it won't desync. But I don't think it will work..

It's not a good method anyways because you can't detect key ups accurately, and it will break if you press two opposing keys at once, or if the camera is moved in another way (with mouse, or by clicking map, or by code).
 

Prozix

New Member
Reaction score
7
Question:
If I set globals for local players, I can still perform local actions with the globals without getting a desync right?

This method's only use in a multiplayer map would be if you want to tell a player that he changed the camera position :banghead:

Question2:
Is there a way to sync data?
 

Viikuna

No Marlo no game.
Reaction score
265
Question2:
Is there a way to sync data?

Yes, but its not instant, its actually pretty slow. You can use gamecache natives with either some list and timer, or some SyncReady native ( or whatever its called ), or maybe do some selection syncing. Its just that thanks to net traffic it will always take time and always be really damn slow.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top