SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 15/11/2014, 06:58 PM   #1
Baltimore
Huge Clucker
 
Join Date: Jun 2014
Posts: 344
Reputation: 20
Default Optimize this code...

Can i optimize this code?:

pawn Code:
stock SendMessage(playerid, phrase[])
{
    new Nom[MAX_PLAYER_NAME];
    new str[256], str2[256], phrase2[256];
    GetPlayerName(playerid, Nom, sizeof(Nom));
    if(strlen(phrase) > 70)
    {
         strmid(str2, phrase, 70, strlen(phrase));
         strmid(phrase2, phrase, 0, 70);
         format(str,sizeof(str),"%s dit : %s ..." ,Nom, phrase2);
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         format(str,sizeof(str),"* ... %s" ,str2);
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         return 1;
    }
    format(str,sizeof(str),"%s dit : %s" ,phrase, Nom);
    SendClientMessage(playerid, 0xFFFFFFFF,str);
    return 1;
}

Thx!
Baltimore is offline   Reply With Quote
Old 15/11/2014, 07:15 PM   #2
sammp
Gangsta
 
sammp's Avatar
 
Join Date: Jan 2014
Posts: 585
Reputation: 49
Default Re: Optimize this code...

Make the strings 144 cells big rather than 256 cells big - 256 cells is extremely slow and client messages only support 144 cells
__________________
SA-MP must become open-source to maximise longetivity of the project.
sammp is offline   Reply With Quote
Old 15/11/2014, 07:31 PM   #3
proSeryoga
Big Clucker
 
proSeryoga's Avatar
 
Join Date: Jun 2012
Location: Russia
Posts: 138
Reputation: 1
Default Re: Optimize this code...

new Nom[MAX_PLAYER_NAME], str[256], str2[256], phrase2[256];
__________________
Russian operator of tower crane
proSeryoga is offline   Reply With Quote
Old 15/11/2014, 08:45 PM   #4
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,506
Reputation: 1635
Default Re: Optimize this code...

No proSeryoga, no.

Quote:
256 cells is extremely slow
That's a little bit overdone exorbitant haha. But yeah, it's really a waste of memory.
I also see you use max lengh 70 for both the phrases. So you could start calculating: 70 + (MAX_PLAYER_NAME) + ('Other characters in the format') = size of the array (the string).

pawn Code:
stock SendMessage(playerid, phrase[])
{
    new Nom[MAX_PLAYER_NAME];
    new str[105], str2[76], phrase2[70];
    GetPlayerName(playerid, Nom, sizeof(Nom));
    if(strlen(phrase) > 70)
    {
         strmid(str2, phrase, 70, strlen(phrase));
         strmid(phrase2, phrase, 0, 70);
         format(str,sizeof(str),"%s dit : %s ..." ,Nom, phrase2); //24 (player name) + 11 (' dit :   ...') + 70 = 105
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         format(str,sizeof(str),"* ... %s" ,str2); //6 ('* ... ' + 70 = 76)
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         return 1;
    }
    format(str,sizeof(str),"%s dit : %s" ,phrase, Nom);
    SendClientMessage(playerid, 0xFFFFFFFF,str);
    return 1;
}
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Kwarde is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help optimize this code Raefal Scripting Help 8 06/09/2014 09:09 AM
Optimize code Face9000 Scripting Help 4 05/01/2014 11:43 PM
Optimize this part of code Face9000 Scripting Help 3 29/12/2013 10:51 AM
OPTIMIZE CODE!!!!!!!! Nadim Scripting Help 2 01/11/2013 04:43 PM
help me to optimize my code armyoftwo Help Archive 3 14/11/2010 11:19 AM


All times are GMT. The time now is 04:09 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.