11.05.2012, 09:39
(
Last edited by Jack_Leslie; 16/05/2012 at 12:17 PM.
)
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 TextdrawThe 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];
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]);
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;
}
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];
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);
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;
}
[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;
}
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;
}
Example 2:
pawn Code:
if(strcmp("/lol", cmdtext, true) == 0)
{
ShowDescriptionText(playerid, "~p~Laugh Out Loud");
return 1;
}
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
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.