23.10.2011, 14:25
(
Last edited by FireCat; 11/11/2012 at 05:59 PM.
)
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:
- forward
- public function
- 2d array
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".
};
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.
pawn Code:
new sometext[4] = Yes;
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();
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";
}
Ok back to the tutorial, after you forwarded the timer, under OnGameModeInit add this:
pawn Code:
SetTimer("SendRandomMessage",30000,true);
- "SendRandomMessage" - This is the callback that we want to call
- 30000 - This is the interval between each call in milliseconds (ms). 1000 ms equals one second.
- true - This parameter tells the timer to repeat itself. It can also be set to 'false', in which case it will only run once.
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! (: