SA-MP Forums Archive
Would this work? - 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: Discussion (https://sampforum.blast.hk/forumdisplay.php?fid=84)
+---- Thread: Would this work? (/showthread.php?tid=625059)



Would this work? - Eoussama - 27.12.2016

So, when it comes to script optimization( specially to the smart use of strings length) you would find me create a string new str[250] just to display a message like Welcome Back!
So I started focussing on change the way I work into a cleaner one, well, while I was working on something a couple of hours ago, it occurred to me something that can use as much low string size to store player's name, something much smaller than MAX_PLAYER_NAME, and then I wondered, what if I used this
PHP Code:
#define P_NAME strlen(GetName(%0))
GetName(playerid){
    new 
name_str[MAX_PLAYER_NAME];
    
GetPlayerName(playeridname_strsizeof(name_str));
    return 
name_str;

I don't know if this would work or not, so I wanted to ask you guys, what do you thing?


Re: Would this work? - Spmn - 27.12.2016

You can't declare arrays with size unknown at compile-time, so the following code is invalid and will result in a compilation error:
Code:
new str[PNAME(x)];



Re: Would this work? - Eoussama - 27.12.2016

Quote:
Originally Posted by Spmn
View Post
You can't declare arrays with size unknown at compile-time, so the following code is invalid and will result in a compilation error:
Code:
new str[PNAME(x)];
thanks for the fast reply


Re: Would this work? - SickAttack - 27.12.2016

The more cells you define, the slower it gets. That's why it's recommended to use a number that is close to what you will need.

For client messages, set it to 144. You can't use any more than that.


Re: Would this work? - SyS - 27.12.2016

It would have been nice if pawn supported dynamic allocation of arrays.


Re: Would this work? - Misiur - 28.12.2016

y_malloc allows dynamic allocation, but I'm certain 99% of people don't have the need for anything other than static allocation. I'd rather go with smarter compiler which doesn't take ages to allocate arrays.


Re: Would this work? - CodeStyle175 - 28.12.2016

just use new s[24];


Re: Would this work? - SickAttack - 28.12.2016

Quote:
Originally Posted by CodeStyle175
View Post
just use new s[24];
MAX_PLAYER_NAME + 1 is actually "more correct".


Re: Would this work? - CodeStyle175 - 28.12.2016

no....


Re: Would this work? - BiosMarcel - 28.12.2016

Quote:
Originally Posted by CodeStyle175
View Post
no....
SickAttack is right, MAX_PLAYER_NAME + 1(Null terminator).