[Tutorial] Random Messages (Yes another one)
#1

Brief introduction
Hello aprentices.
Yes another "random messages" tutorial, but I'll try to explain it has good has I can (:

Resources
For this tutorial, you won't need to download anything, however you will need to create a few this.
Lets check the functions:
Creating the array
Alright lets start the practic and theoric method. (:
First of all we will have to create a "section" of what we call '2d array'
pawn Code:
new RandomMessages[][] = //Creating the array, and leaving the [][] empty so you can create as many messages as you want.
{
"Random message 1",//Message nr.1, we put them in quotes because it's a string.
"[HiC]TheKiller stinks",//Exactly the same
"Blood is a mawd."//The same, but without the coma because it's the last one, leading the pawn compiler to say "It's the last one of this array".
};
Every time you want to create some text for some function or send any message,
You have to add them in quotes, otherwise it's another symbol, and of course it will give an error.
For example this:
pawn Code:
new sometext[4] = "Yes"; //3 cells for the text, and 1 for pawn.
Is like x1000 times different from:
pawn Code:
new sometext[4] = Yes;
If you ever want to do it like the 2nd way, there is a possibilty, but you would have to define it first.
So it would become like this:
pawn Code:
#define Yes "Yes"//Noticed we added the quotes?
new sometext[4] = Yes;
Forwarding
Ok, now that we have our array created, lets forward the timer.
Before "OnGameModeInit", add this:
pawn Code:
forward SendRandomMessage();
Q: So what's the difference between Stock and Public? Because stock we don't have to write "forward"...
A: It's a bit simple, well first of all, you can't use stocks for timers, so there is the main reason.
Second of all, publics can't return strings, and stocks can.
For example:
pawn Code:
stock ReturnAdminLevelAsString(playerid,level)
{
switch(level)
{
case 1: return "Level 1";//If the player has level 1 it will return has a text "Level 1".
case 2: return "Level 2";//If the player has level 2 it will return has a text "Level 2".
}
return "Level 0";//Else if the level doesn't match any from above it will return "Level 0";
}
You can't do that example with public.

Ok back to the tutorial, after you forwarded the timer, under OnGameModeInit add this:
pawn Code:
SetTimer("SendRandomMessage",30000,true);
The parameters of SetTimer are:
  1. "SendRandomMessage" - This is the callback that we want to call
  2. 30000 - This is the interval between each call in milliseconds (ms). 1000 ms equals one second.
  3. true - This parameter tells the timer to repeat itself. It can also be set to 'false', in which case it will only run once.
//Credits to hiddos (freakin' annoying bird)
Ok compiled and no errors? Great!

Creating the public function
Alright, after forwarding our timer, and setting the timer, we have to create some function,
So it can actually send a random message to everyone (:
Ok, so at the end of your game mode add this:
pawn Code:
public SendRandomMessage()
{
new ChosenMessage = random(sizeof RandomMessages);//Choosing a random message from our array with the random messages that we created before.
SendClientMessageToAll(-1,RandomMessages[ChosenMessage]);//Sending the message to everyone, with the chosen message, that the server randomly choose.
return 1;//We couldn't put here "return SendClientMessage..." because this is a public, not a stock.
}
The end
Ok, if you followed all the steps, and got no errors, try loading the the server and go in game! (:
CAUTION: I did not test the script, so please tell me if it worked or if you're getting any errors, so we can discuss about it.
Bye bye and good luck! (:
Reply


Messages In This Thread
Random Messages (Yes another one) - by FireCat - 23.10.2011, 14:25
Re: Random Messages (Yes another one) - by [MWR]Blood - 23.10.2011, 14:28
Re: Random Messages (Yes another one) - by Kostas' - 23.10.2011, 14:40
Re: Random Messages (Yes another one) - by vassilis - 23.10.2011, 14:48
Re: Random Messages (Yes another one) - by FireCat - 23.10.2011, 14:50
Re: Random Messages (Yes another one) - by Vince - 23.10.2011, 15:07
Re: Random Messages (Yes another one) - by FireCat - 23.10.2011, 15:09
Re: Random Messages (Yes another one) - by Biesmen - 23.10.2011, 16:15
Re: Random Messages (Yes another one) - by FireCat - 23.10.2011, 17:01
Re: Random Messages (Yes another one) - by Kostas' - 23.10.2011, 17:50

Forum Jump:


Users browsing this thread: 1 Guest(s)