25.10.2010, 15:48
(
Последний раз редактировалось Hiddos; 25.10.2010 в 17:06.
)
Frankly enough I haven't seen a topic about this subject before, however this is a widely-used snippet used on many servers. That's why I've decided to create another (useless) tutorial once again, this time about (random) automatic messages send by the server, or as I call 'em: Serverized spam.
But let's cut to the point, and so: Let's get started.
Firstoff you'll need to create a new callback from which we will send the messages to the players, I'll call it "SendRandomMsgToAll":
You can place this anywhere in your script.
Now to the point of getting the callback 'called', we go to either OnFilterScriptInit() or OnGameModeInit, determined whether you're using it as a filterscript or in your gamemode.
In this section, we'll create a timer which calls our callback/function after a specified amount of time:
I hope you all can still follow me.
Back to our callback, "SendRandomMsgToAll". Remember that we had this:
That function will now get called, however there is no code to be executed, which means there are no random messages that the script will send!
I'll post the code here, and use comments to explain what everything means:
There you go, now you have an auto-message script!
For a filterscript version (without comments): http://pastebin.com/AzbMYnmF
Useful links on functions:
More information on the switch/case
More information on the SetTimer function
I hope I was clear in my tutorial, please provide questions and feedback so that I can improve it!
But let's cut to the point, and so: Let's get started.
Firstoff you'll need to create a new callback from which we will send the messages to the players, I'll call it "SendRandomMsgToAll":
pawn Код:
forward SendRandomMsgToAll();
public SendRandomMsgToAll()
{
}
Now to the point of getting the callback 'called', we go to either OnFilterScriptInit() or OnGameModeInit, determined whether you're using it as a filterscript or in your gamemode.
In this section, we'll create a timer which calls our callback/function after a specified amount of time:
pawn Код:
public OnGameModeInit() // If you're using a filterscript, use "public OnFilterScriptInit" instead.
{
SetTimer("SendRandomMsgToAll", 60 * 1000, 1);
/* Explanation of parameters:
"SendRandomMsgToAll" < The function that we want to be called.
60 * 1000 < Timers are set in milliseconds, and one second equals 1000 millisecond. That means that the '1000' means '1 second', and the '60' means '60 times 1 second'. Change the '60' to the amount of seconds you want to use.
1 < A bool variable, setting this to '0' means that the function will get called only once, setting it to '1' means that it'll keep getting called until we force-stop it. */
return 1;
}
Back to our callback, "SendRandomMsgToAll". Remember that we had this:
pawn Код:
forward SendRandomMsgToAll();
public SendRandomMsgToAll()
{
}
I'll post the code here, and use comments to explain what everything means:
pawn Код:
forward SendRandomMsgToAll();
public SendRandomMsgToAll()
{
switch(random(4))// "switch - case" is a sort of "if - else if - else" statement (operator?)
{
case 0: SendClientMessageToAll(0x00ff00ff, "The cake is a lie"); //Case means "if ( The function from switch() ) equals '0' (In this case)."
case 1: SendClientMessageToAll(0x00ff00ff, "You're playing on a stupid server! I advise you to leave!");
case 2: SendClientMessageToAll(0x00ff00ff, "If you get caught on cheating you will most probably be banned, so do it unnoticed.");
case 3: SendClientMessageToAll(0x00ff00ff, "Server closed the connection.");
/*Few more notes
random(input) returns a value of minimum '0' and maximum 'input - 1', so if you do:
random(12)
You will get a value between '0' and '11' (12 minus 1 = 11).
Because random(4) gives us values between '0' and '3', we need four 'case' functions, starting at '0' and ending at '3'. */
}
/* More explanation on the switch-case system:
switch(variable) //Retrieves the value of a variable OR from a function with the given parameters
{
case 0: DoSomething(); //If the variable/function result is the same as '0'
case 1: DoSomethingDifferent(); //If the variable/function result is the same as '1'
} */
}
For a filterscript version (without comments): http://pastebin.com/AzbMYnmF
Useful links on functions:
More information on the switch/case
More information on the SetTimer function
I hope I was clear in my tutorial, please provide questions and feedback so that I can improve it!