Hello There, Guest! Login or Register


Useful Functions/Snippets/Commands
#1
Like in the old SA:MP Forums, use this topic to post any useful functions/snippets/commands you may have!
Reply
#2
Lock + Unlock

Code:
    if (strcmp(cmdtext, "/lock", true)==0)
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            new State=GetPlayerState(playerid);
            if(State!=PLAYER_STATE_DRIVER)
            {
                SendClientMessage(playerid,COLOR_RED,"You must be the driver to lock your vechicle!");
                return 1;
            }
            new i;
            for(i=0;i<MAX_PLAYERS;i++)
            {
                if(i != playerid)
                {
                    SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid),i, 0, 1);
                }
            }
            SendClientMessage(playerid, COLOR_ORANGE, "Vehicle locked!");
            new Float:pX, Float:pY, Float:pZ;
            GetPlayerPos(playerid,pX,pY,pZ);
            PlayerPlaySound(playerid,1056,pX,pY,pZ);
        }
        else
        {
            SendClientMessage(playerid, COLOR_RED, "You're not in a vehicle!");
        }
        return 1;
    }

    if (strcmp(cmdtext, "/unlock", true)==0)
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            new State=GetPlayerState(playerid);
            if(State!=PLAYER_STATE_DRIVER)
            {
                SendClientMessage(playerid,COLOR_RED,"You must be the driver of the vechicle to unlock!");
                return 1;
            }
            new i;
            for(i=0;i<MAX_PLAYERS;i++)
            {
                SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid),i, 0, 0);
            }
            SendClientMessage(playerid, COLOR_ORANGE, "Vehicle unlocked!");
            new Float:pX, Float:pY, Float:pZ;
            GetPlayerPos(playerid,pX,pY,pZ);
            PlayerPlaySound(playerid,1057,pX,pY,pZ);
        }
        else
        {
            SendClientMessage(playerid, COLOR_RED, "You're not in a vehicle!");
        }
        return 1;
    }

Vehicle [ Id ] ( Id's can be found on sa-mp wiki ) ( Do not type in a invalid id it will crash server, altough i could just do a invalid comment if id is incorrect ) ( rcon command )

Code:
    if(IsPlayerAdmin(playerid))
    {
        if(!strcmp(cmd, "/v", true))
        {
            new str[256], Float:x, Float:y, Float:z, id, tmp[256];
            tmp = strtok(cmdtext, idx);
            if(!strlen(tmp)) return SendClientMessage(playerid, COLOR_RED, "Correct Usage: /v [id]");
            id = strval(tmp);
            GetPlayerPos(playerid, x, y, z);
            CreateVehicle(id, x+2, y, z, 0, -1, -1, -1);
            format(str, 256, "Got vehicle: %d", id);
            SendClientMessage(playerid, COLOR_ORANGE, str);
            return true;
        }
    }

Reply
#3
[me=Dimplex]command :)[/me]

Code:
if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
    {
    if(cmdtext[3] == 0) {
        SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
        return 1;
    }
    new str[128];
    new Mecolor;
    Mecolor = GetPlayerColor(playerid);
    GetPlayerName(playerid, str, sizeof(str));
    format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
    SendClientMessageToAll(Mecolor, str);
    return 1;
    }
Reply
#4
Meh, wanna keep this section alive, dunno what to post :S

KickAllExceptAdmins()
An example on how to use looping:

