PlayerTextDraws: Best Practice
#1

So, over the past few days, I've been trying to learn more about best practices concerning textdraws, per-player textdraws in particular. I've stumbled upon a few threads that discussed this exact issue which you can find here:I couldn't really find a satisfying answer, though. I'll try to break it down:

Do I need to use an array with PlayerTextDraws?
Some say it's needed because the order in which the textdraws are created matters, and IDs could get mixed up when you're not using an array. This, however, assumes that player textdraws are created when they're needed as opposed to creating them in bulk when a player connects and only showing and hiding them. At least that's how I understand it.
Others say it's not needed because there would be no point in having a 'playerid' parameter then. This assumes that you create all textdraws in bulk when a player connects and never destroy them. We're talking about a variable size reduction by the factor of MAX_PLAYERS, which is huge. This leads me to my next question...

When do I create/destroy textdraws?
Do I want to create them all at once and only show and hide them, or do I want to create them when they are needed and destroy them immediately when they are not needed anymore?
Reply
#2

One of the thread you mentioned already gives you the answer though.

I'm sure that's it's cleared to everyone who browse this forum actively that player text-draws do need a [MAX_PLAYERS] array. Because player-textdraws are made for the purpose that they will be changed/ be different to each player.

So in summary of the above text, Global-textdraws are supposed to show global data that won't be different per-player. Player-textdraws are supposed to show per-player data for an individual.

You can create the player-textdraw only when it's needed and remove it when it's not needed anymore, and this is only applicable on some circumstances; One example can be of a jail system where you display player the time when they'll be un-jailed or something similar. But remember that you can use Game-text functions too in replace of some things!
Reply
#3

See section 3.2 of my tutorial for an explanation, but I'll try to go in on it a little further here.

The wiki says the current per-player textdraw limit is 256. So when you first start the server, every player has all of his 256 IDs available.
Available:Not available:
Player 0:0 .. 255/
Player 1:0 .. 255/
Player 2:0 .. 255/
Say your script creates a player textdraw when a player connects, the script will get one of the available IDs for that player. Since all players are in the exact same situation when they connect, the same ID will be chosen for each player. HOWEVER, ID 0 from player 0 is not the same textdraw as ID 0 from player 1! They are each their own individual texdraw, because if you change a thing to the texdraw of one player, it will not happen to the textdraw of another player!
Available:Not available:New ID used:
Player 0:1 .. 25500
Player 1:1 .. 25500
Player 2:1 .. 25500
Next, your script needs an extra player textdraw for player 1. The next free ID for that player will be chosen.
Available:Not available:New ID used:
Player 0:1 .. 2550/
Player 1:2 .. 2550 .. 11
Player 2:1 .. 2550/
Next, your scripts needs an extra player textdraw for each player. Already the situation is different for player 1.
Available:Not available:New ID used:
Player 0:2 .. 2550 .. 11
Player 1:3 .. 2550 .. 22
Player 2:2 .. 2550 .. 11
See the last column? That's the reason why you need an array.

EDIT:
I didn't really answer all your questions.

Quote:
Originally Posted by Manyula
View Post
Others say it's not needed because there would be no point in having a 'playerid' parameter then.
The playerid parameter is there because you want to create a textdraw for that particular player and not for another player.

Quote:
Originally Posted by Manyula
View Post
This assumes that you create all textdraws in bulk when a player connects and never destroy them.
True, but you create the assumption that every player will get the same ID. Say some SA-MP update changes the code to get a free player textdraw ID from a linear method to a random method. It's pretty much guaranteed that each player will get a different ID. It's better not to create assumptions. On the other hand it's also good to create a habit of using an array, so that when you copy the player textdraw code for a new player textdraw, you won't get in a situation where you forgot the code you copied relied on an assumption that does not apply to the new player textdraw.

Quote:
Originally Posted by Manyula
View Post
We're talking about a variable size reduction by the factor of MAX_PLAYERS, which is huge. This leads me to my next question...
An array of 1000 elements is not that big really. Maybe in the context of SA-MP it is... Current systems have lots of memory. It's better to create an extra variable if that makes your code more readable in the long run.

Quote:
Originally Posted by Manyula
View Post
Do I want to create them all at once and only show and hide them, or do I want to create them when they are needed and destroy them immediately when they are not needed anymore?
As with so many things, it depends. Take a speedometer for example, the chances that you will need to show it a lot, are pretty high. So, in that case you'd better create the textdraws beforehand and keep them around. However, if it is a thing that is rarely used, it's better to create and destroy the textdraws when needed. Maybe if you're sure you're not going to need more player textdraws than the 256 limit, you can create them all in bulk. But again, it's best to think of the future were you might add a few more player textdraws and end up hitting the limit.

So, in short:
  • when the player textdraw will be shown a lot, create it beforehand and keep it around
  • when the player textdraw will be shown rarely, create/destroy it when needed
  • always use an array, so that you don't rely on assumptions
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)