Everyone is using player textdraws wrong ?
#1

Is it me or is everyone using player textdraws wrong ?
People are releasing scripts and answering stuff with playertextdraws - and even the wiki does it - wrong.

Before we had player textdraws we did this: (i did too)
pawn Код:
// This variable is used to store the id of the textdraw
// so that we can use it throught the script
new Text:welcomeText[MAX_PLAYERS];
 
public OnPlayerConnect(playerid)
{
    // First, create the textdraw
    welcomeText[playerid] = TextDrawCreate(320.0, 240.0, "Welcome to my SA-MP server");
 
    // Now show it
    TextDrawShowForPlayer(playerid, welcomeText[playerid]);
}

Now everyone - and the wiki - is doing this: (but not me)
pawn Код:
// This variable is used to store the id of the textdraw
// so that we can use it throught the script
new PlayerText:welcomeText[MAX_PLAYERS];
 
public OnPlayerConnect(playerid)
{
    // First, create the textdraw
    welcomeText[playerid] = CreatePlayerTextDraw(playerid, 320.0, 240.0, "Welcome to my SA-MP server");
 
    // Now show it
    PlayerTextDrawShow(playerid, welcomeText[playerid]);
}
Do you see it ?
I do. Why in the name of scripting do people still use arrays. Everyone is still using arrays for playertextdraws.

When I create player textdraws I do this and it works perfectly:
pawn Код:
new PlayerText:Stats;
 
public OnPlayerConnect(playerid)
{
        Stats = CreatePlayerTextDraw(playerid,246.000000,434.000000,"~r~ Score: ~y~ x ~r~ Deaths:~y~ x ~r~ Kills: ~y~ x");
    PlayerTextDrawUseBox(playerid,Stats,1);
    PlayerTextDrawBoxColor(playerid,Stats,0x00ffff33);
    //....
}

forward UpdateStats(playerid);
public UpdateStats(playerid)
{
    new string[256];
    format(string,sizeof string,"~r~Score: ~y~%i ~r~Deaths: ~y~%i ~r~Kills: ~y~%i ~r~Headshots: ~y~%i",GetPlayerScore(playerid),Player[playerid][Deaths],Player[playerid][Kills],Player[playerid][Headshots]);
    PlayerTextDrawSetString(playerid,Stats,string);
    PlayerTextDrawShow(playerid,Stats);;
}
Stop using arrays for playertextdraws.

If you would think logical it explains itself why you don't need arrays:

Before playertextdraws:
Quote:

welcomeText[playerid] = TextDrawCreate(320.0, 240.0, "Welcome to my SA-MP server");

After:
Quote:

welcomeText[playerid] = CreatePlayerTextDraw(playerid, 320.0, 240.0, "Welcome to my SA-MP server");

You see the difference ? There's a playerid parameter. You don't need an array.

pawn Код:
welcomeText = CreatePlayerTextDraw(playerid, 320.0, 240.0, "Welcome to my SA-MP server");
If i'm wrong, please tell me why. I posted this because i'm curious btw.
Greetings.
Reply
#2

I always told people about this issue when it came to discussions but no one believed me but had arguments against this method of using them.
Reply
#3

But what if you want to remove the player textdraw?
You delete the player textdraw that the variable is assigned to.

So let's say 2 players have the textdraw shown for them, the second (last) will have his textdraw removed correctly, but the first player cannot, since welcomeText was first assigned to his player textdraw.
But then when the other player got the textdraw shown, the variable get's assigned to his player textdraw, therefore the first textdraw isn't assigned anymore.

And therefore you have to use an array, so the same array can be assigned to multiple player textdraws.
Reply
#4

I really don't understand why some people use array sizes on PlayerTextDraws seeing as it has per-playered functions. Some weeks ago i made the mistake of ignore and stop using PlayerTextDraws and started using indexed TextDraws, you will easily hit the textdraws limit if you do this, be careful, TextDraws are static and global, PlayerTextDraws are "dynamics" and per-playered (you can change the value and it will not affect others players, but it is limited).
Reply
#5

I see the problem now. So PlayerTextDraws are just ... like normal TextDraws but have their resources saved at the client's side.
Reply
#6

I indeed don't destroy them after the player disconnects or whatsoever, even more I do the same way for 2 other player textdraws - I don't destroy them either. So the fact is that i'm just using ID's wrong (as you said above) but with some luck they can't mess up because i don't destroy them. Then i completely don't know the difference between CreatePlayerTextdraw and TextdrawCreate anymore as they just work on the same way - It creates a textdraw and returns an ID - Is the answer what Meta said ?

Quote:
Originally Posted by ipsLeon
Посмотреть сообщение
I really don't understand why some people use array sizes on PlayerTextDraws seeing as it has per-playered functions.
That's what i thought too, what's the point of the playerid parameter or per-player functions when you still need an array.
Reply
#7

Global textdraws limit is 2048 (as of 0.3z), so if you wanted use only them for player specific information, assuming MAX_PLAYERS of 500, you could only create ~4 textdraws for each player. Not much to be honest. Sometimes it's worth to combine global td's with player td's, like some statistic with an icon - the icon is common for all players, and the value of that statistic is user specific.
Reply
#8

@****** : I'm sure that it was you who've said it in other posts. The reason why people assume this (that IDs are linear and increasing) is about player IDs. Those start from 0 to MAX_PLAYERS-1, and are attributed such way as the lowest unused ID is the one that'll be attributed. And it works the same way with vehicles. But if we take the example of objects, we can face situations where this rule doesn't apply (I ran into one a long ago, though don't remember it exactly).

This problem is also seen when you trynna do a custom object system. For example, roadblocks :

PHP код:

enum rbInfo
{
      
// some infos here
};
new 
rbInfo[MAX_OBJECTS][rbInfo]; 
The first roadblock you create (in this case) wouldn't necessarly be using the first index (0).

Anyway, I know of no reason why the SA-MP server should change his way of attributing IDs. It would break compatibility with basically 3/4 of the releases over there (filterscripts/includes).
Reply
#9

if you need set string in your code it's prefaire to use PlayerTextdraws , if not just use Global textdraws.
Reply
#10

Don't you think you should edit the main post to avoid confusions?
Reply
#11

Quote:
Originally Posted by [KHK]Khalid
Посмотреть сообщение
Don't you think you should edit the main post to avoid confusions?
I would have quoted Y_Less' post if he didn't deleted it ... and if i wouldn't be slow/inactive for that
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)