View Single Post
Old 11/01/2018, 08:28 PM   #3
Freaksken's Avatar
Join Date: Mar 2009
Location: Belgium
Posts: 612
Reputation: 278
Default Re: PlayerTextDraws: Best Practice

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.

I didn't really answer all your questions.

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.

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.

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.

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
Always keep in mind that a lot of people are active on this forum in their spare time.
They are sacrificing time they could easily spend on things they would rather do, to help you instead.

[Include] FCNPC A.I. - [Tutorial] Speedometer - [WIP] Single-player-like co-op mission - [Other] List of all my work

Last edited by Freaksken; 11/01/2018 at 09:24 PM.
Freaksken is offline   Reply With Quote