[Tutorial] Making a Health/armour textdraw! (Global textdraw)
#1

Howdy, I've seen alot of people posting over time about their issues with Textdraws and making them show for everyone, Such as a Health textdraw, Only showing for ID:0? well, im here to show you, and explain, how to make one for health and armour
Let's start.

We will start with making the global variable, Pretty simple really, top of the script.
pawn Код:
new Text:Health[MAX_PLAYERS];
new Text:Armour[MAX_PLAYERS];
Lets break this down, We got two new variables, Notice the "text" well, it means were defining a textdraw,

new = new variable| Text = Textdraw define| Health = Name of the variable| MAX PLAYERS = All player loop.
Now, we add the textdraws themselfs, Using a loop, Notice as you see read the code, They will say. Health[ i ]
The i, is another word for MAX_PLAYERS, read the first line, It Makes the letter i a variable for MAX_PLAYERS.

Now here's the textdraws, along with the global variable i.
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
    {
    Health[i] = TextDrawCreate(566.000000, 67.000000, "100%");
    TextDrawBackgroundColor(Health[i], 255);
    TextDrawFont(Health[i], 1);
    TextDrawLetterSize(Health[i], 0.219999, 0.899999);
    TextDrawColor(Health[i], -1);
    TextDrawSetOutline(Health[i], 1);
    TextDrawSetProportional(Health[i], 1);
   
    Armour[i] = TextDrawCreate(566.000000, 44.000000, "100%");
    TextDrawBackgroundColor(Armour[i], 255);
    TextDrawFont(Armour[i], 1);
    TextDrawLetterSize(Armour[i], 0.219999, 0.899999);
    TextDrawColor(Armour[i], -1);
    TextDrawSetOutline(Armour[i], 1);
    TextDrawSetProportional(Armour[i], 1);
    }
Add these to OnGameModeInIt, or Filterscriptinit, You still awake? These create the textdraw out of the textdraw functions, The numbers you see are the XY of the screen, where they are placed. They can be placed anywhere you want! But for now, these are right over the Health and armour bar.

Now we're gunna show the Health textdraw, we will get to the armour one later.
pawn Код:
TextDrawShowForPlayer(playerid, Health[playerid]);
Drop this in your OnPlayerSpawn.
This will show it for every player in the server!
Ok now, We update the textdraws to monitor the health and armour, and update the textdraws! OH MA GAWD
-
For this we need to create a new function! Oh snap! Ok, we're gunna call our function vitals, Sounds good?
Anyways, heres the function, i will explain abit.
pawn Код:
forward vitals(playerid);
public vitals(playerid)
{
new string[5]
;new Float:pHealth, Float:pArmour;
GetPlayerHealth(playerid,pHealth);
GetPlayerArmour(playerid,pArmour);
format(string, sizeof(string), "%.0f%", pHealth);
TextDrawSetString(Health[playerid], string);
format(string, sizeof(string), "%.0f%", pArmour);
TextDrawSetString(Armour[playerid], string);
}
Ok, so, Lets read this, new string, is the string array, the 5 in it, is the max number of cells were using, Since theirs 4 in the textdraws (each) we will use 5, ok, see the "New floatHealth, FloatArmour;"? Thats creating new Float variables to read the Health and armour of a player, Notice we got them crammed in their with the GetPlayerHealth and armour? Thats how its done. Its grabbing their health and armour, and inserting them into those variables, Now. Were formating the text into the string, See it says %.0f? Let me explain, %f would be a normal float, but we dont want 1000000 for our health do we? So keep it %.0f wich rounds it down to the nearest 0. Making it 100. Sounds simple enough? And below that, Textdrawsetstring, Its whats updating your Textdraws.

Ok, now lets get to the armour, Remember we didnt show it on spaw, heres what we are doing, Not everyone has armour all the time, And i doubt you want a 0% floating above your health bar? So you can either make a new function, or add this to OnPlayerUpdate,
pawn Код:
new Float:pArmour;
GetPlayerArmour(playerid, pArmour);
if(pArmour == 0){
    TextDrawHideForPlayer(playerid, Armour[playerid]);
    }else{
    TextDrawShowForPlayer(playerid, Armour[playerid]);
}
This will make it so the armour textdraw will only show if the person has armour. So theirs no stupid floating 0. Lol

Now for the finishing touchs, Adding a timer! Under OnGameModeInIt, add.
pawn Код:
SetTimer("vitals",1000,1);
Oh snap, timers are in. Sexy no? Well anyways, Let me explain it incase you dont know. SetTimer Creates a timer to run a function, Where it says vitals, is the name of our function without the Playerid junk, And where it says 1000 is a second in miliseconds, Timers ALWAYS Use this. and where it says 1, is the value of looping it, if 1, it continues to loop the timer, if 0, it is called once.

Now, once you got all this, Compile, and run your server, Once you spawn, you will see a 100% in the center of your healthbar, Grab some armour!

pawn Код:
if(!strcmp(cmdtext,"/armour",true)) {
SetPlayerArmour(playerid, 100);
return 1;}
Just incase you dont got a command, Now, once you got armour, you should have a 100% on your armour bar!
Get a rocket launcher, or some exploding weapon, and fire it near you, to test them. if they go down, success! if not, check code, if any issues, post back. i will try to help.

thanks for reading, hope its usefull, great learning experience of using floats and textdraws, and minor pawn shit.
Anyways, From yours truely.

-Rebel Son-
Reply
#2

Nice man thanks
Reply
#3

Not a problem sir.
Reply
#4

This is sexy, thanks for this!
Reply
#5

You should consider just creating one textdraw instead of two (would reduce from 1000 textdraws to 500). You could do this by using the ~n~ inside the textdraw, after displaying the health. For example:
pawn Код:
Health[i] = TextDrawCreate(566.000000, 67.000000, "100%~n~100%");

format(string, sizeof(string), "%.0f%~n~%.0f%", pHealth, pArmour);
TextDrawSetString( Health[ playerid ], string );
Just a suggestion!
Reply
#6

thanks grim, i brought that into consideration, but couldnt get the armour textdraw without a second loll
Reply
#7

1 small fix here, use %% in textdraws, to get the percentage sign, % will just put a space or something
Reply
#8

Negative donya, It works fine. %.0f% shows the percentage of the float. and then the sign.
Reply
#9

Nice tutorial!
Reply
#10

Thanks!
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)