Getting quite confused about the "new, decl" thing, whenever to use new or decl, and about what sizes i need to use like [32] [128] and so on
It's probably easier to understand if I give you an example.
PHP Code:
new String:mystring[32]; Format(mystring, sizeof(mystring), "more than 32 characters will be truncated"); PrintToChatAll("[SM] Print: %s", mystring);
The chat output would be "[SM] Print: more than 32 characters will be". This is because the array is only 32 bytes large which means it can only hold 32 characters. In my example mystring would need to be 42 bytes large in order to hold the full format string.
It's probably easier to understand if I give you an example.
PHP Code:
new String:mystring[32];
Format(mystring, sizeof(mystring), "more than 32 characters will be truncated");
PrintToChatAll("[SM] Print: %s", mystring);
The chat output would be "[SM] Print: more than 32 characters will be". This is because the array is only 32 bytes large which means it can only hold 32 characters. In my example mystring would need to be 42 bytes large in order to hold the full format string.
Always use new, decl isn't even available in the new syntax. Sizes can be a bit harder, you have to think about how many characters you'll really need (and then generally add a few more), there are some defines to help you, such as PLATFORM_MAX_PATH for filepaths and a few others.
Always use new, decl isn't even available in the new syntax. Sizes can be a bit harder, you have to think about how many characters you'll really need (and then generally add a few more), there are some defines to help you, such as PLATFORM_MAX_PATH for filepaths and a few others.
I dont know about new syntax not using decl, nor have I ever even read that to be the case. Seems silly to get rid of something having to do with optimization that could make/break some plugins executing certain code in OnGameFrame (or other frequent events). For many things, the difference is insignificant, but functions that repeat multiple times/second (OnGameFrame, OnPlayerRunCmd, etc) and have a lot of processing, decl can be very important to use for strings and arrays. The only downside is when people misuse it in the sense of trying to read the variable before something is saved to it, which will spit out garbage or whatever was last saved in that piece of memory (and could even crash a server). But dont remove optimization options just because some people might ignorantly skip proper checks and code planning.
“Fast” Garbage Declarations
Lastly, as far as major features go, we’ve introduced a new variable declaration keyword: “decl.” decl can be used in lieu of new, and it has one caveat: your variable won’t be automatically zeroed.
Automatically zeroing variables is considered very expensive. For example, code like this in AMX Mod X could easily increase CPU usage:
public server_frame()
{
for (new i=1; i< =maxplayers; i++)
{
new some_array[256]
}
}
Imagine - every frame, you are zeroing 256*4 (1KB) of memory! Writing to memory is expensive, and wasteful if you're a)simply going to overwrite it again and b)not going to use all of it.
In AMX Mod X, the dirty hack was to use "static" instead, which makes the array pseudo-global. But this removes re-entrancy. So for SourcePawn, the new decl keyword will skip the zeroing step, allowing you to opt into fine-tuned, optimized, but unsafe storage.
While bail has later himself said that it is a messy operation, it is mainly due to people trying to read a value before it is set properly.