[Tutorial] Creating auto-messages.
#1

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":

pawn Код:
forward SendRandomMsgToAll();
public 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:

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;
}
I hope you all can still follow me.

Back to our callback, "SendRandomMsgToAll". Remember that we had this:

pawn Код:
forward SendRandomMsgToAll();
public SendRandomMsgToAll()
{
 
}
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:

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'
        } */

}
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!
Reply
#2

ey very nice tutorial handy if you need it ^^

-J

edit: lmao at last message xDDD 'server closed the connection' xD
Reply
#3

Nice tutorial. I'm using random help messages in my server.
Reply
#4

This will help out a lot of people, actually. I get asked all the time on how to make random messages, but I always tell people to just go and download a GF edit - you'll find it there. This is much more efficient.
Reply
#5

Thanks. Added a bit more explanation to the switch/case structure.
Reply
#6

Thanks, i will to add this on my server
Reply
#7

isnt there a tutorial for a more basic version on the wiki?

Nice for more advanced users though, as 'new' scripters probably wont have a clue
Reply
#8

Quote:
Originally Posted by funky1234
Посмотреть сообщение
isnt there a tutorial for a more basic version on the wiki?

Nice for more advanced users though, as 'new' scripters probably wont have a clue
There may be, but why would you want it to be more basic? The faster, the better... (IMO)
Reply
#9

Thnx Dude
Reply
#10

This works, along with wiki. Nice one Hiddios!
Reply
#11

Nice tutorial keep it up
Reply
#12

Thanks guys, I was a li'l bored and I mentioned there wasn't a tut about this already, never knew this was on the wiki though :P.
Reply
#13

very handy thx
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)