08.02.2012, 13:19
(
Последний раз редактировалось KingHual; 11.02.2012 в 21:47.
)
WARNING: This tutorial is not for PAWN newbies!
Introduction:OK, first - I want to tell you I wanted to post this, because I recently began scripting for a MiniGames server, and there are like 100 gamemode scripts (The MiniGames themselves). And this surely helps on not having to actually replace all the functions. That's why I thought it would be useful to post it. Please don't start saying "OMG It's shit" and stuff.
This tutorial is a tutorial on how to detect native SA-MP functions which aren't detected by default, like: SetPlayerScore, GivePlayerMoney, GetPlayerScore and etc. and add new functions to them. This is useful if you've got a gamemode and you want to do something every time the server calls the default function. I'll now show you with my example function: SetPlayerScore(playerid, score);
Why you need this?
Well, let's imagine you've got a gamemode, in which you're using SetPlayerScore() like 100+ times. Now, you want to detect when this SetPlayerScore() function is called. Well, wouldn't it be a pain in the ass to add a CallRemoteFunction EVERY TIME you need to add this, plus you'll need a variable every time to save the score... Well, this tutorial solves it all. This method is used to hook a callback with default SA-MP functions and execute a piece of code WHILE doing what the function does by default. And all of that, when you type the original function code.
Tutorial:
1st. Open "a_players.inc". You can find it in your "YourServer/pawno/include" folder. Now, find the line
pawn Код:
native SetPlayerScore(playerid,score);
pawn Код:
native CUSTOM_SetPlayerScore = SetPlayerScore;
2nd. Open the "a_samp.inc", located in the same directory as "a_players.inc". Go to the bottom line (Something like 330 by default in 0.3d). In a new line (The last line for the file) type:
pawn Код:
stock My_SetPlayerScore(playerid, score)
{
new CurScore = score - GetPlayerScore(playerid);
CUSTOM_SetPlayerScore(playerid, score);
CallRemoteFunction("OnPlayerScoreStateChange", "iii", playerid, CurScore, GetPlayerScore(playerid));
}
pawn Код:
stock My_SetPlayerScore(playerid, score)
{
CUSTOM_SetPlayerScore(playerid, score);
CallRemoteFunction("OnPlayerScoreStateChange", "i", playerid);
}
Now let me explain the lines above - The
pawn Код:
stock My_SetPlayerScore(playerid, score)
pawn Код:
CUSTOM_SetPlayerScore(playerid, score);
pawn Код:
CallRemoteFunction("OnPlayerScoreStateChange", "i", playerid);
3rd. Now, on a new line, below the stock, add
pawn Код:
#if defined _ALS_SetPlayerScore //This checks if SetPlayerScore is defined
#undef SetPlayerScore //This undefines it, so you can hook it with the function above
#else
#define _ALS_SetPlayerScore //This defines it if it's not defined
#endif
#define SetPlayerScore My_SetPlayerScore //This defines the default function as our function "My_SetPlayerScore"
4th. Under the above line, type
pawn Код:
forward OnPlayerScoreStateChange(playerid);
pawn Код:
forward OnPlayerScoreStateChange(playerid, difference, newscore);
This is used to declare the function.
5th. Now, save the include. You can place the
pawn Код:
public OnPlayerScoreStateChange(playerid) {//Code here }
pawn Код:
public OnPlayerScoreStateChange(playerid, difference, newscore) {//Code here }
pawn Код:
public OnPlayerScoreStateChange(playerid, difference, newscore)
{
SendClientMessage(playerid, 0xff0000ff, "Your score has changed");
}
pawn Код:
public OnPlayerScoreStateChange(playerid, difference, newscore)
{
new str[64];
format(str, sizeof(str), "Difference:%i New score:%i", difference, newscore);
SendClientMessage(playerid, 0xff0000ff, str);
}
NOTE: You HAVE TO recompile all the scripts that use "SetPlayerScore" for this to work.
NOTE: You don't have to add the next part to your scripts! It's just a test
Here's the output of the functions. I put this script under "OnPlayerCommandText":
pawn Код:
if (strcmp("/give", cmdtext, true, 10) == 0)
{
SetPlayerScore(playerid, GetPlayerScore(playerid) + 5);
return 1;
}
https://sampforum.blast.hk/showthread.php?tid=85907
Here are the results:
*Advanced function:
*Simple function:
I hope this was useful.
Presented to you by: King_Hual