[Tutorial] TextDraw streaming/arranging
#1

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
pawn Code:
new Text:Speed[MAX_PLAYERS]; //top

OnPlayerConnect(playerid)
{
Speed[playerid] = TextDrawCreate(...
}
//Somewhere Show it etc

OnPlayerDisconnect(playerid)
{
TextDrawDestroy(Speed[playerid]);
}
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 )
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
Now we will make function which will handle per player textdraw
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
    }
}
So in basic, we create it, if it isnt, updating if created and destroying if dont need it anymore.


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
Places where is usefull to destroy
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;
Reply
#2

Explain more please? Like what does
pawn Code:
enum tdinfo
{
    tSpeed,
};
Do... You did not explain it
Reply
#3

Well. its usual per player variable, I just use enum, because it this way I can use words instead of digits, for example we can also use

new IsSpeedoCreated[MAX_PLAYERS];

But to make it more simple I used enum

also can use
#define Speed 0

new TDs[MAX_PLAYERS][MyTDCount];

and later in script use

TDs[playerid][Speed] = 1;// so I you see we define that Speed = 0, I just more like to work with words/letters, than digits
Reply
#4

There is already tutorial about Speedos
Reply
#5

Quote:
Originally Posted by black_dota
View Post
There is already tutorial about Speedos
Did you even read the title?

It's not about speedos....

Read properly, stop trying to increase your post count by thinking you know what your saying, when you don't and you just want to increase your post rate.
Reply
#6

I know it isn't but there is already tutorial like this
Reply
#7

Quote:
Originally Posted by black_dota
View Post
I know it isn't but there is already tutorial like this
Dude .... and if?? any problem? he just wanted to help people , and it doesnt mean he want to show his skills, he knows how much he can script, so dont think that all people are copiers ....
=============================================
===============================================
================================================== =
And btw nice tutorial its really good
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)