Issue with /time command.
#1

So I'm trying to make a /time command which should show a textdraw displaying the current time. But instead I see a black box. What did I do wrong?

PHP код:
new Text:InfoText[MAX_PLAYERS], DisplayingText[MAX_PLAYERS], TextTiming[MAX_PLAYERS];
stock CreateInfoTextDraw(playerid)
{
    
InfoText[playerid] = TextDrawCreate(319.000000380.000000"");
    
TextDrawAlignment(InfoText[playerid], 2);
    
TextDrawBackgroundColor(InfoText[playerid], 255);
    
TextDrawFont(InfoText[playerid], 1);
    
TextDrawLetterSize(InfoText[playerid], 0.3200001.500000);
    
TextDrawSetProportional(InfoText[playerid], 1);
    
TextDrawSetShadow(InfoText[playerid], 1);
}
stock DisplayInfoTextDraw(playeridstr[], duration)
{
    if(
DisplayingText[playerid])
    {
        
KillTimer(TextTiming[playerid]);
    }
    
TextDrawSetString(InfoText[playerid], str);
    
TextDrawShowForPlayer(playeridInfoText[playerid]);
    
TextTiming[playerid] = SetTimerEx("HideInfoTextDraw"duration *10000"i"playerid);
    
DisplayingText[playerid] = 1;
    return 
1;
}
stock HideInfoTextDraw(playerid)
{
    
TextDrawHideForPlayer(playeridInfoText[playerid]);
    
DisplayingText[playerid] = 0;
    return 
1;
}
stock MonthName(Month)
{
    new 
MonthStr[15];
    switch(
Month)
    {
        case 
1:  MonthStr "January";
        case 
2:  MonthStr "February";
        case 
3:  MonthStr "March";
        case 
4:  MonthStr "April";
        case 
5:  MonthStr "May";
        case 
6:  MonthStr "June";
        case 
7:  MonthStr "July";
        case 
8:  MonthStr "August";
        case 
9:  MonthStr "September";
        case 
10MonthStr "October";
        case 
11MonthStr "November";
        case 
12MonthStr "December";
    }
    return 
MonthStr;
}
CMD:time(playerid,params[])
{
    new 
monthdayyearhourminutesecondstr[500];
    
getdate(yearmonthday);
    
gettime(hourminutesecond);
    if(
Account[playerid][Prison] == 1)
    {
        
format(strsizeof(str), "~y~%02d %s, %d ~w~~n~%02d:%02d:%02d~n~Prison Sentence: %d minutes and %d seconds"dayMonthName(month), yearhourminutesecondAccount[playerid][PrisonTime] / 60Account[playerid][PrisonTime] / 60 60);
    }
    else
    {
        
format(strsizeof(str), "~y~%02d %s, %d ~w~~n~%02d:%02d:%02d"dayMonthName(month), yearhourminutesecond);
    } 
    
DisplayInfoTextDraw(playeridstr8);
    return 
1;

Reply
#2

Alright, few things ...

1.
You're using global textdraws everywhere, that's not good, since you'll hit the max of 2048 quite quick. In your time command you show one of two things:
  • current time + prison sentence: since the prison sentence is different for each player, it's a prime example of when you need a player textdraw
  • current time: since the current time is the same for every player, it's a prime example of when you need a global textdraw
So in total you only need 1 player texdraw and 1 global textdraw.

2.
You don't initialize your textdraws, this can lead to very strange behaviour in the future.

3.
You're also doing this:
Код:
new str[500];
new MonthStr[15];
Just learn to calculate how large the string can get, but this is just a minor issue.

Other than that, I just tested you're code, and the time does show for me.
Reply
#3

Quote:
Originally Posted by Freaksken
Посмотреть сообщение
Alright, few things ...

1.
You're using global textdraws everywhere, that's not good, since you'll hit the max of 2048 quite quick. In your time command you show one of two things:
  • current time + prison sentence: since the prison sentence is different for each player, it's a prime example of when you need a player textdraw
  • current time: since the current time is the same for every player, it's a prime example of when you need a global textdraw
So in total you only need 1 player texdraw and 1 global textdraw.

2.
You don't initialize your textdraws, this can lead to very strange behaviour in the future.

3.
You're also doing this:
Код:
new str[500];
new MonthStr[15];
Just learn to calculate how large the string can get, but this is just a minor issue.

Other than that, I just tested you're code, and the time does show for me.

I am getting this black box, how come it would work for you? I'm confused. What's wrong with making a textdraw like this? I don't want to make 200 different textdraws, just change the text based on what I want to tell the player at that time.
Reply
#4

Quote:
Originally Posted by Stefhan
Посмотреть сообщение
I am getting this black box, how come it would work for you? I'm confused. What's wrong with making a textdraw like this? I don't want to make 200 different textdraws, just change the text based on what I want to tell the player at that time.
That's because most likely you've hit the global textdraw limit (just like I've told you you would) and the script is trying to display another textdraw and not the one you just created.
Reply
#5

Quote:
Originally Posted by Freaksken
Посмотреть сообщение
That's because most likely you've hit the global textdraw limit (just like I've told you you would) and the script is trying to display another textdraw and not the one you just created.
I understand, so how would I go over making it a player textdraw instead?
Reply
#6

Quote:
Originally Posted by Stefhan
Посмотреть сообщение
I understand, so how would I go over making it a player textdraw instead?
Instead of giving you the code on a plate, I'd suggest you read this tutorial. It's about how to create a speedometer with textdraws, but similar concepts apply here. Definitely read the first section, since it explains the difference between global and player textdraws.

After reading that, and you have any further questions, let me know here.
Reply
#7

Freaksken solved the issue with me through Discord. If anyone else faces this problem, you need to make a player textdraw. I suggest following Freaksken's guide which you can find here. https://sampforum.blast.hk/showthread.php?tid=625685

This is the code I have now, that fixed it.

PHP код:
new PlayerText:InfoText[MAX_PLAYERS] = {PlayerText:INVALID_TEXT_DRAW, ...}, DisplayingText[MAX_PLAYERS], TextTiming[MAX_PLAYERS], AnimTiming[MAX_PLAYERS];
stock CreateInfoTextDraw(playerid)
{
    
InfoText[playerid] = CreatePlayerTextDraw(playerid319.000000380.000000"");
    
PlayerTextDrawAlignment(playeridInfoText[playerid], 2);
    
PlayerTextDrawBackgroundColor(playeridInfoText[playerid], 255);
    
PlayerTextDrawFont(playeridInfoText[playerid], 1);
    
PlayerTextDrawLetterSize(playeridInfoText[playerid], 0.3200001.500000);
    
PlayerTextDrawSetProportional(playeridInfoText[playerid], 1);
    
PlayerTextDrawSetShadow(playeridInfoText[playerid], 1);
}
stock DisplayInfoTextDraw(playeridstr[], duration)
{
    if(
DisplayingText[playerid])
    {
        
KillTimer(TextTiming[playerid]);
    }
    
PlayerTextDrawSetString(playeridInfoText[playerid], str);
    
PlayerTextDrawShow(playeridInfoText[playerid]);
    
TextTiming[playerid] = SetTimerEx("HideInfoTextDraw"duration *10000"i"playerid);
    
AnimTiming[playerid] = SetTimerEx("AnimTimer"2000false"i"playerid);
    
DisplayingText[playerid] = 1;
    return 
1;
}
forward HideInfoTextDraw(playerid);
public 
HideInfoTextDraw(playerid)
{
    
PlayerTextDrawHide(playeridInfoText[playerid]);
    
DisplayingText[playerid] = 0;
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
CreateInfoTextDraw(playerid);
    return 
1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    
DisplayingText[playerid] = 0;
    
InfoText[playerid] = PlayerText:INVALID_TEXT_DRAW;
    return 
1;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)