SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 11/01/2018, 04:42 PM   #1
Manyula
Huge Clucker
 
Join Date: Jan 2012
Location: Germany
Posts: 276
Reputation: 28
Default PlayerTextDraws: Best Practice

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?
Manyula is offline   Reply With Quote
Old 11/01/2018, 05:14 PM   #2
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Location: NL-RP
Posts: 1,393
Reputation: 241
Default Re: PlayerTextDraws: Best Practice

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!
__________________
Developer @ NWG:FD TDM and NL-RP
Disclaimer: Affiliate of PrestigeSteve
Providing SA-MP, G-MOD, Web hosting and Hosted tab listing for cheap @ www.prestigesteve.com.
Logic_ is offline   Reply With Quote
Old 11/01/2018, 08:28 PM   #3
Freaksken
Gangsta
 
Freaksken's Avatar
 
Join Date: Mar 2009
Location: Belgium
Posts: 575
Reputation: 239
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.

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
__________________
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
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decent GM for practice Mo123 Scripting Help 3 25/12/2017 03:44 AM
Q: Scripting practice Uberanwar Scripting Help 4 02/04/2016 04:34 PM
Just practice Razturach Scripting Help 6 01/12/2015 11:14 AM
[GameMode] DMP [Deathmatch Practice] RedFusion Gamemode Scripts 18 21/05/2014 01:32 PM
PlayerTextDraws Roko_foko Scripting Help 12 27/06/2012 10:16 PM


All times are GMT. The time now is 02:44 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.