Textdraw problem
#1

Hi, I have made a filterscript that shows the player's vehicle health,
but I have a little problem.
When only one player is in a vehicle, it works perfectly,
but if two or more players are in a vehicle, the textdraw starts to flash.
I think I'm doing something wrong, but I can't find out what.
Any ideas?

Код:
new Text:Text;
new Float:VehicleHealth;
new string[128];

public OnPlayerUpdate(playerid)
{
	if(IsPlayerInAnyVehicle(playerid) == 1)
	{
		new VehicleID = GetPlayerVehicleID(playerid);
		GetVehicleHealth(VehicleID, VehicleHealth);
        	VehicleHealth = floatround(VehicleHealth / 10, floatround_round);
		format(string,sizeof(string), "%0.0f%%", VehicleHealth);
		TextDrawDestroy(Text);
		Text = TextDrawCreate(5, 433, string);
		TextDrawSetShadow(Text, 0);
		TextDrawUseBox(Text, 1);
		TextDrawBoxColor(Text, 0x202020FF);
		TextDrawTextSize(Text, 48, 0);
  		if(VehicleHealth <= 100) TextDrawColor(Text,0x00FF00FF);
		if(VehicleHealth < 90) TextDrawColor(Text,0x40FF00FF);
  		if(VehicleHealth < 80) TextDrawColor(Text,0x80FF00FF);
  		if(VehicleHealth < 70) TextDrawColor(Text,0xC0FF00FF);
 		if(VehicleHealth < 60) TextDrawColor(Text,0xFFFF00FF);
 		if(VehicleHealth < 50) TextDrawColor(Text,0xFFC000FF);
		if(VehicleHealth < 40) TextDrawColor(Text,0xFF8000FF);
		if(VehicleHealth < 30) TextDrawColor(Text,0xFF4000FF);
		if(VehicleHealth < 25) TextDrawColor(Text,0xFF0000FF);
		TextDrawShowForPlayer(playerid, Text);
	}
	if(IsPlayerInAnyVehicle(playerid) == 0) TextDrawDestroy(Text);
	return 1;
}
Reply
#2

Код:
new Text:Text[MAX_PLAYERS];
new Float:VehicleHealth;
new string[128];

public OnPlayerUpdate(playerid)
{
	if(IsPlayerInAnyVehicle(playerid) == 1)
	{
		new VehicleID = GetPlayerVehicleID(playerid);
		GetVehicleHealth(VehicleID, VehicleHealth);
        VehicleHealth = floatround(VehicleHealth / 10, floatround_round);
		format(string,sizeof(string), "%0.0f%%", VehicleHealth);
		TextDrawDestroy(Text[playerid]);
		Text[playerid] = TextDrawCreate(5, 433, string);
		TextDrawSetShadow(Text[playerid], 0);
		TextDrawUseBox(Text[playerid], 1);
		TextDrawBoxColor(Text[playerid], 0x202020FF);
		TextDrawTextSize(Text[playerid], 48, 0);
  		if(VehicleHealth <= 100) TextDrawColor(Text[playerid],0x00FF00FF);
		if(VehicleHealth < 90) TextDrawColor(Text[playerid],0x40FF00FF);
  		if(VehicleHealth < 80) TextDrawColor(Text[playerid],0x80FF00FF);
  		if(VehicleHealth < 70) TextDrawColor(Text[playerid],0xC0FF00FF);
 		if(VehicleHealth < 60) TextDrawColor(Text[playerid],0xFFFF00FF);
 		if(VehicleHealth < 50) TextDrawColor(Text[playerid],0xFFC000FF);
		if(VehicleHealth < 40) TextDrawColor(Text[playerid],0xFF8000FF);
		if(VehicleHealth < 30) TextDrawColor(Text[playerid],0xFF4000FF);
		if(VehicleHealth < 25) TextDrawColor(Text[playerid],0xFF0000FF);
		TextDrawShowForPlayer(playerid, Text[playerid]);
	}
	if(IsPlayerInAnyVehicle(playerid) == 0) TextDrawDestroy(Text[playerid]);
	return 1;
}
Try this.
Reply
#3

That doesn't work either, now the textdraw flashes even if the other player isn't in a vehicle.
Reply
#4

You shouldnt create your textdraw in OnPlayerUpdate since it gets called very frequently

The whole code optimised
pawn Код:
new
    Text: gtpVehicleHealth[MAX_PLAYERS] = {Text: INVALID_TEXT_DRAW, ...};
pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate) {
    switch((newstate ^ oldstate) & newstate) {
        case PLAYER_STATE_DRIVER: {
            new Text: text = gtpVehicleHealth[playerid] =
            TextDrawCreate(5, 433, "_");
            TextDrawSetShadow(text, 0);
            TextDrawUseBox(text, 1);
            TextDrawBoxColor(text, 0x202020FF);
            TextDrawTextSize(text, 48, 0);
            TextDrawShowForPlayer(playerid, text);
        }
    }
    switch((oldstate ^ newstate) & oldstate) {
        case PLAYER_STATE_DRIVER: {
            TextDrawDestroy(gtpVehicleHealth[playerid]);
            gtpVehicleHealth[playerid] = Text: INVALID_TEXT_DRAW;
        }
    }
    return true;
}
pawn Код:
public OnPlayerUpdate(playerid) {
    if(gtpVehicleHealth[playerid] != Text: INVALID_TEXT_DRAW) {
        static
            string[16],
            Float: vHealth;
        static const
            hColors[] = {
                0x00FF00FF,
                0x40FF00FF,
                0x80FF00FF,
                0xC0FF00FF,
                0xFFFF00FF,
                0xFFC000FF,
                0xFF8000FF,
                0xFF4000FF,
                0xFF0000FF
        };
        GetVehicleHealth(GetPlayerVehicleID(playerid), vHealth);
        format(string, sizeof string, "%.0f%%", vHealth);
        TextDrawSetString(gtpVehicleHealth[playerid], string);
        TextDrawColor(gtpVehicleHealth[playerid],
            hColors[sizeof hColors - max(1, floatround(((vHealth - 250.0) / (750.0 / sizeof hColors)), floatround_round))]
        );
    }
    return true;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)