23.02.2011, 19:19
Here I will write how you can use almost any count of textdraws as you want, plus how to arrange them so they never gets fucked up, due I had this problem recently, I made small investigation.
So I guess any of you use some so called "per player textdraw", for such things as speedometer or some other things, most of people use
and will think this is done.
Well, when you will use this style and you will have few per player textdraws, in the end TDs wont handle many players, like if you run server with 150 slots last slots can run out of TDs, due samp limit 2048.
To avoid this, here is my solution, there can be many ways, I will show what I use ( without thinking about memory etc )
Now we will make function which will handle per player textdraw
So in basic, we create it, if it isnt, updating if created and destroying if dont need it anymore.
In use
Places where is usefull to destroy
In short version
We create textdraw only if we want to show it, like if you are out of car, why you need to make speedometer textdraw, so when you enter car we create one and when we exit car or disconnecting we destroying it.
But most important thing what I wanted to say is, its highly recommended to say to script which TD is valid and which isnt.
.. = Text:INVALID_TEXT_DRAW;
So I guess any of you use some so called "per player textdraw", for such things as speedometer or some other things, most of people use
pawn Code:
new Text:Speed[MAX_PLAYERS]; //top
OnPlayerConnect(playerid)
{
Speed[playerid] = TextDrawCreate(...
}
//Somewhere Show it etc
OnPlayerDisconnect(playerid)
{
TextDrawDestroy(Speed[playerid]);
}
Well, when you will use this style and you will have few per player textdraws, in the end TDs wont handle many players, like if you run server with 150 slots last slots can run out of TDs, due samp limit 2048.
To avoid this, here is my solution, there can be many ways, I will show what I use ( without thinking about memory etc )
pawn Code:
new Text:Speed[MAX_PLAYERS] = {Text:INVALID_TEXT_DRAW, ...}; //top,so we say that its invalid and server wont use it
enum tdinfo
{
tSpeed,
};
new PTD[MAX_PLAYERS][tdinfo]; //Here we will save which TDs is created for each player
pawn Code:
SpeedTD(playerid,create,string[])
{
if(create)//Define if need to create or set string
{
if(PTD[playerid][tSpeed] == 1)//we don't want create it again, so we just update its string
{
TextDrawSetString(Speed[playerid],string);
}
else//else lets make it
{
PTD[playerid][tSpeed] = 1;//We says to script, that we created it, so it will understand what we talk about
Speed[playerid] = TextDrawCreate(30.0,240.0,string);
TextDrawLetterSize(Speed[playerid],0.6,2.0);
TextDrawFont(Speed[playerid],1);
TextDrawSetOutline(Speed[playerid],1);
TextDrawColor(Speed[playerid],0xFFFFFFFF);
TextDrawShowForPlayer(playerid,Speed[playerid]);
}
TextDrawShowForPlayer(playerid,Speed[playerid]);//Here we show TDs no matter created or updated
}
else//So if we dont create it, we obviosly destroying it
{
TextDrawHideForPlayer(playerid,Speed[playerid]);
TextDrawDestroy(Speed[playerid]);
PTD[playerid][tSpeed] = 0;//Here we say that we destroy it
Speed[playerid] = Text:INVALID_TEXT_DRAW;//Here we once again say that this is invalid, so TDs wont fuck up by time
}
}
In use
pawn Code:
//My Speedometer
SpeedTD(playerid,1,string);//playerid, 1 - we want to create or update it, string - text you want show,
a.k.a TextDrawSetString
pawn Code:
OnPlayerStateChange(playerid....)
{
SpeedTD(playerid,0,"Destroy");//playerid, 0 - we want to destroy it, so it wont use memory etc anymore, "Destroy" - just some text so we use all tags
}
OnPlayerDisconnect(playerid....)
{
SpeedTD(playerid,0,"Destroy");//Same as above
}
In short version
We create textdraw only if we want to show it, like if you are out of car, why you need to make speedometer textdraw, so when you enter car we create one and when we exit car or disconnecting we destroying it.
But most important thing what I wanted to say is, its highly recommended to say to script which TD is valid and which isnt.
.. = Text:INVALID_TEXT_DRAW;