SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 01/08/2019, 12:12 AM   #1
Lost
Little Clucker
 
Join Date: Jul 2019
Posts: 1
Reputation: 0
Default Is it safe to use one global string variable for all formatted messages?

Apologies if this is a dumb question. Is it safe to use the second example and just use one global string variable for all formatted client messages sent by the script? I am just worried that there might be conflicts when there's a lot of messages being sent out to hundreds of different players. I am aware I can use a better method such as SendClientMessageEx but I'm doing something that requires me to do it this way.

1st example
PHP Code:
public SomeFunction(playerid)
{
    new 
szMsg[145];
    
format(szMsgsizeof(szMsg), "Hello world");
    
SendClientMessage(playeridCOLOR_WHITEszMsg);

2nd example
PHP Code:
new szMsg[145]; //global

public SomeFunction(playerid)
{
    
format(szMsgsizeof(szMsg), "Hello world");
    
SendClientMessage(playeridCOLOR_WHITEszMsg);

Lost is offline   Reply With Quote
Old 01/08/2019, 01:35 AM   #2
Flamehaze7
Big Clucker
 
Flamehaze7's Avatar
 
Join Date: Feb 2016
Posts: 133
Reputation: 10
Default Re: Is it safe to use one global string variable for all formatted messages?

You may be showing the wrong message with a global scope variable, so it's better to use a local scope variable instead of a global scope one.
Flamehaze7 is offline   Reply With Quote
Old 01/08/2019, 08:01 AM   #3
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,167
Reputation: 73
Default Re: Is it safe to use one global string variable for all formatted messages?

better using local variables for that purpose.
btw you can use custom formatted message function.
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.

Last edited by Mugala; 01/08/2019 at 08:49 AM.
Mugala is offline   Reply With Quote
Old 02/08/2019, 08:23 PM   #4
Freaksken
Gangsta
 
Freaksken's Avatar
 
Join Date: Mar 2009
Location: Belgium
Posts: 501
Reputation: 367
Default Re: Is it safe to use one global string variable for all formatted messages?

Only use global variables when you absolutely have to. That is, when you need to be able to access some data in a function in which you can not get to that data with the given parameters.
Always start writing your code with local variables first and only then switch to globals when needed.

First reason: cleaner and more readable code
See the following example:
PHP Code:
public SomeFunction(playerid)
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    
SetVehicleVirtualWorld(vehicleid0);
}

public 
SomeOtherFunction(playerid)
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    
LinkVehicleToInterior(vehicleid0);

Since we can always get the vehicleid with GetPlayerVehicleID if we know the playerid, there is no reason to store the variable globally.

Now see this example:
PHP Code:
new myPU;

public 
SomeFunction()
{
    new 
myPU CreatePickup(000.00.00.0);
}

public 
SomeOtherFunction()
{
    
DestroyPickup(myPU);

If we would use a local variable, we wouldn't know what pickup to destroy in SomeOtherFunction. Thus it is clear that we need a global variable here.

Arrays and thus strings are no different! You should treat arrays just like you would use other types of variables. In other words, if a variable/array is not of any use outside the function, than that variable/array should be declared locally.
PHP Code:
public SomeFunction(playerid)
{
    new 
szMsg[11 1]; 
    
format(szMsgsizeof(szMsg), "Hello world"); 
    
SendClientMessage(playeridCOLOR_WHITEszMsg);

Second reason: sending only the needed amount of bytes
See the following example:
PHP Code:
public SomeFunction(playerid)
{
    new 
szMsg[144 1]; 
    
format(szMsgsizeof(szMsg), "Hello world"); 
    
SendClientMessage(playeridCOLOR_WHITEszMsg);
}

public 
SomeOtherFunction(playerid)
{
    new 
szMsg[11 1]; 
    
format(szMsgsizeof(szMsg), "Hello world"); 
    
SendClientMessage(playeridCOLOR_WHITEszMsg);

What's the difference between these 2 functions? In SomeFunction you send 144+1 bytes to the client, when you actually only need to send 11+1. To know the amount of bytes needed, count the characters of 'Hello world' and add 1 for the null terminator. Sending more than the needed amount of bytes is just a waste.

Now see this example:
PHP Code:
new szMsg[144 1];

public 
SomeFunction(playerid)
{
    
format(szMsgsizeof(szMsg), "Hello world"); 
    
SendClientMessage(playeridCOLOR_WHITEszMsg);
}

public 
SomeOtherFunction(playerid)
{
    
format(szMsgsizeof(szMsg), "Some other text"); 
    
SendClientMessage(playeridCOLOR_WHITEszMsg);

When using a global variable, you ALWAYS send 144+1 bytes in both functions. As explained above, this is a waste. In SomeFunction you only need 11+1 bytes and in SomeOtherFunction you only need 15+1 bytes.
__________________
Always keep in mind that a lot of people are active on this forum in their spare time.
They are sacrificing time they could easily spend on things they would rather do, to help you instead.


[Include] FCNPC A.I. - [Tutorial] Speedometer - [WIP] Single-player-like co-op mission - [Other] List of all my work
Freaksken is offline   Reply With Quote
Old 02/08/2019, 09:05 PM   #5
Unte99
Gangsta
 
Unte99's Avatar
 
Join Date: Jul 2010
Location: Lithuania
Posts: 628
Reputation: 65
Default Re: Is it safe to use one global string variable for all formatted messages?

No, it is not safe. Better use example No. 1. I'm not experienced enough, but my guess is you could potentially have server memory problems.

Think of it like this. When you are on thin ice, which way would you break the ice faster? Going prone and dividing your weight over as much space as possible (using local variables) or standing on your feet and concentrating your whole weight on one point (using one global variable)? Of course standing on your feet would be the fastest way to break the ice and fall in. And that is dangerous.
Unte99 is offline   Reply With Quote
Old 03/08/2019, 02:30 AM   #6
Freaksken
Gangsta
 
Freaksken's Avatar
 
Join Date: Mar 2009
Location: Belgium
Posts: 501
Reputation: 367
Default Re: Is it safe to use one global string variable for all formatted messages?

Quote:
Originally Posted by Lost View Post
Is it safe to use the second example and just use one global string variable for all formatted client messages sent by the script? I am just worried that there might be conflicts when there's a lot of messages being sent out to hundreds of different players.
Quote:
Originally Posted by Unte99 View Post
No, it is not safe. Better use example No. 1. I'm not experienced enough, but my guess is you could potentially have server memory problems.
Unsafe/conflicts?
There is nothing that makes the use of global variables 'unsafe', since the SA-MP server is single threaded, which means the server can only do one thing at a time.

Memory problems?
Global variables can't be kept in the local stack, since their values should be able to be accessed at any time. Thus they are handled a bit differently. However, with today's hardware, lack of memory is not a (practical) problem anymore. An other thing having to do with memory are memory leaks, but these can't occur since Pawn doesn't have pointers.

Global variables in itself are not bad, but should be used correctly. However, for some reason people in this community have a bad habit of declaring many variables globally.
__________________
Always keep in mind that a lot of people are active on this forum in their spare time.
They are sacrificing time they could easily spend on things they would rather do, to help you instead.


[Include] FCNPC A.I. - [Tutorial] Speedometer - [WIP] Single-player-like co-op mission - [Other] List of all my work
Freaksken 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
formatted string on dialog help masterart Scripting Help 6 09/02/2019 02:16 PM
Getting numeric value from a variable name formatted into a string DTV Scripting Help 5 21/03/2017 05:19 AM
Concern: global or local string variable scope? Dr.Einstein Scripting Help 2 29/01/2014 06:06 AM
[help] Need a define that returns a formatted string DeathTone Scripting Help 4 08/05/2012 11:50 PM


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


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