[Tutorial] Simple Random Messages
#1

Introduction

Since I've seen a couple tutorials on this, and they were not explained very well at all, I figured I would create a tutorial of my own showing how I personally do it. I'm aware this may not be the best way to do it, and it in all honesty probably isn't, but I know for a fact it's not the worst way either.

Let's get started...

First off, I'm going to start with the definitions. Definitions are static meaning they cannot be changed script-wise, you will have to recompile and restart your server in order to change them.

For this system, we are going to need to define all of the random messages text beforehand, so..

pawn Code:
#define     RANDOM_MESSAGE1     "This is random message numero uno!"
    #define     RANDOM_MESSAGE2     "This is random message numero dos!"
        #define     RANDOM_MESSAGE3     "This is random message numero tres!"
            #define     RANDOM_MESSAGETIMEINMINUTES     3 //Every three minutes a random message will be shown.
Alright, so, we have created our messages, and we have defined how often the message will show, but as of now, none of this matters and is just using up useless amounts of memory. To make this system actually work, we need to create a timer, which is done by using the SetTimer function.

Now, assuming you are placing this inside of your Gamemode, find OnGameModeInit and create a timer INSIDE of the callback.

pawn Code:
public OnGameModeInit()
{
    //The rest of your code
    SetTimer("ShowARandomMessage", RANDOM_MESSAGETIMEINMINUTES*60000, true); //3 * 60000 = 180,000 milliseconds which translates to 3 minutes. (60,000 milliseconds = 1 minute)
    /* I suppose I will explain SetTimer:
            SetTimer (" Function Name " , Time in Milliseconds, Loop? (true/false) );
                                        */

    return 1;
}
In order to do it in a FilterScript, just place the code under OnFilterScriptInit!

pawn Code:
public OnFilterScriptInit()
{
    //The rest of your code
    SetTimer("ShowARandomMessage", RANDOM_MESSAGETIMEINMINUTES*60000, true); //3 * 60000 = 180,000 milliseconds which translates to 3 minutes. (60,000 milliseconds = 1 minute)
    /* I suppose I will explain SetTimer:
            SetTimer (" Function Name " , Time in Milliseconds, Loop? (true/false) );
                                        */

    return 1;
}
"ShowARandomMessage" is our function to broadcast the random message to the server (and all of your online players!)

Now, in order for your timer to actually function, we need to forward and place the remainder of our code inside of the callback we have created. ("ShowARandomMessage")

Let's do so, shall we?

First off, in order to avoid the error "warning 235: public function lacks forward declaration (symbol "ShowARandomMessage")", we need to forward the callback.

To do so, simply type:

pawn Code:
forward ShowARandomMessage();
This will help us to avoid the error mentioned above, and keep our code working as planned.

Now, we need to create the ShowARandomMessage callback. I will explain the rest of this in comments next to the code.

pawn Code:
public ShowARandomMessage()
{
    new
        RandomSelection = random(3); //Randoms start from 0 and work their way up -- THEY DO NOT START AT 1.
       
    switch(RandomSelection) { //This goes through all of the possible scenarios for "RandomSelection" (which is also "random(3)")
       
        case 0: { //If RandomSelection equals zero, this will be the result.
            return SendClientMessageToAll(0xE8B210FF, RANDOM_MESSAGE1); //Sends the message defined as "RANDOM_MESSAGE1" above
            //Returning here will keep the rest of the code from working, think of 'return' as a roadblock.
        }
        case 1: { //If RandomSelection equals one, this will be the result.
            return SendClientMessageToAll(0xE8B210FF, RANDOM_MESSAGE2); //Sends the message defined as "RANDOM_MESSAGE2" above
            //Returning here will keep the rest of the code from working, think of 'return' as a roadblock.
        }
        case 2: {
            return SendClientMessageToAll(0xE8B210FF, RANDOM_MESSAGE3); //Sends the message defined as "RANDOM_MESSAGE3" above
            //Returning here will keep the rest of the code from working, think of 'return' as a roadblock.
        }
    }
    return 1;
}
Congratulations!

You have created your first Random Message system, and perhaps in doing so have helped your players find commands, or hidden features that they never knew about! Good luck!
Reply
#2

I'm aware that this is a simple system (hense the title), but I would still like some feedback. Is there anything you guys didn't understand in that tutorial? Anything you've noticed that I did wrong?
Reply
#3

Its simple but nice. I using same "system"
Reply
#4

Hmm..Can make it even simpler by doing this

pawn Code:
#define MINUTES 3

new RandMessages[][] = {
    "Somethin",
    "Somethin2",
    "Somethin3"
};

new RandTimer; //in case we want to kill it later
forward RandomMessages();
pawn Code:
public OnGameModeInit()
{
    RandTimer = SetTimer("RandomMessages", 60000*MINUTES, true);
}
public OnGameModeExit()
{
    KillTimer(RandTimer);
}
public RandomMessages()
{
    new Rand = random(sizeof(RandMessages));
    SendClientMessageToAll(0x00CCCCAA, RandMessages[Rand]);
}
Reply
#5

Quote:
Originally Posted by [ABK]Antonio
View Post
Hmm..Can make it even simpler by doing this

pawn Code:
#define MINUTES 3

new RandMessages[][] = {
    "Somethin",
    "Somethin2",
    "Somethin3"
};

new RandTimer; //in case we want to kill it later
forward RandomMessages();
pawn Code:
public OnGameModeInit()
{
    RandTimer = SetTimer("RandomMessages", 60000*MINUTES, true);
}
public OnGameModeExit()
{
    KillTimer(RandTimer);
}
public RandomMessages()
{
    new Rand = random(sizeof(RandMessages));
    SendClientMessageToAll(0x00CCCCAA, RandMessages[Rand]);
}
Yes, I suppose that would be short and more simple, but this is merely showing how I personally do it. Although I appreciate you showing me this, and maybe I will add a part into the tutorial and explain how to do it using arrays.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)