Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[New G2O][Client-Side] bindKey
#1
*Introduction:

NOTE! You have to enable event onRender via enableEvent_Render function

Info: This script is stable and propably doesn't have any bugs.

Hello everyone! I've been working on this little framework called "bindKey".
It's is very similar to bindKeys from MTA (Multi Theft Auto) modification.
We can bind some function to specific key and it's key-state only on client-side.
I didn't add this as shared, because it would be useless, we can call some server function from client-side when player click some key.
There are four different key-states: ("Pressed","Down","Released","Both"). You can modify this script whatever you want,
i'm hoping it will be useful in some way.

License

Download

*List of functions:



void bindKey(int or array key, string state, func callback, [[optional: int delay, bool single_downKey ]])
void unbindKey(int or array key, [[optional: string state, func callback, int delay, bool single_downKey ]])



*List of key states: (they're blind to letter capitalization)



"Pressed" // This key state calls function only once, when we pressed the key.
"Down" // This key state calls function more than one time, when we click the key and hold it.
"Released" // This key state calls function only once, when we released the key.
"Both" // This key state calls function only once, but in two states, "Pressed" and "Released".



*Function (bindKey, unbindKey) parameters description:

Info: The more arguments you give in unbindKey function, the more precise you remove bound key(s).
NOTE! You can skip (set as null) only arguments: callback, delay, single_downKey in calling unbindKey function



// Required parameters for bindKey (can't be skipped as null)

int or array key // Key(s) array or id.
string state // The key(s) state (look below).
void callback // The function which will be called.

// Optional parameters for bindKey (only for "Down" state)

int delay // The delay of timer.
bool single_downKey /* If this argument is equal true (default is), it will kill every existing down key timer
(except this one) when player press and hold "Down" key.*/




*Examples:



*Basic key binding (Playing pee animation):

Squirrel Script
  1. bindKey(KEY_P,"Pressed",function()
  2. {
  3. switch(getPlayerAni(heroId))
  4. {
  5. case "S_RUN": // checking if player is doing nothing
  6. playAni(heroId,"T_STAND_2_PEE")
  7. setFreeze(heroId,true)
  8. break
  9.  
  10. case "S_PEE": // checking if player is peeing
  11. playAni(heroId,"T_PEE_2_STAND")
  12. setFreeze(heroId,false)
  13. break
  14. }
  15. })



*Same example but with holding key ("Both" state):

Squirrel Script
  1. local sw = false
  2.  
  3. bindKey(KEY_P,"Both",function()
  4. {
  5. switch(sw)
  6. {
  7. case false: // checking if player is holding the key
  8. playAni(heroId,"T_STAND_2_PEE")
  9. setFreeze(heroId,true)
  10. break
  11.  
  12. case true: // checking if player released the key
  13. playAni(heroId,"T_PEE_2_STAND")
  14. setFreeze(heroId,false)
  15. break
  16. }
  17.  
  18. sw = !sw
  19. })



*Basic unbind key:

Squirrel Script
  1. function testBindFunc()
  2. {
  3. print("This bind will be called only once!")
  4. unbindKey(KEY_Q,"Pressed",testBindFunc) // removing bind
  5. }
  6. bindKey(KEY_Q,"Pressed",testBindFunc)




*Basic binding timer key:

Squirrel Script
  1. local seconds = 0
  2.  
  3. bindKey(KEY_P,"Down",function()
  4. {
  5. seconds++
  6. print(seconds) // printing seconds in console (~)
  7. },1000,false) // this timer key won't kill the rest, if player is holding more than one key
  8.  
  9. bindKey(KEY_P,"Released",function()
  10. {
  11. seconds = 0 // cleaning seconds variable, when player release P key
  12. })


Reply
#2
Script has been updated. I've removed some repeated things in code.

Also i'm thinking to add fourth argument to bindKey function, which could define, if function will be called when we're holding our key more than one time, or only one time.
Reply
#3
Thread and script has been updated,

I've changed how "Pressed" and "Both" state works. Now "Pressed" state is called only once, when we click key (not hold), same thing in "Both". To add function which will be called more than one time when we are holding our key, use "Down" state.

Now in thread we have key-states description.
Reply
#4
Thread and script have been updated.

I've fixed errors which were caused by bad if syntax.
Reply
#5
Thread and script have been updated.

I've added new argument into functions: bindKey and unbindKey.

This argument is delay of timer, which is calling when you hold your key. NOTE that this script version is stable, but it's experimental. I'm testing this new feature, it's very possible, that i'll add something new in the future Wink. (Maybe possibility to kill every active key timer when we hold new key)
Reply
#6
Thread and script have been updated.

-Old thread has been completely rewritten.
-Script has been optimalized, and added the fitth argument to bindKey and unbindKey functions which is "single_downKey".
Reply
#7
Script has been updated.

-From now on script (down state) doesn't use timers, but onRender event, be sure to enable it!
-Removed instant call in down state, first call is after some delay

I think that this script (if it's free from bugs) is finished.
Maybe it's not the most optimal, but it's not that bad either.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Client] Version check Quarchodron 2 4,008 25.08.2019, 14:06
Last Post: Quarchodron
  [Client] GUI Creator Quarchodron 6 7,512 12.08.2019, 23:05
Last Post: Arkhan
  [New G2O][Shared] Command Handler Patrix 3 6,168 05.09.2018, 15:42
Last Post: Patrix
  [New G2O][Shared] packetListener Patrix 3 6,644 20.06.2018, 18:43
Last Post: Tommy
  [0.1.4.*] [client/server] Simple anty-cheat Profesores 8 10,241 20.03.2018, 19:16
Last Post: Patrix
  [0.1.3] [client/server] BotCreator HammelGammel 22 26,540 08.02.2018, 22:02
Last Post: HammelGammel
  [0.1.3] [client/server] Utility Scripts HammelGammel 1 5,249 27.01.2018, 14:03
Last Post: HammelGammel
  [G2O v.0.1.2][Server-Side] NPC Manager Quarchodron 16 17,470 21.01.2018, 19:59
Last Post: Quarchodron
  [New G2O][Client-Side] Key Combination Patrix 1 3,777 23.12.2017, 13:16
Last Post: Patrix
  [CLIENT]Draw(Line/Color) Tommy 0 3,113 19.12.2017, 04:23
Last Post: Tommy

Forum Jump:


Users browsing this thread: