SA-MP Forums Archive
[Tutorial] To simple check if the player swims - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] To simple check if the player swims (/showthread.php?tid=498998)



To simple check if the player swims - CrazyFrenzy - 06.03.2014

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.


Re: To simple check if the player swims - GalaxyHostFree - 06.03.2014

Dear this isn't really a tutorial. You must explain them line by line.


Re: To simple check if the player swims - CrazyFrenzy - 06.03.2014

Quote:
Originally Posted by GalaxyHostFree
View Post
Dear this isn't really a tutorial. You must explain them line by line.
You're right buddy, I managed to adjust it a little.


Re: To simple check if the player swims - GalaxyHostFree - 06.03.2014

Quote:
Originally Posted by CrazyFrenzy
View Post
You're right buddy, I managed to adjust it a little.
Good job! Now I can learn something from your script. Looking forward to your new tutorials soon!