This means that:
Code: Select all
#include <amxmodx>
#include <amxmisc>
public plugin_init()
{
register_plugin("The Internet","Has","Spoken")
fnDoFunc()
iNum = 2
}
fnDoFunc()
{
static iNum
iNum = 1
}
The example above does not exemplify the true power of static variables. If you're just going to zero it before you use it, use the operator "new". Declaring a new variable is not an expensive operation, simply the zeroing of it is.
But what can a static variable do?
Here's an example of the usage of a static variable vs. a new variable:
Static
Code: Select all
#include <amxmodx>
#include <amxmisc>
public plugin_init()
register_plugin("The Internet","Has","Spoken")
public client_connect(id)
{
static szName[33]
get_user_name(id,szName,32)
client_print(0,print_chat,"the matrix has %s",szName)
}Code: Select all
#include <amxmodx>
#include <amxmisc>
public plugin_init()
register_plugin("The Internet","Has","Spoken")
public client_connect(id)
{
new szName[33]
get_user_name(id,szName,32)
client_print(0,print_chat,"the matrix has %s",szName)
}So for example, take in this hypothetical sequence of events in a server:
Static
Code: Select all
static variable initialized
Hawk552 joins server
client_connect called
static variable already exists
get name
print name
*5 minutes later*
zomg joins server
client_connect called
static variable already exists with contents "Hawk552\0..."
get name -> now looks like "zomg\052\0...", however it ends at the first \0 so the trailing 52 doesn't matter
print nameCode: Select all
Hawk552 joins server
client_connect called
new variable initialized
get name
print name
*5 minutes later*
zomg joins server
client_connect called
new variable initialized
get name
print name