Code:
stock KickAllExceptAdmins()
{
    for(new i = 0; i<GetMaxPlayers(); i++)
    {
        if(IsPlayerConnected(i)
        {
           if(!IsPlayerAdmin(i))
           {
                Kick(i);
           }
        }
    }
}

Simple Join Messages

Again, I guess it can be used as an example.

Code:
public OnPlayerConnect(playerid)
{
    new gConnectname[255],str[256];
    GetPlayerName(playerid, gConnectname, 255);
    format(str,256,"*** %s (ID:%d) has joined the server.",gConnectname,playerid);
    SendClientMessageToAll(0xAFAFAFAA,str);
    return true;
}

public OnPlayerDisconnect(playerid,reason)
{
   new str[256],gConnectname[255];
   GetPlayerName(playerid,gConnectname,255);
   switch(reason)
   {
        case 0: format(str,256,"%s has Timed Out!",gConnectname);
        case 1: format(str,256,"%s has left the server.",gConnectname);
        case 2: format(str,256,"%s has been kicked out the server.",gConnectname);

   }
   SendClientMessageToAll(0xAFAFAFAA,str);
   return true;
}

Dunno what else :S
Reply
#5
shouldn't this
Code:
public OnPlayerDisconnect(playerid,reason)
{
   new str[256],gConnectname[255];
   GetPlayerName(playerid,gConnectname,255);
   switch(reason)
   {
        case 0: format(str,256,"%s has Timed Out!");
        case 1: format(str,256,"%s has left the server.");
        case 2: format(str,256,"%s has been kicked out the server.");

   }
   SendClientMessageToAll(0xAFAFAFAA,str);
   return true;
}
be
Code:
public OnPlayerDisconnect(playerid,reason)
{
   new str[40],gConnectname[24];
   GetPlayerName(playerid,gConnectname,24);
   switch(reason)
   {
        case 0: format(str,sizeof(str),"%s has Timed Out!",gConnectname);
        case 1: format(str,sizeof(str),"%s has left the server.",gConnectname);
        case 2: format(str,sizeof(str),"%s has been kicked out the server."gConnectname);

   }
   SendClientMessageToAll(0xAFAFAFAA,str);
   return true;
}

you forgot to add gConnectname to the format thing :)

now some of my stuff:

Random Messages:

Up the top:
Code:
forward msgtimer();
new Help[][] =
{
    "Try \"/Commands\" for help.",
    "Spotted a hacker? read \"/help 2\".",
    "Don't use any hacks/cheats, you will be banned instantly!",
    "Don't ask to be an admin!",
    "English in mainchat, Use \"/pm\" to use Non-English",
    "Please don't use CAPS, you will be muted!",
    "Remember to obey the \"/rules\"!!",
    "Don't spawn-kill, you will be kicked!",
    "Spotted a cheater/hacker? use /report [ID] [Report].",
    "Don't door-camp (Shooting at doors) you will be kicked.",
    "Try \"/admins to see the online admins.",
    "Never ever report cheaters in main-chat.",
    "Remember to read the /rules!"

};

Under OnGameModeInit:

Code:
SetTimer("msgtimer",180000,1);

Somewhere except in another callback:

Code:
public msgtimer()
{
    SendClientMessageToAll(COLOR_YELLOW, Help[random(sizeof(Help))]);
}
You may want to change the messages to some of your own   ;)

/grove [playerid]
Code:
if(strcmp(cmd, "/grove", true) == 0)
{
new tmp[256];
tmp = strtok(cmdtext, idx);
new otherplayer = strval(tmp);
if(!strlen(tmp))
{
    SendClientMessage(playerid,0x33AA33AA,"USAGE: /grove [playerid]");
    return 1;
}
if(IsPlayerConnected(otherplayer))
{
    SetPlayerPos(otherplayer,2500.9519,-1668.9618,13.3554);
}
else
{
    SendClientMessage(playerid,0x33AA33AA,"Invalid ID");
}
return 1;
}
with this you need to define strtok into your script

Repair your vehicle with /repair
Code:
if (strcmp("/Repair", cmdtext, true) == 0)
{

if(!IsPlayerInAnyVehicle(playerid))
{
    SendClientMessage(playerid,COLOR_WHITE,"You're not in a vehicle!");
    return 1;
}
if(GetPlayerMoney(playerid) <= 9999)
{
    SendClientMessage(playerid,COLOR_GREEN,"You don't have $10,000 dollars to repair your vehicle");
    return 1;
}

new string[128];
new name[24];
GetPlayerName(playerid,name,24);
SetVehicleHealth(GetPlayerVehicleID(playerid),1000);
format(string,sizeof(string),"%s (ID: %d) Has repaired his vehicle for $10,000 dollars!",name,playerid);
SendClientMessageToAll(COLOR_WHITE,string);

return 1;
}
"/rapair" costs 10 grand :)

