SA-MP Forums Archive
[Tutorial] Adding Variables...to strcats! - 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: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Adding Variables...to strcats! (/showthread.php?tid=370718)



Adding Variables...to strcats! - HydraX - 21.08.2012

Hello you guys, this is my first tutorial, so excuse me if there are some errors.
INFO: So today, I finally found a solution to my dreaded problem, https://sampforum.blast.hk/showthread.php?tid=369745, and I'd like to share to you my solution to "Using variables in dialogs, using strcat".

TUTORIAL: STEP 1
Always! and I mean Always! First start with adding a string size, depending on how many chars you use, you don't have to be exact, but don't go crazy and type 2000 chars.
pawn Code:
new catstring[800],
STEP 2
The next step is to add the "baby strings".
These strings are created and it can be whatever you like, it can be words or whatever. In this tutorial though, we will learn how to use variables within a dialog with longer chars and no longer receive this error.

pawn Code:
error 075: input line too long (after substitutions)
STEP 3:
So let's add those strings!
pawn Code:
new string1[200];
new string2[200];
new string3[200];
new string4[200];
So at this point, it should look like this!

pawn Code:
new catstring[800];
new string1[200];
new string2[200];
new string3[200];
new string4[200];
STEP 4:
We are almost done! Next up we will format the strings, and by formatting strings, I mean "string1, string2, string3, and string4" aka the baby strings!

Let's say you want to use a players name.

NOTE: THIS IS AN EXAMPLE, DO NOT USE THE SAME VARIABLES I TYPE, USE YOUR OWN VARIABLES.
pawn Code:
format(string1,sizeof(string1),"%s's {FFFFFF}stats",GetName(clickedplayerid));
That's string1! Obviously my chars are short in this format, but this is just an example and you may edit the number of chars per string if needed.

Here is string2!
pawn Code:
format(string2, sizeof(string2),"{FFFFFF}%s has {33FF00}%d{FFFFFF} kills and {FF0000}%d{FFFFFF} deaths. \nHe averages {FFFF66}%0.2f {FFFFFF}Kills per Death.", GetName(clickedplayerid),kills,deaths, Float:ratio);
The cool part about this is, you may now add as much data in this format as needed, as long as you don't exceed the input line. Whenever you run out of space, that's where string3 comes in!

pawn Code:
format(string3,sizeof(string3),"\n\n\tAdmin Status: %s{FFFFFF}\n\tAccount Type: %s{FFFFFF}\n\t",AdmRank,AccType);
Here is string3! Once again, you made add as much as you want.

...and for the final touch on our baby strings..
pawn Code:
format(string4,sizeof(string4),"He has {336600}$%d{FFFFFF}.\n\tHe has %d points.\n\tHe also has %d experience points, and %d cookies.\n\tPing: %d ms\n\n%s\n%s\n%s\nIf he is in the cage, he is in for %i seconds.",cash,score,xp,cookies,GetPlayerPing(clickedplayerid), Jailed, Muted, Caged, time);
So once you are done with this, it should like this
pawn Code:
new catstring[800];
new string1[200];
new string2[200];
new string3[200];
new string4[200];
format(string1,sizeof(string1),"%s's {FFFFFF}stats",GetName(clickedplayerid));
format(string2, sizeof(string2),"{FFFFFF}%s has {33FF00}%d{FFFFFF} kills and {FF0000}%d{FFFFFF} deaths. \nHe averages {FFFF66}%0.2f {FFFFFF}Kills per Death.", GetName(clickedplayerid),kills,deaths, Float:ratio);
format(string3,sizeof(string3),"\n\n\tAdmin Status: %s{FFFFFF}\n\tAccount Type: %s{FFFFFF}\n\t",AdmRank,AccType);
format(string4,sizeof(string4),"He has {336600}$%d{FFFFFF}.\n\tHe has %d points.\n\tHe also has %d experience points, and %d cookies.\n\tPing: %d ms\n\n%s\n%s\n%s\nIf he is in the cage, he is in for %i seconds.",cash,score,xp,cookies,GetPlayerPing(clickedplayerid), Jailed, Muted, Caged, time);
so now that we formatted our baby strings, here comes the end.

STEP 5:
This is where catstring[800]; comes to play!
pawn Code:
strcat(catstring, string1);
strcat(catstring, string2);
strcat(catstring, string3);
strcat(catstring, string4);
MORE INFO on strcats! https://sampwiki.blast.hk/wiki/Strcat

FINAL STEP:
Now we add our dialog, and place "catstring" here.
pawn Code:
ShowPlayerDialog(playerid, DIALOGNAMEHERE, DIALOG_STYLE_MSGBOX, "Title", catstring, "Close", "");
In the end.. It should look like this..
pawn Code:
new catstring[800];
new string1[200];
new string2[200];
new string3[200];
new string4[200];
format(string1,sizeof(string1),"%s's {FFFFFF}stats",GetName(clickedplayerid));
format(string2, sizeof(string2),"{FFFFFF}%s has {33FF00}%d{FFFFFF} kills and {FF0000}%d{FFFFFF} deaths. \nHe averages {FFFF66}%0.2f {FFFFFF}Kills per Death.", GetName(clickedplayerid),kills,deaths, Float:ratio);
format(string3,sizeof(string3),"\n\n\tAdmin Status: %s{FFFFFF}\n\tAccount Type: %s{FFFFFF}\n\t",AdmRank,AccType);
format(string4,sizeof(string4),"He has {336600}$%d{FFFFFF}.\n\tHe has %d points.\n\tHe also has %d experience points, and %d cookies.\n\tPing: %d ms\n\n%s\n%s\n%s\nIf he is in the cage, he is in for %i seconds.",cash,score,xp,cookies,GetPlayerPing(clickedplayerid), Jailed, Muted, Caged, time);
ShowPlayerDialog(playerid, DIALOGNAMEHERE, DIALOG_STYLE_MSGBOX, "Title", catstring, "Close", "");

catstring[800] has 4 child strings, these are stored in strcat, and joins together into the reference.
Congrats! You now know how to add variables and add as many words and references as you like without getting an input error.

This is my first tutorial, If I made any mistakes, please let me know!


Re: Adding Variables...to strcats! - [KHK]Khalid - 21.08.2012

You could've done it with ONLY one string.

pawn Code:
new str[1024]; // guess this is enough for a too long text
format(str, sizeof(str), "Long text"); // here str = "LongText"
format(str, sizeof(str), "%s Another long text", str); // here str = "Long text Another long text"
format(str, sizeof(str), "%s Last long text", str); // here str = "Long text Another long text Last long text"



Respuesta: Re: Adding Variables...to strcats! - HydraX - 21.08.2012

Quote:
Originally Posted by HellSphinX
View Post
You could've done it with ONLY one string.

pawn Code:
new str[1024]; // guess this is enough for a too long text
format(str, sizeof(str), "Long text"); // here str = "LongText"
format(str, sizeof(str), "%s Another long text", str); // here str = "Long text Another long text"
format(str, sizeof(str), "%s Last long text", str); // here str = "Long text Another long text Last long text"
I agree this could have been another way, and thanks for the quick reply, but I took a more "newbie" approach and I would assume once you have enough experience, this could be a lot neater and shorter.

Thanks for the suggestion!