MySQL. Issue with module design.
#5

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.

PHP код:
static gConnectionHandle = -1;
public 
OnGameModeInit()
{
    
gConnectionHandle mysql_connect(...);
}
stock GetConnectionHandle()
    return 
gConnectionHandle;
// hook stuff for OnGameModeInit goes here, see tutorial on ALS hooking 
This means that in another file you can't do this:
PHP код:
printf("%d"gConnectionHandle); 
Because the variable isn't recognized and you will get an error, but you can do this:
PHP код:
printf("%d"GetConnectionHandle()); 
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:

PHP код:
#define private static
#define protected 
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:

PHP код:
private myFunction(foobar) { ... } 
Reply


Messages In This Thread
MySQL. Issue with module design. - by ZZ - 15.06.2015, 17:25
Re: MySQL. Issue with module design. - by ZZ - 16.06.2015, 08:50
Re: MySQL. Issue with module design. - by mamorunl - 16.06.2015, 09:17
Re: MySQL. Issue with module design. - by ZZ - 16.06.2015, 09:45
Re: MySQL. Issue with module design. - by Vince - 16.06.2015, 09:58
Re: MySQL. Issue with module design. - by ZZ - 16.06.2015, 10:15
Re: MySQL. Issue with module design. - by Vince - 16.06.2015, 10:30

Forum Jump:


Users browsing this thread: 1 Guest(s)