Useful Functions

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.
Reply

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.
Reply

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
Reply

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;
}
Reply

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

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

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.
Reply

what's not working in m script?
Reply

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
Reply

-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;
}
Reply

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
Reply

@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).
Reply

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.
Reply

@[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."
Reply

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."
Reply

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

Ryder: that is a prefix for zero-terminated string
Reply

I get it, thanks
Reply

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...
Reply

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.).
Reply

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)
Reply


Forum Jump:


Users browsing this thread: 33 Guest(s)