[Tutorial] Creating a Player Information Text draw
#1

Creating a Player Information Text draw


Information
Pretty much, I have written this tutorial due to the amount of people posting topics in the scripting discussion regarding information using textdraws such as health, location etc. I'll try explain this tutorial in the best way possible, please post if you have any issues regarding this tutorial.

Step 1 - Setting up the Text draw
There are multiple ways that you can set up your textdraw position, looks, size and more. I will explain two ways on how to make the textdraw, one inside and outside of sa-mp.

In-game
For the in-game textdraw editor, I personally would recommend Zamaroht's Textdraw Editor. You can pretty much edit anything in game and it's probably the quickest way to get the Text draw looking good. Make sure that you have enough characters to make the textdraw fit. Type a sentence that wouldn't exceed your maximum amount of characters used.

Out of the game
Firstly we must find the textdraws position. We can do this by downloading the blank image the same size as we need for our canvas size.



Now, you can use multiple programs to find the position you want the texdtaw to be in. For this tutorial, we are going to be using Microsoft Paint. After downloading the image, open it up in paint and you should see a grey rectangle. Move your mouse around the image and you will see down the bottom bar that some co-ordonates near the crosshair icon are changing. Put your mouse over the place where you want the top of the first letter of the texdraw to be.

Now, with these co-ordonates that we have, we are going to put them in the following template.
pawn Код:
new Text:PlayerTextDraw;
PlayerTextDraw = TextDrawCreate(/*X Position*/, /*Y Position*/, "This is going to be about our max textdraw length");
There are multiple other things you can change for the textdraw to look nice as well, such as editing the Font, Letter size, Colours and much more. If you want to do this easily, you can use Zamaroht's Textdraw Editor.


Step 2 - Setting the Textdraw for All players
Now, we will have something looking similar to this or less if you did not use the text draw editor. The same way to do this applies. We are putting the textdraw setup on OnPlayerConnect because the texdraw is for a single player, not all players. We are using OnPlayerConnect because we do not want to reformat it OnPlayerSpawn (Every Spawn time including deaths).

pawn Код:
new Text:Textdraw0;
Textdraw0 = TextDrawCreate(281.000000, 427.000000, "This is going to be about our max textdraw length");
TextDrawBackgroundColor(Textdraw0, 255);
TextDrawFont(Textdraw0, 1);
TextDrawLetterSize(Textdraw0, 0.220000, 1.600000);
TextDrawColor(Textdraw0, -16776961);
TextDrawSetOutline(Textdraw0, 1);
TextDrawSetProportional(Textdraw0, 1);
Change the
pawn Код:
new Text:Textdraw0;
And add [MAX_PLAYERS] to the end of the variable so there is a variable for each player.
pawn Код:
new Text:Textdraw0[MAX_PLAYERS];
And everything else that is setting the prettiness of the the textdraw from this
pawn Код:
public OnPlayerConnect(playerid)
{
    Textdraw0 = TextDrawCreate(281.000000, 427.000000, "This is going to be about our max textdraw length");
    TextDrawBackgroundColor(Textdraw0, 255);
    TextDrawFont(Textdraw0, 1);
    TextDrawLetterSize(Textdraw0, 0.220000, 1.600000);
    TextDrawColor(Textdraw0, -16776961);
    TextDrawSetOutline(Textdraw0, 1);
    TextDrawSetProportional(Textdraw0, 1);
    return 1;
}
And add [playerid] on the end of the Text draw variable so we edit the current players textdraw.
pawn Код:
public OnPlayerConnect(playerid)
{
    Textdraw0[playerid] = TextDrawCreate(281.000000, 427.000000, "This is going to be about our max textdraw length");
    TextDrawBackgroundColor(Textdraw0[playerid], 255);
    TextDrawFont(Textdraw0[playerid], 1);
    TextDrawLetterSize(Textdraw0[playerid], 0.220000, 1.600000);
    TextDrawColor(Textdraw0[playerid], -16776961);
    TextDrawSetOutline(Textdraw0[playerid], 1);
    TextDrawSetProportional(Textdraw0[playerid], 1);
    return 1;
}
You may be thinking why we are doing this, and there is a good reason. If we only had a single Text draw then when we updated the Text draw, it would update everyone's textdraw to your information. Currently, there is no PlayerTextDrawCreate .

Step 3 - Setting up the Timer
OK, so because we are updating the information constantly, we need a timer to do this. We will have a timer running at every one second because anything lower isn't really needed. You can if you want change the timer variable.

pawn Код:
public OnGameModeInit() //Called when the gamemode starts.
{
    SetTimer("UpdateTimer", 1000, true);
    return 1;
}
Edit the 1000 if you want to change the timer duration. Now, we are going to make the actual function that is called by the timer. It is going to update our players textdraw.

