[Tutorial] Textdraws instead of GameTextForPlayer
#1

Textdraws instead of GameTextForPlayer


I've seen a lot of servers that use GameTextForPlayer to show descriptions, or required actions, like "type /enter to go inside", or something like that. These are messy and can sometimes lag the player/server. They also look horrible when there's a lot of information in it. So I'm going to show you an easy way to use Textdraws instead of GameTextForPlayer. As I go through the tutorial I will be doing this in a FilterScript, then adding the FilterScript at the end of the tutorial. I've only ever seen two servers use this; a server I used to develop for, and my current server.
Step 1: Create the variable for the Textdraw
The variable for the textdraw needs to be a player variable, as players will have different descriptions at different times. When we create a variable for a Textdraw, we need to do "new Text:variable". So add this on-top of your script, under the includes, example:
pawn Code:
#include <a_samp>
new Text:DescriptionText[MAX_PLAYERS];
Step 2: Create the Textdraw
Now we have the variable for the Textdraw, we can create the Textdraw, because this is a player textdraw, we need to do the code under OnPlayerConnect, instead of OnGameModeInit or OnFilterScriptInit. Add this under OnPlayerConnect:
pawn Code:
DescriptionText[playerid] = TextDrawCreate(320.0, 380.0, " ");
TextDrawAlignment(DescriptionText[playerid], 2);
TextDrawFont(DescriptionText[playerid], 1);
TextDrawLetterSize(DescriptionText[playerid], 0.320000, 1.700000);
TextDrawSetOutline(DescriptionText[playerid], 1);
TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
Step 3: The stock for displaying the description text
So now we have the Textdraw created, we need to add the stock that will display it for us, with the desired description text. Add this anywhere in your script:
pawn Code:
stock ShowDescriptionText(playerid, string[])
{
    KillTimer(DescriptionTimer[playerid]);
    TextDrawSetString(DescriptionText[playerid], string);
    TextDrawShowForPlayer(playerid, DescriptionText[playerid]);
    DescriptionTimer[playerid] = SetTimerEx("HideDescriptionText", 5000, 0, "i", playerid);
    return 1;
}
Step 4: The timer to hide the Textdraw
Unlike GameTextForPlayer, Textdraws don't automatically hide for a player. So we need a timer that will hide the Texrdraw for us. If you look in the stock above, we have set the timer to hide the Textdraw after 5000 ms (5 seconds, 1000ms = 1s). You can change that time to whatever you want. The first bit of the timer is creating the variable for it, add this code under the variable for the Textdraw:
pawn Code:
new DescriptionTimer[MAX_PLAYERS];
You also need the public that will actually hide the Textdraw. In the ShowDescriptionText stock, we set a timer to call this function which hides the Textdraw, how ever a timer must call a public, not a stock.
pawn Code:
forward HideDescriptionText(playerid);

public HideDescriptionText(playerid)
{
    TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
    return 1;
}

So far, the top of your script should look like this (unless you have a gamemode with other stuff)
pawn Code:
#define FILTERSCRIPT
#include <a_samp>
new Text:DescriptionText[MAX_PLAYERS];
new DescriptionTimer[MAX_PLAYERS];

