[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


Messages In This Thread
Textdraws instead of GameTextForPlayer - by Jack_Leslie - 11.05.2012, 09:39
Re: Textdraws instead of GameTextForPlayer - by Mr.Fames - 11.05.2012, 11:16
Re: Textdraws instead of GameTextForPlayer - by Dripac - 11.05.2012, 12:23
Re: Textdraws instead of GameTextForPlayer - by Verbal - 11.05.2012, 12:44
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 11.05.2012, 13:17
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 11.05.2012, 23:41
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 11.05.2012, 23:53
Re: Textdraws instead of GameTextForPlayer - by spedico - 14.05.2012, 17:50
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 14.05.2012, 22:04
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 15.05.2012, 12:06
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 15.05.2012, 12:11
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 15.05.2012, 12:13
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 15.05.2012, 12:31
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 15.05.2012, 12:52
Re: Textdraws instead of GameTextForPlayer - by spedico - 15.05.2012, 15:29
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 15.05.2012, 18:28
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 15.05.2012, 22:08
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 16.05.2012, 08:27
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 16.05.2012, 09:47
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 16.05.2012, 10:59
Re: Textdraws instead of GameTextForPlayer - by Jack_Leslie - 16.05.2012, 12:09
Re: Textdraws instead of GameTextForPlayer - by Face9000 - 16.05.2012, 13:13
Re: Textdraws instead of GameTextForPlayer - by x96664 - 08.07.2013, 19:10

Forum Jump:


Users browsing this thread: 1 Guest(s)