[Include] Limex's Swimming Include (Detect swimming!)
#1

Limex's Swimming Include


Features 1 function, and 2 callbacks!

New function: IsPlayerSwimming(playerid)

New callbacks: OnPlayerStartSwimming(playerid) and OnPlayerStopSwimming(playerid)

HOW TO USE

Make a limex-swim.inc file in your includes folder!

Put the following code in it:

pawn Code:
/*  =============================

    Limex's Swimming Include
   
    Made by Limex / A
   
    New function:
    IsPlayerSwimming(playerid)
   
    New callbacks:
    OnPlayerStartSwimming(playerid)
    OnPlayerStopSwimming(playerid)
   
    Enjoy!

    ============================= */


#include <a_samp>

new bool:swimming[MAX_PLAYERS];

forward OnPlayerStartSwimming(playerid);
forward OnPlayerStopSwimming(playerid);
forward IsPlayerSwimming(playerid);

public IsPlayerSwimming(playerid)
{
    if(swimming[playerid]) return 1;
    return 0;
}

public OnPlayerUpdate(playerid)
{
    if(GetPlayerAnimationIndex(playerid))
    {
        new animlib[32];
        new animname[32];
        GetAnimationName(GetPlayerAnimationIndex(playerid),animlib,32,animname,32);
        if(strcmp(animlib, "SWIM", true) == 0 && !swimming[playerid])
        {
            swimming[playerid] = true;
            OnPlayerStartSwimming(playerid);
        }
        else if(strcmp(animlib, "SWIM", true) != 0 && swimming[playerid] && strfind(animname, "jump", true) == -1)
        {
            swimming[playerid] = false;
            OnPlayerStopSwimming(playerid);
        }
    }
    else if(swimming[playerid])
    {
        swimming[playerid] = false;
        OnPlayerStopSwimming(playerid);
    }
    return 1;
}
Include it into your script with:

pawn Code:
#include <limex-swim>
You must add the following two callbacks into your script!

pawn Code:
public OnPlayerStartSwimming(playerid)
{
    // EXAMPLE CODE START //
    SendClientMessage(playerid, 0x33CCFFAA, "You started swimming!");
    // EXAMPLE CODE END //
    return 1;
}

public OnPlayerStopSwimming(playerid)
{
    // EXAMPLE CODE START //
    SendClientMessage(playerid, 0x33CCFFAA, "You stopped swimming!");
    // EXAMPLE CODE END //
    return 1;
}
Reply
#2

Just wow... Never thought anyone would actually attempt to make a swimming detector... Nice job with this

EDIT: I heard something about OnPlayerUpdate causing lots of lag or something like that so I wouldn't use it.
Reply
#3

another cool but random release :P

This forum requires that you wait 120 seconds between posts. Please try again in 28 seconds.
GTFO.
Reply
#4

Quote:
Originally Posted by willsuckformoney
View Post
Just wow... Never thought anyone would actually attempt to make a swimming detector... Nice job with this

EDIT: I heard something about OnPlayerUpdate causing lots of lag or something like that so I wouldn't use it.
With the code he has in it, no, it won't lag.
Reply
#5

Quote:
Originally Posted by Grim_
View Post
With the code he has in it, no, it won't lag.
pawn Code:
if(GetPlayerAnimationIndex(playerid))
    {
        new animlib[32];
        new animname[32];
        GetAnimationName(GetPlayerAnimationIndex(playerid),animlib,32,animname,32);
        if(strcmp(animlib, "SWIM", true) == 0 && !swimming[playerid])
        {
            swimming[playerid] = true;
            OnPlayerStartSwimming(playerid);
        }
        else if(strcmp(animlib, "SWIM", true) != 0 && swimming[playerid] && strfind(animname, "jump", true) == -1)
        {
            swimming[playerid] = false;
            OnPlayerStopSwimming(playerid);
        }
    }
    else if(swimming[playerid])
    {
        swimming[playerid] = false;
        OnPlayerStopSwimming(playerid);
    }
