16.06.2015, 09:58
With modular designs, pay close attention to the scope of the variables. If you need to use globals in a module, declare them as static rather than new so they are only visible in the module they're used in. Local variables declared within functions usually need not be declared static since they're only visible to the function, anyway.
For you MySQL "class", create a global variable to hold the connection handle. Make it static so it is only visible in that file. This connection handle can be used in that file as is. If you need to use the same handle in other files, create a "getter" (like in Java), a simple wrapper that returns the variable.
This means that in another file you can't do this:
Because the variable isn't recognized and you will get an error, but you can do this:
This effectively gives you a read-only variable.
To make things easier for myself, I created an include named "default.inc" and placed it the pawno/include folder. This file will be implicitly included by the compiler without the need to specify it yourself. Then I defined these:
private and protected are keywords from C and although they don't exist in Pawn, Pawno and others editors will still highlight them as keywords. So if I wanted to make a function that is only visible in the current file I'd write:
For you MySQL "class", create a global variable to hold the connection handle. Make it static so it is only visible in that file. This connection handle can be used in that file as is. If you need to use the same handle in other files, create a "getter" (like in Java), a simple wrapper that returns the variable.
PHP код:
static gConnectionHandle = -1;
public OnGameModeInit()
{
gConnectionHandle = mysql_connect(...);
}
stock GetConnectionHandle()
return gConnectionHandle;
// hook stuff for OnGameModeInit goes here, see tutorial on ALS hooking
PHP код:
printf("%d", gConnectionHandle);
PHP код:
printf("%d", GetConnectionHandle());
To make things easier for myself, I created an include named "default.inc" and placed it the pawno/include folder. This file will be implicitly included by the compiler without the need to specify it yourself. Then I defined these:
PHP код:
#define private static
#define protected
PHP код:
private myFunction(foo, bar) { ... }