Question for strings
#1

I have one public for payday, and which option is better, and why?:

1. option
Код:
public PayDay()
{
foreach(Player, i)
    {
    if(IsPlayerConnected(i) && gPlayerLogged[i] == 1)
      {
		if(PlayerInfo[i][pPayDay] < 5) return 1;
	    new coordstring[64], coordstring1[32], coordstring2[50], coordstring3[45], coordstring4[80], coordstring5[45], coordstring6[50], coordstring7[48];
        new coordstring8[80], coordstring9[35], coordstring10[50], coordstring11[72], coordstring12[50], coordstring13[45], coordstring15[75], payday[830];
...
2. option
Код:
public PayDay()
{
new coordstring[64], coordstring1[32], coordstring2[50], coordstring3[45], coordstring4[80], coordstring5[45], coordstring6[50], coordstring7[48];
        new coordstring8[80], coordstring9[35], coordstring10[50], coordstring11[72], coordstring12[50], coordstring13[45], coordstring15[75], payday[830];
foreach(Player, i)
    {
    if(IsPlayerConnected(i) && gPlayerLogged[i] == 1)
      {
		if(PlayerInfo[i][pPayDay] < 5) return 1;
...
Reply
#2

Well, in my opinion it's depending on how you will use those string variables, i'd go with option 2. But i'm wondering what kind of string you are trying to save with that a lot numbers and how you use it? Also you don't need IsPlayerConnected() when using foreach() function
Reply
#3

Quote:
Originally Posted by RoboN1X
Посмотреть сообщение
Well, in my opinion it's depending on how you will use those string variables, i'd go with option 2. But i'm wondering what kind of string you are trying to save with that a lot numbers and how you use it? Also you don't need IsPlayerConnected() when using foreach() function
Hmm, im using that strings for dialog...

And my logic is option 2, because i think that I don't need to create that strings for ex: 100 times(100 players online), when i can create that just once... Is that true?

Here is that part where im using that strings:
Код:
format(coordstring, sizeof(coordstring),"        {FFFFFF}|___{00FFFF}BANKARSKI IZVESTAJ{FFFFFF}___|");
					format(coordstring1, sizeof(coordstring1),"\n{00FFFF}Plata: {FFFFFF}$%d", checks);
	                format(coordstring2, sizeof(coordstring2),"\n{00FFFF}Steceni interes: {FFFFFF}$%d",interes);
	                format(coordstring3, sizeof(coordstring3),"\n{00FFFF}Kamatna stopa: {FFFFFF}0.%d posto",kamatnastopa);
					format(coordstring4, sizeof(coordstring4),"\n\n        {FFFFFF}|---------------- {00FFFF}Racuni {FFFFFF}----------------|");
					format(coordstring5, sizeof(coordstring5),"\n{00FFFF}Racun za struju: {FFFFFF}$%d",struja);
					format(coordstring6, sizeof(coordstring6),"\n{00FFFF}Racun za vodu: {FFFFFF}$%d",voda);
					format(coordstring7, sizeof(coordstring7),"\n{00FFFF}Racun za komunalije: {FFFFFF}$%d",komunalije);
					format(coordstring8, sizeof(coordstring8),"\n\n        {FFFFFF}|---------------- {00FFFF}Porezi {FFFFFF}----------------|");
					format(coordstring9, sizeof(coordstring9),"\n{00FFFF}Rent: {FFFFFF}%d",rent);
					format(coordstring10, sizeof(coordstring10),"\n{00FFFF}Porez na bogatstvo: {FFFFFF}$%d",porezbogastvo);
					format(coordstring11, sizeof(coordstring11),"\n\n        {FFFFFF}|-----------------------------------------------|");
					format(coordstring12, sizeof(coordstring12),"\n{00FFFF}Staro Stanje: {FFFFFF}$%d{FFFFFF}", PlayerInfo[i][pAccount] - checks - interes + (porezbogastvo + struja + voda + komunalije));
					format(coordstring13, sizeof(coordstring13),"\n{00FFFF}Novo Stanje: {FFFFFF}$%d",PlayerInfo[i][pAccount]);
					format(coordstring15, sizeof(coordstring15),"\n\n        {FFFFFF}|-----------------------------------------------|");
					format(payday, sizeof(payday)," %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",coordstring,coordstring1,coordstring2,coordstring3,coordstring4,coordstring5,coordstring6,coordstring7,coordstring8,coordstring9,coordstring10,coordstring11,coordstring12,coordstring13,coordstring15);
				    ShowPlayerDialog(i, PLATA, DIALOG_STYLE_MSGBOX, "Bankarski izvestaj gradjana", payday , "U redu", "");
Reply
#4

That's the naive approach, quite resource heavy and also quite crappy (sorry).
Use ONE temporary string to format one line at a time and then immediately use strcat to glue it to the result.
Reply
#5

of course option 2. but u only need one coordstring. like vince said use strcat to append it to the payday string
Reply
#6

Instead of making those many strings, you can do it in one string..
PHP код:
public PayDay()
{
    new 
payday[830];
    foreach(
Playeri)
    {
        if(
IsPlayerConnected(i) && gPlayerLogged[i] == 1)
        {
            
format(paydaysizeof(payday),"        {FFFFFF}|___{00FFFF}BANKARSKI IZVESTAJ{FFFFFF}___|");
            
format(paydaysizeof(payday),"%s\n{00FFFF}Plata: {FFFFFF}$%d"paydaychecks);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Steceni interes: {FFFFFF}$%d"payday,interes);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Kamatna stopa: {FFFFFF}0.%d posto"payday,kamatnastopa);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|---------------- {00FFFF}Racuni {FFFFFF}----------------|",payday);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Racun za struju: {FFFFFF}$%d"payday,struja);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Racun za vodu: {FFFFFF}$%d"payday,voda);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Racun za komunalije: {FFFFFF}$%d"payday,komunalije);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|---------------- {00FFFF}Porezi {FFFFFF}----------------|",payday);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Rent: {FFFFFF}%d",payday,rent);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Porez na bogatstvo: {FFFFFF}$%d",payday,porezbogastvo);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|-----------------------------------------------|",payday);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Staro Stanje: {FFFFFF}$%d{FFFFFF}",paydayPlayerInfo[i][pAccount] - checks interes + (porezbogastvo struja voda komunalije));
            
format(paydaysizeof(payday),"%s\n{00FFFF}Novo Stanje: {FFFFFF}$%d",payday,PlayerInfo[i][pAccount]);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|-----------------------------------------------|",payday);
            
ShowPlayerDialog(iPLATADIALOG_STYLE_MSGBOX"Bankarski izvestaj gradjana"payday "U redu""");
        }
    }

PS: sorry vince.. Just saw your post.. Tut none wanted to help him :*
Reply
#7

Thank you all! I have 2 more situations

What is better of these options:
1.1 Option:
Код:
public Plata()
{
new bank = 1;
foreach(Player, i)
    {
    if(gPlayerLogged[i] == 1)
      {
       if(PlayerInfo[i][pVip] > 1) bank = 5;
       GiveMoney(i, money*bank);
..
1.2 option:
Код:
public Plata()
{
foreach(Player, i)
    {
    if(gPlayerLogged[i] == 1)
      {
       new bank = 1;
       if(PlayerInfo[i][pVip] > 1) bank = 5;
       GiveMoney(i, money*bank);
..
And question: If i already define one big string, ex: string[500]; and i use it for dialog, can i use that for more functions in that public, ex:
Код:
new string[500];
format(string, sizeof(string), "Big message with 490 characters");
ShowPlayerDialog(i, PLATA, DIALOG_STYLE_MSGBOX, "Bankarski izvestaj gradjana", string, "U redu", "");
format(string, sizeof(string), "Small message with 30 characters");
SendClientMessage(i, Color, string);
Is there better to define one more string for this small message or it dont needed??
Reply
#8

Option 1 is best here.
It has no use to re-declare variables every iteration of a loop.
Using "new bank = 1;" actually asks the compiler to reserve some memory, then assign it a starting value.
Since it's already reserved, you don't need to reserve it a second time, or even a third or as many times as you have players.

If you declare variables inside a loop, they're destroyed when you exit the loop, after which you re-enter it again as long as there are players.
With 50 players online, you would reserve memory and destroy it again 50 times for the same purpose.
Doing this with a single integer won't matter much, but if you were to do it with big arrays, you would notice a slowdown.



Second question:
Yes, you can re-use strings as many times as you want inside the same function.
Using "format", it overwrites the entire string so it doesn't matter if you put something inside it already.
Unless you use
pawn Код:
format(string, sizeof(string), "%sSmall message with 30 characters", string);
This would first put the current contents of "string", put it where the "%s" is located, then append the rest of the given text to it.
The above is comparable to strcat, which is used to merge 2 strings together.
Reply
#9

Quote:
Originally Posted by Mic_H
Посмотреть сообщение
Instead of making those many strings, you can do it in one string..
PHP код:
public PayDay()
{
    new 
payday[830];
    foreach(
Playeri)
    {
        if(
IsPlayerConnected(i) && gPlayerLogged[i] == 1)
        {
            
format(paydaysizeof(payday),"        {FFFFFF}|___{00FFFF}BANKARSKI IZVESTAJ{FFFFFF}___|");
            
format(paydaysizeof(payday),"%s\n{00FFFF}Plata: {FFFFFF}$%d"paydaychecks);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Steceni interes: {FFFFFF}$%d"payday,interes);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Kamatna stopa: {FFFFFF}0.%d posto"payday,kamatnastopa);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|---------------- {00FFFF}Racuni {FFFFFF}----------------|",payday);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Racun za struju: {FFFFFF}$%d"payday,struja);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Racun za vodu: {FFFFFF}$%d"payday,voda);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Racun za komunalije: {FFFFFF}$%d"payday,komunalije);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|---------------- {00FFFF}Porezi {FFFFFF}----------------|",payday);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Rent: {FFFFFF}%d",payday,rent);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Porez na bogatstvo: {FFFFFF}$%d",payday,porezbogastvo);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|-----------------------------------------------|",payday);
            
format(paydaysizeof(payday),"%s\n{00FFFF}Staro Stanje: {FFFFFF}$%d{FFFFFF}",paydayPlayerInfo[i][pAccount] - checks interes + (porezbogastvo struja voda komunalije));
            
format(paydaysizeof(payday),"%s\n{00FFFF}Novo Stanje: {FFFFFF}$%d",payday,PlayerInfo[i][pAccount]);
            
format(paydaysizeof(payday),"%s\n\n        {FFFFFF}|-----------------------------------------------|",payday);
            
ShowPlayerDialog(iPLATADIALOG_STYLE_MSGBOX"Bankarski izvestaj gradjana"payday "U redu""");
        }
    }

PS: sorry vince.. Just saw your post.. Tut none wanted to help him :*
In all certainty that is actually much slower (I mean by a lot) than using "format" + "strcat". There's no appending used there, but pure re-adding what was already added and processed. Trust me, I've done benchmarks on this before.

Use:
pawn Код:
#define strcat_format(%0,%1,%2) format(%0[strlen(%0)], %1 - strlen(%0), %2)
It takes about 29 milliseconds to store "HELLO WORLD" (50,000 iterations). Using "format" + "strcat" takes about 31 milliseconds (50,000 iterations).
Reply
#10

@AmigaBlizzard Do i need to put this:
Код:
 public Plata()
{
new bank = 1;
foreach(Player, i)
    {
    if(gPlayerLogged[i] == 1)
      {
       bank = 1; // this
       if(PlayerInfo[i][pVip] > 1) bank = 5;
       GiveMoney(i, money*bank);
..
Because, if i dont put this, for first player who have vip bank will be 5, and for all next players??

@SickAttack Thank you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)