[Tutorial] To optimize our GM and our Server improves.
#28

Quote:
Originally Posted by xVIP3Rx
View Post
So you say that the end user is able to get the global "New"s in samp ?
Or you mean for other programs ?
Lets say you create a library that runs on a specified timer. In the include you retrieve this timer and store it in a variable so you can delete it at server close - this variable is g_Timer. In your script that you include that library, they can access and modify the g_Timer variable, which in return can completely screw up the library. This is just one example, there are multiple other cases.

Here is a quick example:
pawn Code:
// timer.inc
// the include that runs off the timer

// declare the variable to store the timer ID so we can kill it later on
new g_Timer;

// Initialize the timer when the mode starts by hooking OnGameModeInit
hook OnGameModeInit() { g_Timer = SetTimer(...); }

// Kill the timer when the mode ends by hooking OnGameModeExit()
hook OnGameModeExit() { KillTimer(g_Timer); }
Now, here is the script that includes that library
pawn Code:
// test.pwn

#include <timer>

stock RandomFunction()
{
    // Here the "g_Timer" variable's value is altered meaning we no longer have the correct
    // timer ID to kill via our library
    g_Timer = SetTimer(...);
}
If you had declared the "g_Timer" variable as static, the test.pwn script wouldn't have been able to access the variable and screw things up. The static keyword is not only there to prevent things like this, but also hide implementation details from the end user. It's good programming practice. Like I've said, look up encapsulation.
Reply


Messages In This Thread
To optimize our GM and our Server improves. - by Swedky - 23.12.2013, 05:37
Re: To optimize our GM and our Server improves. - by nGen.SoNNy - 23.12.2013, 06:14
Re: To optimize our GM and our Server improves. - by Emmet_ - 23.12.2013, 06:54
Re: To optimize our GM and our Server improves. - by Ada32 - 23.12.2013, 07:02
Re: To optimize our GM and our Server improves. - by Bakr - 23.12.2013, 07:16
Respuesta: Re: To optimize our GM and our Server improves. - by Swedky - 23.12.2013, 10:29
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 11:07
Respuesta: Re: To optimize our GM and our Server improves. - by Swedky - 23.12.2013, 11:38
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 11:43
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 11:50
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 11:54
Re: To optimize our GM and our Server improves. - by Bakr - 23.12.2013, 12:11
Re: To optimize our GM and our Server improves. - by Ada32 - 23.12.2013, 12:14
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 12:21
Re: To optimize our GM and our Server improves. - by Riddick94 - 23.12.2013, 12:25
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 12:27
Re: To optimize our GM and our Server improves. - by Riddick94 - 23.12.2013, 12:31
Re: To optimize our GM and our Server improves. - by newbienoob - 23.12.2013, 14:14
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 14:29
Re: To optimize our GM and our Server improves. - by newbienoob - 23.12.2013, 14:40
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 14:42
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 14:44
Re: To optimize our GM and our Server improves. - by SuperViper - 23.12.2013, 14:50
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 14:56
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 15:07
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 15:12
Re: To optimize our GM and our Server improves. - by PowerPC603 - 23.12.2013, 16:03
Re: To optimize our GM and our Server improves. - by Bakr - 23.12.2013, 20:10
Re: To optimize our GM and our Server improves. - by Djole1337 - 23.12.2013, 20:32
Re: To optimize our GM and our Server improves. - by SuperViper - 23.12.2013, 22:35
Respuesta: To optimize our GM and our Server improves. - by Swedky - 24.12.2013, 18:09
Re: To optimize our GM and our Server improves. - by Patrick - 24.12.2013, 20:21
Respuesta: To optimize our GM and our Server improves. - by Swedky - 24.12.2013, 20:27
Re: To optimize our GM and our Server improves. - by Mister0 - 15.08.2016, 10:56
Respuesta: Re: To optimize our GM and our Server improves. - by Swedky - 17.10.2016, 07:18

Forum Jump:


Users browsing this thread: 1 Guest(s)