Wont lag with 100 players on? OPD gets called like 30 times a second.. if not more.


now 100 * 30 = 3k, 3k * 60 = 180,000 Times a minute. Would be better to just put in a 1 second timer..
Reply
#6

Nice job. Most of the things this will be used for won't require something as frequent as OnPlayerUpdate; even a 1000ms timer will be fine for everything, I'd go for 2000.
Reply
#7

I'm not a scripter or something,but I can say it's very useful (even tho I don't know how/does works) for RP servers,to prevent water-evaders,etc. :P
Reply
#8

Nice, another good release that makes use of 0.3b. Can be useful e.g. for RP (of course) or racing scripts.

Small tip: Use CallLocalFunction for calling your custom callbacks (OnPlayerStart/StopSwimming), instead of calling them direct. YOu do not have to have the function in your script then, but if you have it, it gets called.
Reply
#9

Thanks for all the nice comments guys!

Yes I guess it would be fine with a 1 second timer, although the timer would be required to loop through ALL players, whereas OnPlayerUpdate is only called for online players. I don't think there would be that much gain from it. Especially as I am not using any loops. However, it would be suitable to stagger the amount that the full code is executed, for example, only being called every 10 updates or so.
Reply
#10

You can also easily detect swimming

pawn Код:
stock IsPlayerInWater(playerid)
{
    new animlib[32],tmp[32];
    GetAnimationName(GetPlayerAnimationIndex(playerid),animlib,32,tmp,32);
    if( !strcmp(animlib, "SWIM") && !IsPlayerInAnyVehicle(playerid) ) return true;
    return false;
}
this is simply faster without lags because it simply stocks it in.
Reply
#11

Well the main idea was that this is a callback, not a function

You can't really do as much stuff as you can with the callback.
Reply
#12

It's more a function than a include, so, you're using OnPlayerUpdate and it laggggs...

BTW: Nice ...
Reply
#13

Quote:
Originally Posted by MrDeath
Посмотреть сообщение
It's more a function than a include, so, you're using OnPlayerUpdate and it laggggs...

BTW: Nice ...
Quote:
Originally Posted by Grim_
Посмотреть сообщение
With the code he has in it, no, it won't lag.
That right there
Reply
#14

Why do people get so huffed up about OnPlayerUpdate lol. It really isn't as bad as people make out. My code does not have any loops in it, so it is fine.
Reply
#15

lol well today the server I play in uses OnPlayerUpdate and has EPIC lag.
Reply
#16

Quote:
Originally Posted by willsuckformoney
Посмотреть сообщение
lol well today the server I play in uses OnPlayerUpdate and has EPIC lag.
If all functions on it would be set correctly, it wouldn't lag if all functions will be usefull. For this i would actually use a player loop.

I had a very laggy speedo, which was put on OnPlayerUpdate, and after ~40 players on the server stoped calling OnPlayerUpdate
Reply
#17

Any ideas for changing it or any feedback? Feel free to tell!
Reply
#18

Quote:
Originally Posted by wups
Посмотреть сообщение
If all functions on it would be set correctly, it wouldn't lag if all functions will be usefull. For this i would actually use a player loop.

I had a very laggy speedo, which was put on OnPlayerUpdate, and after ~40 players on the server stoped calling OnPlayerUpdate
That's because you are checking the speed of the player/vehicle, and re-setting it into a textdraw multiple times a second, of course that would cause at least some lag with multiple users. Textdraws, I would think, would take more time to execute because it has to display something new on the screen while updating everything else (not to mention the destroying/hiding part, and re-showing).
Reply
#19

These are those times that people search more often for snippets (This is a sort of snippet). There are such great codes in the "Useful snippets/functions" topic, why don't people use them .

And like somewhere stated above, this is what OnPlayerUpdate is meant for: Simple checks, not saving accounts and stuff lol.
Reply
#20

OMG just awesome
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)