[Tutorial] Random Server Messages With TEXTDRAW
#1

Introduction
Hey, what's up guys? My name is David, and today I'm going to teach you how to make
random server messages using Textdraw. Sounds like fun, huh? Let's do it !

What We're Going to Do
As I've said above, we're going to make a FS (Filterscript / code / w/e) that is going to show a random message that we define each hour (Of coruse we'll be able to change it).
Let's start, shall we?

Tutorial
Let's open a new file using our editor (Pawno editor, Notepad++, etc...) and save it in the "filterscripts" directory as "rsmt.pwn" (Or whatever you wish).
Okay, now let's add this line to the top of our code:
pawn Code:
#include <a_samp>
In this line we included the include "a_samp". This include has to be in every script you make. Otherwise, the functions, publics, so on... won't work.

Now, add this just below the include line:
pawn Code:
#define MESSAGE_TIME 3600000
This line basically means how much time for each server message. 1000 = 1 sec. Therefore, 3600000 = 1 hour.

When that's done, time to add the Textdraw variable .
Add this line:
pawn Code:
new Text:RMText;
We declared a new variable of the type "Text" (Used for TextDraw's) and we called it "RMText".

Now, add this variable:
pawn Code:
new ServerMessage[3][] = {
    "Our first server message.",
    "Party Rock is in the house tonight!",
    "Awwwwwwwwwwwwwww Yeah!"
};
This variable is an array, means it has several values (cells) in it. In the first rectangular parenthesis ([]) we declared how much cells are going to be in it, plus 1 more cell. (Cells are starting from 0. Therefore, we have 0,1,2 cells in our variable, plus 1 that we declare). After that, we declare the messages. Each message should be inside quotes (""). After the end of the quotes, we need to add a coma (,) But NOT in the last one.

Add this just below:
pawn Code:
forward ShowMessage();
forward HideMessage();
In this line we're forwarding publics that we're going to make. If we want to make a public of ourselves (Not a public that comes with the a_samp, etc..) we'll need to forward it first.

Okay, now that that's done, add those lines below:
pawn Code:
public OnFilterScriptInit() {
    SetTimer("ShowMessage", MESSAGE_TIME, true);
    return 1;
}
The public "OnFilterScriptInit" is called the the filter scripts loads (When you start samp-server.exe for example). Then we've set a timer using the function "SetTimer". In the parenthesis we defined the function name (ShowMessage), interval(MESSAGE_TIME = 1 hour in this example), repeat (true = Yes).

Okay, now let's do the real work . Let's make the ShowMessage public.
Add this code below the "OnFilterScriptInit" public.
pawn Code:
public ShowMessage() {
    new RandomMessage = random(sizeof(ServerMessage));
    RMText = TextDrawCreate(10.0, 100.0, ServerMessage[RandomMessage]);
    TextDrawFont(RMText, 2);
    TextDrawColor(RMText, 0x85D0FFAA);
    TextDrawSetOutline(RMText, 1);
    TextDrawAlignment(RMText, 1);
    TextDrawShowForAll(RMText);
    SetTimer("HideMessage", 3000, false);
}
Let me explain what we've done here.
The first line declares that this is a public (function).
After that, we made a new variable called "RandomMessage", which is going to be a random number from the "ServerMessage" array. The function "random" picks a random number from 0 to the max. number defined inside the parenthesis (). In this case, we didn't want like the number 100. Therefore, we inserted the "sizeof" function which in this case, returns the amount of cells in the array.
After that, we've set the value of "RMText" to a new TextDraw. In the parenthesis of TextDrawCreate we define: x (10.0), y (100.0), text (The message).
We've set the font of the Textdraw (RMText) to font number 2.
We've set the color of the Textdraw (RMText) to 0x85D0FFAA (Very light blue).
We've set the outline of the Textdraw (RMText) to 1.
We've set the alignment of the Textdraw (RMText) to 1 (Left).
We've showed the TextDraw to all players.
We've set a timer to the public "HideMessage" in 3 second, without repeat (false = No repeat).

Now, add this public:
pawn Code:
public HideMessage() {
    TextDrawHideForAll(RMText);
}
When this public is called, it hides the Textdraw (RMText) to all players.

Final Code
Pastebin

Message
I really appreciate when you comment on my guides I really hope you appreciate it. I'm trying to make my best for you guys . If you want guides, comment/PM me and I'll try to make them.
Have a nice day .
Reply
#2

Good job man, now i like it

EDIT:
pawn Code:
new ServerMessage[3][] = {
    "Our first server message.",
    "Party Rock is in the house tonight!",
    "Awwwwwwwwwwwwwww Yeah!"
};
You Could of
pawn Code:
new ServerMessage[][] = {
    "Our first server message.",
    "Party Rock is in the house tonight!",
    "Awwwwwwwwwwwwwww Yeah!"
};
So you can write unlimited messages , just telling however good job again
Reply
#3

Hmm, yeah, that'd work too .
Thanks for the comment.
Reply
#4

Seriously there is already tutorial for this

How to make random message using textdraws by Dripac

Making helpbot as Textdraw by [MWR]Blood
Reply
#5

Yeah, only 1. So what? Can't there be 2 tutorials?
Like there's only a one special edition for tutorials.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)