SA-MP Forums Archive
Useful Functions - 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: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+--- Thread: Useful Functions (/showthread.php?tid=38965)



Re: Useful Functions - RyDeR` - 28.01.2011

Quote:
Originally Posted by __
Посмотреть сообщение
How does the appearance of code have any relevance to coding, at all?

Tab-spacing is very useful for reading your own code and organizing it, but this isn't a fashion contest, it's quite the inverse.
You already got your answer.


Re: Useful Functions - __ - 29.01.2011

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
You already got your answer.
There's a difference between neat tab-spacing to read code and styling your code the same way a hairdresser would style hair.


Re: Useful Functions - Gh0sT_ - 29.01.2011

pawn Код:
stock Teleport( playerid, Float: X, Float: Y, Float: Z, time ) SetTimerEx( "TeleportPlayer", time, false, "dfff", playerid, X, Y, Z );
forward TeleportPlayer( playerid, Float: X, Float: Y, Float: Z );
public TeleportPlayer( playerid, Float: X, Float: Y, Float: Z )
{
    if( !IsPlayerInAnyVehicle( playerid ) ) SetPlayerPos( playerid, X, Y, Z );
    else SetVehiclePos( GetPlayerVehicleID( playerid ), X, Y, Z );
    return true;
}
Example:

Teleport( playerid, 0.0, 0.0, 0.0, 1000 );

Will set player coordinates to 0.0 in 1sec


stock AllowMinigunForPlayer(playerid, allow) - Sasino97 - 03.02.2011

ALLOW MINIGUN FOR PLAYER

Код:
new MinigunCheckTimer;

stock AllowMinigunForPlayer(playerid, allow)
{
    if(allow) return 1;
    MinigunCheckTimer = SetTimerEx("MinigunCheck",2000,1,"i",playerid);
    return 1;
}

public MinigunCheck(playerid)
{
    if(GetPlayerWeapon(playerid) != 38) return 1;
    BanEx(playerid,"Minigun Cheat");
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    KillTimer(MinigunCheckTimer);
    return 1;
}



Re: Useful Functions - dice7 - 03.02.2011

That will ban innocent players. Also thats a snippet, not a fucntion.

Edit; Actually, it wont even work with more then 1 player


Re: stock AllowMinigunForPlayer(playerid, allow) - California - 03.02.2011

Quote:
Originally Posted by [GF]Sasino97
Посмотреть сообщение
ALLOW MINIGUN FOR PLAYER

Код:
new MinigunCheckTimer;

stock AllowMinigunForPlayer(playerid, allow)
{
    if(allow) return 1;
    MinigunCheckTimer = SetTimerEx("MinigunCheck",2000,1,"i",playerid);
    return 1;
}

public MinigunCheck(playerid)
{
    if(GetPlayerWeapon(playerid) != 38) return 1;
    BanEx(playerid,"Minigun Cheat");
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    KillTimer(MinigunCheckTimer);
    return 1;
}
The most simple, and yet, bugged script you could ever find on this thread.


Re: Useful Functions - Sasino97 - 04.02.2011

what's not working in m script?


Re: Useful Functions - Kwarde - 04.02.2011

Alternative version of "IsNumeric"

pawn Код:
stock IsNumeric(string[])
{
    for(new i = 0, length = strlen(string), check; i < length; i++){
        switch(string[i]){
            case '0' .. '9': check ++;
        }
        if(check == length) return true;
    }
    return false;
}
How does it work:
It loops through the whole 'string' length. If string length 'i' (per character) is numeric, it increases 'check' with 1.
If 'check' is the same as the string length, it will return true.

It's easy :P


Re: Useful Functions - Slice - 04.02.2011

-1 is not numeric!



Also, why would you keep looping if you find an invalid number?

Proper version would be (I stole this from myself):
pawn Код:
stock bool:IsValidNumber( const szString[ ] )
{
    if ( !szString[ 0 ] )
        return false;
   
    new
        iLength = strlen( szString ),
        i
    ;
   
    if ( szString[ 0 ] == '-' && szString[ 1 ] )
        i = 1;
   
    for ( ; i < iLength; i++ )
    {
        if ( !( '0' <= szString[ i ] <= '9' ) )
            return false;
    }
   
    return true;
}



AW: Useful Functions - Nero_3D - 04.02.2011

Another version

pawn Код:
stock bool:IsNumeric(const string[])
{
    if(string[0] != EOS) {
        for(new i = -1; ; ) {
            switch(string[++i]) {
                case '+', '-', '.', '0'..'9': continue;
                case EOS: return true;
                default: return false;
            }
        }
    }
    return false;
}
I know that +-.+ would be valid
But I think that + should be included or . for floats


Re: Useful Functions - Slice - 04.02.2011

@Nero_3D: You forgot to make sure the +/- signs are in the beginning, and there can be several dots.
@******: Something equivalent to the JavaScript parseInt function would be cool (since strval isn't capable of dealing with any of the numbers you wrote).


Respuesta: Useful Functions - ipsBruno - 04.02.2011

pawn Код:
stock IsValidNumber( const szString[ ] )
{
    new i;
    if ( szString[0] == '-')    i = 1;
    while (szString[++i]) if(!('0' <= szString[++i] <= '9')) return false;
    return true;
}
I not know if work.


Re: Useful Functions - Slice - 05.02.2011

@[FeK]DraKiNs: It skips the first character, doesn't check for null, and it's slower.

I don't see why you'd change a fully working function!
"If it ain't broke, don't fix it."


Re: Useful Functions - [03]Garsino - 05.02.2011

Quote:
Originally Posted by Slice
Посмотреть сообщение
"If it ain't broke, don't fix it."
"If you don't fix what's not broken, there's no room for improvement."


Re: Useful Functions - RyDeR` - 05.02.2011

Just curious, what's the meaning of the 'sz' in front of ur (string) variable?


Re: Useful Functions - Luka P. - 05.02.2011

Ryder: that is a prefix for zero-terminated string


Re: Useful Functions - RyDeR` - 05.02.2011

I get it, thanks


Re: Useful Functions - Sasino97 - 05.02.2011

Quote:

That will ban innocent players. Also thats a snippet, not a fucntion.

Edit; Actually, it wont even work with more then 1 player

What's wrong? Doesn't it works? I haven't tested it yet, but i was sure it works.

Sorry for my english...


Re: Useful Functions - [03]Garsino - 05.02.2011

Quote:
Originally Posted by [GF]Sasino97
Посмотреть сообщение
What's wrong? Doesn't it works? I haven't tested it yet, but i was sure it works.

Sorry for my english...
You're using a global variable for the timer and not a global player variable, hence why it would only work for the last player you started the timer on (the killtimer part, etc.).


Re: Useful Functions - Steamator - 06.02.2011

pawn Код:
stock PutPlayerInVehicleEx(playerid, vehicleid, seatid)
{
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        CallRemoteFunction("OnPlayerStateChange", "iii", playerid, PLAYER_STATE_ONFOOT, PLAYER_STATE_DRIVER);
        PutPlayerInVehicle(playerid, vehicleid, seatid);
        CallRemoteFunction("OnPlayerStateChange", "iii", playerid, PLAYER_STATE_DRIVER, PLAYER_STATE_ONFOOT);
        return 1;
    }
    else if(GetPlayerState(playerid) == PLAYER_STATE_PASSENGER)
    {
        CallRemoteFunction("OnPlayerStateChange", "iii", playerid, PLAYER_STATE_ONFOOT, PLAYER_STATE_PASSENGER);
        PutPlayerInVehicle(playerid, vehicleid, seatid);
        CallRemoteFunction("OnPlayerStateChange", "iii", playerid, PLAYER_STATE_PASSENGER, PLAYER_STATE_ONFOOT);
        return 1;
    }
    else return PutPlayerInVehicle(playerid, vehicleid, seatid);
}
This Function can maybe fix some erros/bugs, when player is getting ported from one to the another car.
(Won't work, if PutPlayerInVehicle will be called in another method)