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.
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/
  • The Helper The Helper:
    I think we need to add something to the bottom of the front page that shows the Headline News forum that has a link to go to the News Forum Index so people can see there is more news. Do you guys see what I am saying, lets say you read all the articles on the front page and you get to the end and it just ends, no kind of link for MOAR!
  • The Helper The Helper:
    Happy Wednesday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top