10.07.2012, 18:21
I was working with some textdraws today and found out that textdraw IDs start at 0 and increment onward. Now some players have been complaining that their textdraws get "bugged" or "mixed up".
Now here is how textdraw IDs work:
Now textdraw1's ID would be 0 and textdraw2's ID would be 1; that went well, but now when you're ready to destroy a textdraw you DIDN'T created this is what happens:
See, the "TextDrawDestroy" function doesn't check if the textdraw was actually created or not, it just destroys it based on the ID. When using the function above, make sure you're not using ONE textdraw because it wouldn't destroy that ONE textdraw (because the ID of that ONE textdraw is 0 and it only checks for IDs 1 or more) so in that case, to be safe, only use "TextDrawDestroy" function to destroy textdraws with ID 0 (you can know if it's ID 0 because it would be the first textdraw you create)
Here's an example:
What am saying is that I think SA-MP could consider this for a future update, maybe not change the function, but upgrade the "TextDrawDestroy" function. I started using this method and tested it and there has been no bugs/glitches with textdraws and IDs getting mixed up.
Please leave your comments/ideas, thanks.
Now here is how textdraw IDs work:
pawn Code:
textdraw1 = TextDrawCreate(...);
textdraw2 = TextDrawCreate(...);
pawn Code:
new Text:site[2],Text:forum[2];
//remember: variables are assigned a null value of 0 when the're created.
public OnPlayerConnect(playerid)
{
/*site textdraw is the first textdraw created, hence it's ID, 0. The next created textdraw would have
an ID of 1 but the next textdraw "forum[playerid]" wasn't created, so it's value is still 0.
*/
site[playerid] = TextDrawCreate(0.0,0.0,"Visit our webstie at www.website.com");
//forum[playerid] = TextDrawCreate(0.0,0.0,"Visit our webstie at www.website.com"); NEVER CREATED*
//now the scripter FORGOT to create the textdraw for the "forum" variable.
return 1;
}
//Scripter testing his "hidesite" command, result: YAY am the best scripter :) it hid the site textdraw :)
//Scripter: Am gonna test the /hidefourm command now :D
COMMAND:hidesite(playerid, params[])
{
TextDrawDestroy(site[playerid]);//NOTE: the value of the "site" variable is 0.
//therefore it should distroy textdraw 0 (site textdraw)
SendClientMessage(playerid,Grey,"You've hidden the site textdraw.");
return 1;
}
//scripter testing his "hideforum" command, result: WTF?! It hid my site textdraw.
COMMAND:hideforum(playerid, params[])
{
TextDrawDestroy(forum[playerid]);//NOTE: the value of the "forum" variable is 0.
//therefore it would distroy textdraw 0 (site) NOT textdraw 1 (forum - which was never created)
SendClientMessage(playerid,Grey,"You've hidden the forum textdraw.");
return 1;
}
//Now I just made a simple function which would somewhat circumvent the bug/glitch, except with textdraw 0.
stock TextDrawDestroyEx(Text:text)
{
if(_:text > 0)
{
TextDrawDestroy(text);
}
}
Here's an example:
pawn Code:
new td[MAX_PLAYERS][4];
public OnPlayerConnect(playerid)
{
td[playerid][0] = TextDrawCreate(0.0,0.0,"Welcome");//first textdraw created* ID 0
td[playerid][1] = TextDrawCreate(0.0,1.0,"to");//second textdraw created. ID 1
td[playerid][2] = TextDrawCreate(0.0,2.0,"our");//third textdraw created. ID 2
td[playerid][3] = TextDrawCreate(0.0,3.0,"server!");//fouth textdraw created. ID 3
return 1;
}
public OnPlayerDisconnect(playerid)
{
TextDrawDestroy(td[playerid][0]);//This function would only be used for the first textdraw created*
TextDrawDestroyEx(td[playerid][1]);
/*
Let's say you forgot to create the "td[playerid][1]" textdraw and you used "TextDrawDestroy" to destroy it,
it would destroy ID 0 because the value of "td[playerid][1]" is 0. Now by using the "TextDrawDestroyEx" function
you would be on the safe side because it only destroys textdraws with IDs greater than 0 that means IT HAD to be created
in order to have an ID greater than 0.
*/
TextDrawDestroyEx(td[playerid][2]);
TextDrawDestroyEx(td[playerid][3]);
return 1;
}
//I purposely didn't use a loop for the last three, just to let others see what we're dealing with.
Please leave your comments/ideas, thanks.