Detecting a key being held
#1

I have this defined (taken from SA-MP Wiki)

Код:
#define HOLDING(%0) \
	((newkeys & (%0)) == (%0))
and this is the code I use on OnPlayerKeyStateChange

Код:
if(HOLDING(KEY_SPRINT))
{
	SendClientMessage(playerid, -1, "HOLDING SPRINT");
	
	if(pInfo[playerid][ApplyingPressure] == 1)
	{
		SendClientMessage(playerid, -1, "Holding sprint");
	}
}
In game it sends it only once, any idea why?
Reply
#2

Because OnPlayerKeyStateChange is only called when a key is changed. If you press sprint OnPlayerKeyStateChange will be called, but it won't be called again until you release sprint OR you press another key in addition to sprint. Only the latter one will trigger HOLDING.

You will likely want to use PRESSED and RELEASED to set and unset a variable and/or timer.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Because OnPlayerKeyStateChange is only called when a key is changed. If you press sprint OnPlayerKeyStateChange will be called, but it won't be called again until you release sprint OR you press another key in addition to sprint. Only the latter one will trigger HOLDING.

You will likely want to use PRESSED and RELEASED to set and unset a variable and/or timer.
Believe it or not, that's what I thought at first. So I moved my code to OnPlayerUpdate with GetPlayerKeys which still didn't work (before I found "HOLDING" definition on SA-MP Wiki) then I found that define and moved it back to OnPlayerKeyStateChange. *facepalm*

Thanks for the help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)