OnPlayerStateChange - textdraw not show
#1

Hello,

I am making myself an easy speedometer. I got the job done, but when it came to final part - showing it, i experienced a problem. This is my code:
Код:
	if(newstate == PLAYER_STATE_DRIVER)
 	{
		new string[128];
		format(string, sizeof(string), "Vehicle: %s~n~Speed: %fkm/h", VehicleNames[VehicleModel[playerid]-400], floatround(GetVehicleSpeed(playerid), floatround_ceil));
		PlayerTextDrawSetString(playerid, tachometerText[playerid], string);
		PlayerTextDrawShow(playerid, tachometerText[playerid]);
		speedchange_timer[playerid] = SetTimerEx("SpeedChange",1000,true,"i",playerid);
		PlayerInfo[playerid][pInVeh] = 1;
	}
What I have tried:

1. Putting function into command - I executed command in car, while going certain speed. With no problem, everything worked out perfectly.

2. Putting function into OnPlayerEnterVehicle - I used it in (!ispassanger) parameter, because I want it to be shown only to driver. But as soon as I entered vehicle, I was not surprised. Nothing was shown.

3. Printing the function - I printed out whole entire function and found out, that nothing is executing. Like my state was not changing from pedestrian to driver.

4. Using old state. - I have put an oldstate, before checking for newstate, like this: if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER) but nothing shown up.

If anyone would be enable to help me, that reputation button for him, would be on point for me.

Thanks.
Reply
#2

Is the array VehicleModel correctly set up? You use it as index for VehicleNames, so an invalid ID would directly produce an array index out of bounds error. If you aren't sure if that might happen, install crashdetect to see if any errors occur.

Is this the only code in OnPlayerStateChange? If not, make sure it isn't prevented to run by a return or so.

Furthermore your format line isn't really correct. You use %f to show the speed, but the argument is not a float argument. floatround will round a float and return an integer. This will not be correctly shown when using %f for that.
Remove floatround or use %d instead.
Reply
#3

Mhm, maybe format line is not correctly..

Try this:
Код:
VehicleNames[GetVehicleModel(GetPlayerVehicleID(playerid))-400]
And try this function to take vehicle speed:
Код:
stock GetVehicleSpeed(vehicleid)
{
    new Float:xPos[3]:
    GetVehicleVelocity(vehicleid, xPos[0], xPos[1], xPos[2]);
    return floatround(floatsqroot(xPos[0] * xPos[0] + xPos[1] * xPos[1] + xPos[2] * xPos[2]) * 170.00);
}
And don't format with %f ( float ).

Your code is now:

Код:
	if(newstate == PLAYER_STATE_DRIVER)
 	{
		new string[128];
		format(string, sizeof(string), "Vehicle: %s~n~Speed: %dkm/h", VehicleNames[GetVehicleModel(GetPlayerVehicleID(playerid))-400], GetVehicleSpeed(GetPlayerVehicleID(playerid));
		PlayerTextDrawSetString(playerid, tachometerText[playerid], string);
		PlayerTextDrawShow(playerid, tachometerText[playerid]);
		speedchange_timer[playerid] = SetTimerEx("SpeedChange",1000,true,"i",playerid);
		PlayerInfo[playerid][pInVeh] = 1;
	}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)