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.
Should I make a little snippet topic in the tutorial section in case someone want's to use this?
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'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("UP")
//! runtextmacro PressArrowFunc("DOWN")
//! runtextmacro PressArrowFunc("LEFT")
//! runtextmacro PressArrowFunc("RIGHT")
//! textmacro ReleaseArrowFunc takes KEY
private function ReleaseArrow$KEY$ takes nothing returns nothing
set $KEY$[GetPlayerId(GetEventDetectingPlayer())] = false
endfunction
//! endtextmacro
//! runtextmacro ReleaseArrowFunc("UP")
//! runtextmacro ReleaseArrowFunc("DOWN")
//! runtextmacro ReleaseArrowFunc("LEFT")
//! runtextmacro ReleaseArrowFunc("RIGHT")
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?