01.02.2014, 22:47
(
Последний раз редактировалось Abagail; 04.06.2016 в 17:31.
)
Key Presses!
Tutorial by: Abagail
https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange
Edit: This is quite an old, albeit functional, tutorial. I have taken a few minutes to revisit and revise this tutorial, hopefully it clears any grammatical errors.
Before continuing with anything else, we need to let the compiler know that we are including an include file. In this case, we only need the default SA-MP include.
The following defines allow you to detect if a key is being pressed, held, or if it has just been released. Note that you can only detect the keys listed here.
// The key has been pressed.
// The key has been released.
// HOLDING(keys)
#define HOLDING(%0) \
((newkeys & (%0)) == (%0))
// The key is being held down.
We will be interacting with key presses through the OnPlayerKeyStateChange callback. This callback is called when the player uses one of the available detection keys(not any other). It can be called quite frequently, so avoid placing unconditional code.
The list of available keys and their defined macros are available below.
here.
For this example, we will be giving the player NOS upon them pressing the fire key(defined as KEY_FIRE). If we want to give the player nitro when they are holding the key, we can use the HOLDING macro as such:
If we want to give the player nitro after they press the key, we can use the PRESSED macro as such:
If we want to give the player nitro after they release the key, we can use the RELEASED macro as such:
If we put this together, we can put code in the underlying code block and proceed to do what we want.
Reference:
OnPlayerKeyStateChange - SAMP Wiki
Detectable keys - SAMP Wiki
Tutorial by: Abagail
https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange
Edit: This is quite an old, albeit functional, tutorial. I have taken a few minutes to revisit and revise this tutorial, hopefully it clears any grammatical errors.
Before continuing with anything else, we need to let the compiler know that we are including an include file. In this case, we only need the default SA-MP include.
Код:
#include a_samp
Код:
#define PRESSED(%0) \ (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
Код:
#define RELEASED(%0) \ (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
// HOLDING(keys)
#define HOLDING(%0) \
((newkeys & (%0)) == (%0))
// The key is being held down.
We will be interacting with key presses through the OnPlayerKeyStateChange callback. This callback is called when the player uses one of the available detection keys(not any other). It can be called quite frequently, so avoid placing unconditional code.
Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys) {
here.
For this example, we will be giving the player NOS upon them pressing the fire key(defined as KEY_FIRE). If we want to give the player nitro when they are holding the key, we can use the HOLDING macro as such:
Код:
if (HOLDING( KEY_FIRE )
Код:
if (PRESSED( KEY_FIRE )
Код:
if (RELEASED( KEY_FIRE )
Код:
if (PRESSED( KEY_FIRE ) { AddVehicleComponent(GetPlayerVehicleID(playerid),101); }
OnPlayerKeyStateChange - SAMP Wiki
Detectable keys - SAMP Wiki