Question - OnPlayerKeyStateChange
#1

Hello there !
I was testing a code in the callback OnPlayerKeyStateChange (NOTE: i have read the wiki page about that callback) and my code was working well but i have checked a thread/post were the code i was using was wrote in other way,check this -

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
   if ((oldkeys & KEY_SPRINT) && !(newkeys & KEY_SPRINT))
   {
      // My Code
   }
   if ((newkeys & KEY_SPRINT) && !(oldkeys & KEY_SPRINT))
   {
      // My Code
   }
   return true;
}
Alright - That is my normal code that i have made thanks to the wiki information but i have noticed there was other way (in some random thread in the forum)- check below

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
   if(oldkeys - newkeys == KEY_SPRINT)
   {
      // My code
   }
   if(newkeys - oldkeys == KEY_SPRINT)
   {
      // My code
   }
   return true;
}
Both codes/way do the same but i want to know why the second way is not in the wiki if it does the same function and i want to understand that code because as far i know "-" is not an operator ( oldkeys - newkeys == KEY_SPRINT)
Maybe some of you will say "don't worry about it if it's working" but i do want to know.
Reply
#2

The second one shouldn't even work, and the second one is not in the wiki, because I dont see a use for it.

In the most ( almost always ) you have to use & , so when player is runing by lets say holding a W key, server can still check if he have pressed Space bar or any other GTA defined key.
Reply
#3

oldkeys and newkeys are "A map (bitmask) of the keys held "
the "-" is not the right operator to work with bitmasks, hence second code does have situations that won't work (off the top of my head if they release 2 keys at same time it won't detect any of them getting released, not sure if thats the actual case but you get the idea)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)