forward HideDescriptionText(playerid);
And you should have this stock added somewhere:
pawn Code:
stock ShowDescriptionText(playerid, string[])
{
    KillTimer(DescriptionTimer[playerid]);
    TextDrawSetString(DescriptionText[playerid], string);
    TextDrawShowForPlayer(playerid, DescriptionText[playerid]);
    DescriptionTimer[playerid] = SetTimerEx("HideDescriptionText", 5000, 0, "i", playerid);
    return 1;
}
And you should have this public added below the forward:
[pawn
public HideDescriptionText(playerid)
{
TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
return 1;
}
[/pawn]

And your OnPlayerConnect should look like this:
pawn Code:
public OnPlayerConnect(playerid)
{
    DescriptionText[playerid] = TextDrawCreate(320.0, 380.0, " ");
    TextDrawAlignment(DescriptionText[playerid], 2);
    TextDrawFont(DescriptionText[playerid], 1);
    TextDrawLetterSize(DescriptionText[playerid], 0.320000, 1.700000);
    TextDrawSetOutline(DescriptionText[playerid], 1);
    TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
    return 1;
}
If it looks like that, then you have everything added!

How to display the Texdraw description:
It's pretty simple, there's not much explaining really needed. Here's some examples for OnPlayerCommandText.

Example 1:
pawn Code:
if (strcmp("/whatsmyname", cmdtext, true) == 0)
    {
        new string[126], playername[MAX_PLAYER_NAME];
        GetPlayerName(playerid, playername, sizeof(playername));
        format(string, sizeof(string), "~w~Your username is: ~y~%s~w~.", playername);
        ShowDescriptionText(playerid, string);
        return 1;
    }
http://i48.tinypic.com/wtmwy.png

Example 2:
pawn Code:
if(strcmp("/lol", cmdtext, true) == 0)
    {
        ShowDescriptionText(playerid, "~p~Laugh Out Loud");
        return 1;
    }
http://i45.tinypic.com/5v7vwy.png

Also, if you've never worked with player textdraws before, I better tell you, to destroy the Textdraw when the player disconnects. If you don't, sometimes when a player connects, the textdraw won't create properly for the player, and they won't be able to see the texrdraw. Add this under OnPlayerDisconnect:
pawn Code:
TextDrawDestroy(DescriptionText[playerid]);
The End
In conclusion, using Textdraws looks a lot nicer and neater then GameTextForPlayer. Some might say it could lag the server if you have heaps of players, due to the timers, but I've found no problems in using this before. Please provide positive and negative feedback; as negative feedback can improve my self and others. The filterscript I used and then tested is posted below.
Filterscript: http://pastebin.com/bcqfBbPp
Reply
#2

I didn't check it but it looks like you've put effort on it so i will just +rep you , oh and also it would be cool if you put some pics
Reply
#3

That's it, +rep!
Reply
#4

Very nice, thanks . I'll use it on the GM I'm building (Not Copy&Paste, I'll just make my own public).
Reply
#5

Quote:
Originally Posted by Mr.Fames
View Post
I didn't check it but it looks like you've put effort on it so i will just +rep you , oh and also it would be cool if you put some pics
The tinypic links under the examples go to images.

Thanks for the positive feedback
Reply
#6

Thank you,i appreciate it! +rep.
Reply
#7

Quote:
Originally Posted by Logitech90
View Post
Thank you,i appreciate it! +rep.
Thanks

Hope to see servers replace game text with textdraws!
Reply
#8

You do not have such function as "HideDescriptionText" in the script.. So the timer doesn't hide the textdraw and it stays there until you disconnect.
Reply
#9

Quote:
Originally Posted by spedico
View Post
You do not have such function as "HideDescriptionText" in the script.. So the timer doesn't hide the textdraw and it stays there until you disconnect.
Oops, I didn't notice that. I do have a function to hide it but I forgot to post it, how stupid of me.

Thanks for noticing it.

Thread updated.
Reply
#10

I've a problem,i've added the textdraw to some commands,but it doesnt hide after 5 seconds,it hides only if i type another command without the textdraw.What's wrong?
Reply
#11

Quote:
Originally Posted by Logitech90
View Post
I've a problem,i've added the textdraw to some commands,but it doesnt hide after 5 seconds,it hides only if i type another command without the textdraw.What's wrong?
I forgot to add the stock to hide the textdraw.
Please re-read the thread, and add the HideDescriptionText stock.
Reply
#12

Quote:
Originally Posted by Jack_Leslie
View Post
I forgot to add the stock to hide the textdraw.
Please re-read the thread, and add the HideDescriptionText stock.
I've it already,but the textdraw doesn't hide.

Do i need to place
pawn Code:
SetTimerEx("HideDescriptionText", 5000, 0, "i", playerid);
After
pawn Code:
ShowDescriptionText(playerid, string);
....?
Reply
#13

You should have these two stocks:

pawn Code:
stock ShowDescriptionText(playerid, string[])
{
    KillTimer(DescriptionTimer[playerid]);
    TextDrawSetString(DescriptionText[playerid], string);
    TextDrawShowForPlayer(playerid, DescriptionText[playerid]);
    DescriptionTimer[playerid] = SetTimerEx("HideDescriptionText", 5000, 0, "i", playerid);
    return 1;
}

stock HideDescriptionText(playerid)
{
    TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
    return 1;
}
Reply
#14

I have it.
Reply
#15

The "ShowDescriptionText" already has the timer part, so as long as you have the stock to hide the textdraw, it should work. So no need to start the timer "manually".
Reply
#16

So why it doesn't work?
Reply
#17

Quote:
Originally Posted by Logitech90
View Post
So why it doesn't work?
Show me every single piece of code you have relating to the Textdraws.
Reply
#18

I just followed the steps,the code is similar like the yours.
Reply
#19

Yeah paste what you have so I can try and see what you did wrong..
Reply
#20

pawn Code:
new Text:DescriptionText[MAX_PLAYERS];
new DescriptionTimer[MAX_PLAYERS];
pawn Code:
stock ShowDescriptionText(playerid, string[])
{
    KillTimer(DescriptionTimer[playerid]);
    TextDrawSetString(DescriptionText[playerid], string);
    TextDrawShowForPlayer(playerid, DescriptionText[playerid]);
    DescriptionTimer[playerid] = SetTimerEx("HideDescriptionText", 5000, 0, "i", playerid);
    return 1;
}

stock HideDescriptionText(playerid)
{
    TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
    return 1;
}
OnPlayerConnect:

pawn Code:
DescriptionText[playerid] = TextDrawCreate(320.0, 380.0, " ");
    TextDrawAlignment(DescriptionText[playerid], 2);
    TextDrawFont(DescriptionText[playerid], 1);
    TextDrawLetterSize(DescriptionText[playerid], 0.320000, 1.700000);
    TextDrawSetOutline(DescriptionText[playerid], 1);
    TextDrawHideForPlayer(playerid, DescriptionText[playerid]);
OnPlayerDisconnect

pawn Code:
TextDrawDestroy(DescriptionText[playerid]);
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)