EDIT:
Guide on good string sizes:

instead of having all your sizes 256 you can count up how many bytes each "SendClientMessage" is and add names if there included so for example:

Code:
new string[];
new name[24];
GetPlayerName(playerid,name, 24);
format(string,sizeof(string),"Hello %s your my friend",name);
SendClientMessage(playerid,0xAFAFAFAA,string);
i left the new string[]; empty.
now where gonna count the bytes to see what it should be: okay so lets count
Hello  your my friend = 21 bytes,we leave the %s alone, Note in every sentance there is an invisible NUL byte so instead of 21 it would be 22 so now we have counted 22
since we have a name, the size of the name is 24 so what is 22 + 24? it is 46 :) so now we change the
new string[]; to new string[46];
:)
also remember that the max string size for SendClientMessage is 128
Reply
#6
Well, thats what I get for not properly looking over and testing my code :P

Thanks for pointing it out, fixed.
Reply
#7
this section is nice  :)

Errr... i have a problem idk how 2 send a pm to a player from rcon. this is the code for that:
Code:
public OnRconCommand(cmd[])
{
    if (strlen(cmd) > 50){
        print("Invalid command length (exceeding 50 characters)");
            return true;
    }

    if(!strcmp(cmd, "pm", .length = 2)){
        new arg_1 = argpos(cmd),
             arg_2 = argpos(cmd, arg_1),
              targetid = strval(cmd[arg_1]),
               message[128];
            if ( !cmd[arg_1] || cmd[arg_1] < '0' || cmd[arg_1] > '9' || targetid > max_playerid || targetid < 0 || !cmd[arg_2])
                printf("Usage: \"pm <playerid(0-%d)> <message>\"",max_playerid);
               else if ( !IsPlayerConnected(targetid) )
            print("This player is not connected!");
            else{
                format(message, sizeof(message), "[RCON] PM from an admin: %s", cmd[arg_2]);
                SendClientMessage(targetid, 0xFFFFFFFF, message);
            }
        return true;
        }
    return 0;
}

it may be a small error
Reply
#8
(04-23-2008, 04:54 PM)Jay link Wrote: Well, thats what I get for not properly looking over and testing my code :P

Thanks for pointing it out, fixed.
No problem  ;) we all get tired somtime


Quote:this section is nice 

Errr... i have a problem idk how 2 send a pm to a player from rcon. this is the code for that:


theres nothing wrong with that code. you probebly didn't define argpos its here if u need it put it in your script but not in a callback:

Code:
argpos(const string[], idx = 0, sep = ' ')// (by yom)
{
    for(new i = idx, j = strlen(string); i < j; i++)
        if (string[i] == sep && string[i+1] != sep)
            return i+1;

    return -1;
}


also i figured u copied that code from ladmin :P anyway with  "Invalid command length (exceeding 50 characters)"
you might want to return false instead of true to stop the proccess. :)

ohh and i 4got u have to defien maX_playerid to your script to
so add

Code:
new max_playerid = 0;

to your script :)

or you can do

Code:
#define max_playerid 0

:) hope i helped

i will add some more stuff later  8)
Reply
#9
all that was already done, i just didnt post it because i pasted it a long time ago, so i didnt remember which parts coumpund it
Reply
#10
/namesoff and /nameson command, to hide names!


Code:
if(strcmp(cmdtext, "/namesoff", true) == 0)
{
for(new a=0; a<MAX_PLAYERS; a++)
{
ShowPlayerNameTagForPlayer(playerid, a, false);
}
SendClientMessage(playerid, COLOR_YELLOW, "You have sucessfully turned NameTags off. Type /nameson To Turn Them Back On!");
return 1;
}

if(strcmp(cmdtext, "/nameson", true) == 0)
{
for(new a=0; a<MAX_PLAYERS; a++)
{
ShowPlayerNameTagForPlayer(playerid, a, true);
}
SendClientMessage(playerid, COLOR_YELLOW, "You have sucessfully turned NameTags on!");
return 1;
}
Reply