09.07.2009, 00:48
Adding onto a message
By Lavamike
Difficulty: Medium [Knowledge of DCMD required]By Lavamike
Introduction
Hello! My name is Mike. Today I will be showing you about a neat but simple to understand thing that I didn't even know was possible at first. Ever wanted to create a list of multiple things instead of having multiple lines? I will show you how to do this! The example I will be using is a command. In this case the command is /list. The goal is to create a list separated by commas of all the players currently online. I will be using DCMD. If you are not familiar with DCMD I suggest you read up about it by doing a search on the forums here. If you searched and you really can't find anything about it, post here and I will see what I can do. Alright, let's get to it!
*NOTE: I would also like to ask that you actually read the tutorial instead of copy and pasting. If you really want to learn how to script, read this instead of copying the code and running off to your buddy's telling them you made it.
The Theory
Well first let's talk about the theory. It is a very simple concept to follow. We will simply create a string and add names onto it using format. We will add the names on one at a time.
The Code
Step #1 - Basic setup
First let us start off with our basic command structure.
pawn Код:
dcmd_list(playerid, params[])
{
return 1;
}
Step #2 - Main setup
Now we setup what we need for the command.
pawn Код:
dcmd_list(playerid, params[])
{
SendClientMessage(playerid, 0x55FF55FF, "--- Present Players"); // Send a message before we list the online players
new ListCount = 0; // A value we will use later.
new string[130]; // Create our new string
format(string, sizeof(string), "Players: "); // Set the start of each message. Meaning if we have a lot of players on, and their names go off the screen we can continue it down to another line. So we will start each line with, "Players: "
return 1;
}
Step #3 - Setting up the loop
Now we will create a loop that goes through each player slot. This is a commonly used loop which runs through each player slot.
For more information on loops: https://sampwiki.blast.hk/wiki/Loop
pawn Код:
dcmd_list(playerid, params[])
{
SendClientMessage(playerid, 0x55FF55FF, "--- Present Players"); // Send a message before we list the online players
new ListCount = 0; // A value we will use later.
new string[130]; // Create our new string
format(string, sizeof(string), "Players: "); // Set the start of each message. Meaning if we have a lot of players on, and their names go off the screen we can continue it down to another line. So we will start each line with, "Players: "
for(new i = 0; i < MAX_PLAYERS; i++) // Setup the loop
{
if(IsPlayerConnected(i)) // Only continue if someone is connected to the player slot.
{
}
}
return 1;
}
Step #4 - The main code
Now we will make the main code inside the loop. For more information, check the comments.
pawn Код:
dcmd_list(playerid, params[])
{
SendClientMessage(playerid, 0x55FF55FF, "--- Present Players"); // Send a message before we list the online players
new ListCount = 0; // A value we will use later.
new string[130]; // Create our new string
format(string, sizeof(string), "Players: "); // Set the start of each message. Meaning if we have a lot of players on, and their names go off the screen we can continue it down to another line. So we will start each line with, "Players: "
for(new i = 0; i < MAX_PLAYERS; i++) // Setup the loop
{
if(IsPlayerConnected(i)) // Only continue if someone is connected to the player slot.
{
new PlayerName[24]; // Set a new variable to store the player's name to.
GetPlayerName(i, PlayerName, 24); // Get and store the player's name to PlayerName.
if(ListCount == 0) // We set this to 0 earlier. Basically the purpose of the ListCount value is so we can have the, "Players: " display correctly. Notice the first thing in each of the strings below. The first one is just %s which will be filled in by "Players: " the second one is %s, so we can list names. We don't want it to say, "Players: , Name(ID), Name(ID)" because there is just a random coma there in the beginning so we use this solution.
{
format(string, sizeof(string), "%s %s(%d)", string, PlayerName, i); // Format the first message of each line.
} else format(string, sizeof(string), "%s, %s(%d)", string, PlayerName, i); // Format the list of all the other players.
ListCount++; // This is equivalent to, "ListCount = ListCount + 1;" So we are adding 1 onto it. Technically all we need to do is set this to anything over 0 but this works anyway.
if(strlen(string) > 100) // Alright so this is where we create a new line. If the length of the list gets to be over 100 characters we will continue here.
{
SendClientMessage(playerid, 0xB4B4B4FF, string); // First we send what we currently have to the player.
format(string, sizeof(string), "Players: "); // Then we format the string back to the original
ListCount = 0; // And we also set this value back to the original, and continue with the process until all the players have been marked.
}
}
}
if(ListCount > 0) SendClientMessage(playerid, 0xB4B4B4FF, string); // Now we first check if ListCount is greater than 0. This is because if there are no other names, we don't want to send a line that just says, "Players: " That would look silly :) But anyway if ListCount is greater than 0, that means there is at least one name still in the list that hasn't been sent to the player yet. So we send the last line.
return 1;
}
That concludes this tutorial. I hope I explained it clear enough. If there is something I made a little vague that you would like me to clear up a bit, please let me know. Please also let me know about spelling/grammar mistakes. Also, please let me know if I made an error in the code. I don't think I did but I could be wrong.
Anyway, Happy Scripting!
![Cheesy](images/smilies/biggrin.png)
-Mike