Questions about string / variable
#1

Hi there, I have some questions about strings and variables.
I would like to create a script as optimized as possible but I'm new on pawn and I have this questions:

Should I use global string or local string? I'm making a roleplay server.
For commands like /me should I use global or local?

I would like to make a note system, this note system will take from my datebase several datas, and I wish to show in a dialog or textdraw but for that I will need several strings.

Sometimes I think is better use local string but why should I use a string in OnPlayerConnec just to tell the administrators that someone logged in? why should I use a string juts to commands like /me, /do while I can use a global string?

There's some difference?

I'm wainting some answer, thank you.
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
If you are new, you are worrying about the wrong things! The difference between those types of strings in terms of speed is minimal at best (though there are more serious issues when using global strings that generally make them a bad idea). Your mode will almost certainly be better off with you not worrying about tweaking every little function and instead just concentrating on what the mode does. Write high-level functions and IF (IF, not WHEN) you find that one part is too slow THEN you can worry about stupid things like where a variable is declared.

People here put WAY too much emphasis on being more "efficient" with no clue of what it means or even if their current code is inefficient. There are multiple sayings about "good enough" - if your code that took 5 minutes to write works, why change it for code that takes 2 hours to write and only saves you 5ms ever?
You're right.
Well I was just wondering because I don't really wanna weste cells when don't need.
Anyway thank you very much
Reply
#3

You should never declare global strings, unless they're part of an enumerator. It can lead to problems sometimes when formatting or concatenating.

When a string is declared in PAWN, the amount of memory it allocates on the stack is 4x the amount of cells the string has, so doing this:

pawn Код:
new string[128];
Would allocate 512 bytes onto the stack. You should ALWAYS create strings/arrays locally for efficiency, but then again, you can use packed strings/arrays. The only problem is that there is insufficient support for packed strings/arrays in PAWN.

https://sampforum.blast.hk/showthread.php?tid=55261

Quote:
Originally Posted by ******
Посмотреть сообщение
The way I look at it is - the browser I'm currently using to answer your question is currently using up nearly 300mb and isn't even making a dent in my RAM. Why should people be bothered about 4 bytes here or there in a SA:MP server that uses vastly less RAM either way?
How can a browser use 300mb? Multiple tabs?
Reply
#4

Okay, one more question.

OnPlayerConnect(playerid) {
new string[16];
GetPlayerIp(playerid, string, sizeof(string));
//other code..
return 1;
}

Everytime that OnPlayerConnect is called will make a new string or this string will be there forever and will use again, and againg..?

What is the difference between new string and static string?
Reply
#5

No. Once the function ends, any variables created with new are disposed, but if you use static then they're not disposed. Static variables keep their values when declared and used locally.

More information:

https://sampwiki.blast.hk/wiki/Keywords:Initialisers#static
https://sampforum.blast.hk/showthread.php?tid=289258
Reply
#6

how about Packed strings?
When should I use, how should I use?
Reply
#7

Only used packed strings when you desperately need to save memory. Note that there is insufficient support for packed strings, and I've mentioned this above.

Declaring a packed string:

pawn Код:
new string[128 char];
Would allocate 128 bytes onto the stack, rather than 512.

Check out the links below:

https://sampwiki.blast.hk/wiki/strpack
https://sampwiki.blast.hk/wiki/strunpack
https://sampwiki.blast.hk/wiki/Keywords:Operators#char
Reply
#8

Alright
No more questions, thank you very much.
Reply
#9

dont worry about packed string or static / local .. just use NEW and everything will work fine! but if u have any other questions go look on wiki https://sampwiki.blast.hk/wiki/Strpack or sa-mp forums
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)