[SCRIPT] Random Message

Started by MatheuS, Dec 26, 2014, 11:14 PM

Previous topic - Next topic

MatheuS

Function's
function Announces()
{
    if ( GetPlayers() >= 1 )
    {
        local num = Random(1, 14);
       
        if ( num == 1 ) Message("[#FFFFFF][[#FFFF00][YOURSERVERTAG][#FFFFFF]] Message");
        else if ( num == 2 ) Message("[#FFFFFF][[#FFFF00][YOURSERVERTAG][#FFFFFF]] Message");
        else if ( num == 3 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 4 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 5 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 6 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 7 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 8 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 9 ) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 10) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 11) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 12) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 13) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
                else if ( num == 14) Message("[#FFFFFF][[#FFFF00]YOURSERVERTAG[#FFFFFF]] Message");
    }
}
 
function Random(min=0, max=RAND_MAX)
{
        local r = rand();
        srand(GetTickCount() * r);
        return (r % ((max + 1) - min)) + min;
}

OnScriptLoad
function OnScriptLoad()
{
        NewTimer("Announces", 60000, 0 );
}
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Sebastian

Sooooo maaaaanyyy "else if".
And, ignoring that, there is a lot more easier way.

Something like this:
randmsg <- [
"text1"
"text2"
...
"textn"
];

NewTimer("Announces", 60000, 0);

function Announces()
{
 if(GetPlayers() >= 1)
        Message( randmsg[rand() % randmsg.len()] + "");
}

And that should be all you need. It's just the idea, it's untested.

MatheuS

if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Nothing


.

So many ways to do this more efficiently:

// NOTE: THIS CODE WASNT TESTED FOR SYNTAX FOR LOGIC ERRORS
function random_player()
{
if (GetPlayers() <= 0) return null;
local players = [];
local i_player;
for (local id = GetMaxPlayers()-1; id >= 0; id--) {
i_player = FindPlayer(id);
if (i_player != null) players.push(i_player.ID);
}
srand(GetTickCount());
local player_id = players[rand() % players.len()];
i_player = FindPlayer(player_id);
}

function random_num(min, max)
{
srand(GetTickCount());
return min + (rand() % (max - min + 1))
}

_Messages <- [
"message 0",
"message 1",
"message 2",
"message 3",
"message 4",
"message 5",
"message 6",
"message 7",
"message 8",
"message 9"
];

// Usable across code

Message(_Messages[random_num(0, _Messages.len())]);

ClientMessage(_Messages[random_num(0, _Messages.len())], random_player(), 192, 192, 192);
.


Sk

using switch / case is better the using sooooo many if statements just sayin