[Tutorial] To simple check if the player swims
#1

Check if the player swims


Step 1
Make sure to have the a_samp include and those definitions in the top of your script.
PHP Code:
#include <a_samp>
#define PLAYER_SWIM     (1)
#define PLAYER_WALK     (2) 
These definitions are here to replace numbers to make it look badass.


Step 2
Defining varaibles and callbacks.
PHP Code:
new animName[32], animLib[32], Float:playerGPos[MAX_PLAYERS][3];
forward OnPlayerSwim(playeridstatus); 
This will help us further.

Step 3
Checking the status of a player whether is swimming or not, in order to do so you have to use the public OnPlayerUpdate, yet it is not preferable and you sure want to use a timer instead.

PHP Code:
public OnPlayerUpdate(playerid)
{
    
GetAnimationName(GetPlayerAnimationIndex(playerid), animLibsizeof(animLib), animNamesizeof(animName));
    
GetPlayerPos(playeridplayerGPos[playerid][0], playerGPos[playerid][1], playerGPos[playerid][2]);
    if(
strcmp(animLib"SWIM"false) || playerGPos[playerid][2] <= 1.5)
        
OnPlayerSwim(playeridPLAYER_SWIM);
    else
        
OnPlayerSwim(playeridPLAYER_WALK);
    return 
1;

Explainition of each and every line:
PHP Code:
GetAnimationName(GetPlayerAnimationIndex(playerid), animLibsizeof(animLib), animNamesizeof(animName)); 
It's basically used to reterive the anim libary and name into varaibles so I can make a comparsion.

PHP Code:
GetPlayerPos(playeridplayerGPos[playerid][0], playerGPos[playerid][1], playerGPos[playerid][2]); 
Gets the player position in XYZ coordinates, stores it into 3 Float varaibles.

PHP Code:
if(strcmp(animLib"SWIM"false) || playerGPos[playerid][2] <= 1.5
Compare if the anim libary name is SWIM which is basically called when a player is swimming or is underwater.

PHP Code:
OnPlayerSwim(playeridPLAYER_SWIM); 
If the player swims or is underwater, it returns the player swim within the OnPlayerSwim callback.

PHP Code:
OnPlayerSwim(playeridPLAYER_WALK); 
If the player does not swim, it returns the player walk within the OnPlayerSwim callback.

Step 4
Setting up the OnPlayerSwim callback.

You can use whether switch or elses, it doesn't really matter just a matter of comfort.


PHP Code:
public OnPlayerSwim(playeridstatus)
{
    switch(
status)
    {
        case 
PLAYER_SWIM:
        {
            
// SWIM action
        
}
        case 
PLAYER_WALK:
        {
             
// WALK Action
        
}
    }
    return 
1;

SWIM Action
What will happen to the player that swims, you can use any script code you'd like.

WALK Action
The same thing, just if the player walks.
Reply


Messages In This Thread
To simple check if the player swims - by CrazyFrenzy - 06.03.2014, 04:14
Re: To simple check if the player swims - by GalaxyHostFree - 06.03.2014, 04:28
Re: To simple check if the player swims - by CrazyFrenzy - 06.03.2014, 04:37
Re: To simple check if the player swims - by GalaxyHostFree - 06.03.2014, 04:54

Forum Jump:


Users browsing this thread: 1 Guest(s)