Formating Strings Help!
#1

Is there anyone who can teach me more about formating strings Ive used wikipedia but I dont get it, I really need somone to explain it better.
Reply
#2

Okay first off all take a look at this:

PHP код:
%i    Integer (whole number)
%
d    Integer (whole number).
%
s    String
%f    Floating-point number (Floattag)
%
c    ASCII character
%x    Hexadecimal number
%b    Binary number
%%    Literal '%' 
So the table up here represents format specifiers. So if you wan't to format something you will use them.

So e.g. for strings (text) u use specifier %s.

Here is example code:

PHP код:
new message[128]; // '[128]' = the lenght of the message. In this case its 128. (You can use less or more. E.g. 64 or 32 or 256)...
new Number 9// So we made new number which is 9.
format(message,sizeof(message),"The number is %d",Number); // Okay so lets explain the part in (). I think you now know what is 'message' so lets skip just 1. part. 2. part = 'sizeof(message)' = Here are u checking the message size so its 128. Instead of 'sizeof(message)' you can write 128. 'The number is %d",Number' = Okay so %d means integer so we use numbers. You defined up there number 9 so it will send message: "The number is 9". You must use %d or %i with numbers or it won't display it.
SendClientMessage(playerid,-1,message); // I think you know this part. 
EDIT:

PHP код:
new message[128]; // You learned what this is.
new PlayerName[MAX_PLAYER_NAME]; // Okay so as you can se we created new 'PlayerName' because we are gonna use it in message. 'MAX_PLAYER_NAME' = This represents the maximum player's name which can be displayed. Instead of that you can also write '64' or '32'.
GetPlayerName(playerid,PlayerName,sizeof(PlayerName)); // okay so this is a function which takes player's name as you can see.
format(message,128,"My name is %s",PlayerName(playerid)); // This is a example with string. So i made message "My name is Dino",and i displayed it with %s because it is a string (text). 'PlayerName(playerid)' = This is what we used in GetPlayerName function where we discovered our name. And we had to put it here to send us our name.
SendClientMessage(playerid,-1,message); 
I hope you get it now
Reply
#3

Yea i understand now thanks..
I am working on a reg log system and I want to show his input at the end of the registration so when he types his info he will get the msg dialog at the end with his Name: etc. But I get confused when I save his like gender as an integer how can I convert the integer into string and then format that string onto the msgbox. Like I save his gender as an integet lets say 1 for male and 2 for female and I want to convert that integer 1 into string Male and I want to show this on his screen is this like possible to do with a stock or something.And one more thing Ive heard that format are not so good for scripts that this days we have something better then formats to display text etc. Is that true?
Reply
#4

For the male/female part, since I guess you're making dialogs (?),
I would make a list of genders to pick from.
Like so:


pawn Код:
// First of all, we need a public variable, so put this at the top of your script:
new gender[MAX_PLAYERS];
pawn Код:
// If you have created dialogs already, you probably don't need this...
// But it is a good way to keep track of your dialog IDs. It works almost like #define but easier.
enum
{
    DIALOG_INFO,
    DIALOG_SEX,
}

pawn Код:
// Then, show the list dialog for player, inside your command, or where ever you want.
ShowPlayerDialog(playerid, DIALOG_SEX, DIALOG_STYLE_LIST, "Select gender:","Male\n\Female","Select", "Cancel");
// Now, here is the part you were looking for. We need to convert the integer value to a readable string
pawn Код:
// This goes anywhere outside other functions and is reachable through the entire script.
getsex(playerid) // I had to :D
{
    new gend[10];
    if(!gender[playerid]) gend = "Male"; // '!' means "NOT", so this means 'if zero set string to Male'
    if(gender[playerid]) gend = "Female"; // Means 'if one set string to Female'
    return gend; // returns the string, so you can format your string with getsex(playerid)
}
pawn Код:
// Now, when the player responds with a result, we will need to store this.
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_SEX:
        {
            if(response)
            {
                if(listitem == 1){ gender[playerid] = 1; } // Picked 'Female'
                // The variable is already 0, so we don't have to specify that should he pick 'Male'.
               
                // You can move this if gender is not the last step.
                new str[128],playername[MAX_PLAYER_NAME];
                GetPlayerName(playerid,playername,sizeof(playername));
               
            // Here, we format the string you were talking about. %s is for string.
            format(str,sizeof(str),"Name: %s\nSex: %s",playername,getsex(playerid));
            ShowPlayerDialog(playerid,DIALOG_INFO,DIALOG_STYLE_MSGBOX,"Player Information",str,"OK","Cancel");
            }
        }
       
    }
    return 1;
}
Sorry, indentation got fucked by vBulletin...
Reply
#5

You can create a simple stock function that does that for you.

Код:
stock MaleFemale(gender)
{
    new string[8];
    if(gender == 1)
    {
        format(string, sizeof(string), "Male");
        return string;
    }
    else if(gender == 2)
    {
        format(string, sizeof(string), "Female");
        return string;
    }
    format(string, sizeof(string), "None"); // If the gender number is neither 1 or 2
    return string;
}
You could later use it like so:
Код:
new string[128];
format(string, sizeof(string), "Your current gender is %s!", MaleFemale(1)); // You would replace the number 1 by whatever variable saves your player's gender.
SendClientMessage(playerid, COLOR_WHITE, string);
Reply
#6

Ty very much guys
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)