pawn Код:
forward UpdateTimer(); //Must always forward a timer, read the set timer topic in the wiki for more info.
public UpdateTimer() // Must be a public.
{
    for(new x; x<MAX_PLAYERS; x++) // Loop for all players
    {
        if(!IsPlayerConnected(x) || IsPlayerNPC(x)) continue; // Skips a loop if the player is not connected or is a NPC
        new Float:Health, Float:Armour, Score = GetPlayerScore(x), Money = GetPlayerMoney(x), Name[24]; //Gets variables
        GetPlayerName(x, Name, 24); //Gets a players name
        GetPlayerHealth(x, Health); //Gets players HP
        GetPlayerArmour(x, Armour); //Gets players armour
        new TDstring[150]; // Max string size is 150 characters.
        format(TDstring, sizeof(TDstring), "Name: %s  Health: %.1f  Armour: %.1f  Money: $%d  Score: %d", Name, Health, Armour, Money, Score); //Formats the string, for more info on format, visit the wiki.
        TextDrawSetString(Textdraw0[x], TDstring); //Sets the Text Draw to our formatted string.
    }
    return 1; //Must return a value on a public.
}
Step 4 - Showing the Text draw for the player / destroying it when a player leaves.
Last but not least, we must remember always to show the textdraw for the player. This can be done on OnPlayerConnect or OnPlayerSpawn. We are going to use OnPlayerSpawn. We must also destroy the textdraw when the player leaves because there is a limit on Text Draws .

pawn Код:
public OnPlayerSpawn(playerid)
{
    TextDrawShowForPlayer(playerid, Textdraw0[playerid]); //Shows the textdraw
    return 1;
}
pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    TextDrawDestroy(Textdraw0[playerid]); //Destroys the textdraw
    return 1;
}
Conclusion
If you have any issues / are confused with this tutorial, please contact me or post in this thread. This tutorial is just a general example of how to create a player text draw that constantly updates .
Reply
#2

I usually pre-create the textdraws with a loop inside ongamemodeinit seems to be rather better than having textdraws created every single connection coming from a player.

Nice tutorial anyway

* Lorenc_ gives [HiC]TheKiller cake
Reply
#3

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
I usually pre-create the textdraws with a loop inside ongamemodeinit seems to be rather better than having textdraws created every single connection coming from a player.

Nice tutorial anyway

* Kush gives [HiC]TheKiller cake
* Kush adds the icing.

Not bad.
Reply
#4

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
I usually pre-create the textdraws with a loop inside ongamemodeinit seems to be rather better than having textdraws created every single connection coming from a player.

Nice tutorial anyway

* [HiC]TheKiller gives [HiC]TheKiller cake
Was trying to make it in the least complicated way possible :>. You also have to take into mind that if the player edits the textdraws properties in any way, if you do not re-create the textdraw it stays the same.
Reply
#5

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
Was trying to make it in the least complicated way possible :>. You also have to take into mind that if the player edits the textdraws properties in any way, if you do not re-create the textdraw it stays the same.
TextDrawSetString?

Anyway, if thats so, why not destroy those player textdraws when the player leaves? The textdraws will still be on the server undestroyed

I experienced bugs on my previous server whilst creating textdraws on onplayerconnect, not sure why lol.
Reply
#6

nice..
Reply
#7

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
TextDrawSetString?

Anyway, if thats so, why not destroy those player textdraws when the player leaves? The textdraws will still be on the server undestroyed

I experienced bugs on my previous server whilst creating textdraws on onplayerconnect, not sure why lol.
Yes, I forgot to add the destroying of the text draws. The server will munch itself if the textdraws reach their max limit over time :P. Anyway, there shouldn't be any issue creating text draws on OnPlayerConnect if it's destroyed and you don't reach your limit.
Reply
#8

Looks good. I might try it manually
Reply
#9

nice tutorial
Reply
#10

Decent guide, helped me.
Reply
#11

good job, useful!
Reply
#12

Why are you using a timer??
Can't you just use the TextDrawString function at certain events, like OnPlayerDeath and stuff??

Nice tutorial though!

Edit: oh yeah, I see, your also getting the player's armour and stuff. Nvermind what I said
Reply
#13

C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(27) : error 010: invalid function or declaration
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : error 028: invalid subscript (not an array or too many subscripts): "TextDrawBackgroundColor"
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : warning 215: expression has no effect
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : error 001: expected token: ";", but found "]"
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : error 029: invalid expression, assumed zero
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


5 Errors.
Reply
#14

Quote:
Originally Posted by Dustly
Посмотреть сообщение
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(27) : error 010: invalid function or declaration
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : error 028: invalid subscript (not an array or too many subscripts): "TextDrawBackgroundColor"
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : warning 215: expression has no effect
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : error 001: expected token: ";", but found "]"
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : error 029: invalid expression, assumed zero
C:\Users\RAC\Desktop\only server\gamemodes\AW.pwn(7 : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


5 Errors.
Obviously, you just copy-pasted the codes, try to read the tutorial.
Reply
#15

i've created a FS with this tutorial but te Text Draw doesn't show D:
Reply
#16

ok, it works now tanks ^^
Reply
#17

Good one . This helped me a lot with the filterscript that i relased yesterday I give you credits
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)