Vehicle health help...
#1

The following code works fine to get the vehicle health for the first user in the game, as soon as an other player spawns or gets a vehicle all things go wrong
I can't figure it out how to make it work for multiple players...
I must somehow make it remember the correct timer for the correct user, right? Any idea's?

Код:
#include <a_samp>
#include <a_npc>

new Text:InfoBox;
new IBoxString[128];
new ITimer[999];
new PID[999];

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print("     Vehicle Health Script     ");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
 	KillTimer(ITimer[PID[playerid]]);
	TextDrawHideForPlayer(PID[playerid], InfoBox);
	return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
	if(newstate == PLAYER_STATE_DRIVER)
  {
  	InfoBox = TextDrawCreate(200.0, 360.0, " ");
		TextDrawUseBox(InfoBox,0);
		TextDrawTextSize(InfoBox, 440, 410);
		TextDrawBoxColor(InfoBox,0x00000066);
		TextDrawSetOutline(InfoBox,1);
		TextDrawLetterSize(InfoBox,0.5,1.2);
		TextDrawSetShadow(InfoBox,0);
		PID[playerid] = playerid;
		ITimer[PID[playerid]] = SetTimer("UInfoBox", 100, 1);
	}
	else
	{
	 	KillTimer(ITimer[PID[playerid]]);
		TextDrawHideForPlayer(PID[playerid], InfoBox);
	}
	return 1;
}

forward UInfoBox();
public UInfoBox()
{
	new vehicleid = GetPlayerVehicleID(PID[playerid]);
    new Float:VHealth;
	GetVehicleHealth(vehicleid, VHealth);
  	VHealth = (VHealth/10);
  	
	format(IBoxString, 128, "~r~Vehicle Health: ~y~%.1f~n~~r~", VHealth);
	TextDrawHideForPlayer(PID[playerid], InfoBox);
	TextDrawSetString(InfoBox, IBoxString);
	TextDrawShowForPlayer(PID[playerid], InfoBox);
}
Reply
#2

its because you didnt define the string for individual players, you defined it for all players together. if you use the string variable as global then you should visit http://forum.sa-mp.com/index.php?topic=134164.0 else do the string variable as local inside the Box function


SOLUTION
Код:
#include <a_samp>
#include <a_npc>

new Text:InfoBox[MAX_PLAYERS];
new ITimer[MAX_PLAYERS];
new IBoxString[MAX_PLAYERS][128]; // as we use it as global, we don't want any conflicts with healths of other players
new Float:VHealth[MAX_VEHICLES];

forward UInfoBox(playerid); // defined player id removes need of variable PID[];

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print("     Vehicle Health Script     ");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
 	KillTimer(ITimer[playerid]);
	TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
	if(newstate == PLAYER_STATE_DRIVER)
    {
  	    InfoBox[playerid] = TextDrawCreate(200.0, 360.0, " ");
		TextDrawUseBox(InfoBox[playerid],0);
		TextDrawTextSize(InfoBox[playerid], 440, 410);
		TextDrawBoxColor(InfoBox[playerid],0x00000066);
		TextDrawSetOutline(InfoBox[playerid],1);
		TextDrawLetterSize(InfoBox[playerid],0.5,1.2);
		TextDrawSetShadow(InfoBox[playerid],0);
		ITimer[playerid] = SetTimerEx("UInfoBox", 250, 1,"i",playerid); // changed the timer to 250, no need top be faster unless u use it for speed detection
	}
	else
	{
	 	KillTimer(ITimer[playerid]);
		TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	}
	return 1;
}

public UInfoBox(playerid)
{
	GetVehicleHealth(GetPlayerVehicleID(playerid), VHealth[GetPlayerVehicleID(playerid)]); 	
	format(IBoxString[playerid], sizeof(IBoxString[])-1, "~r~Vehicle Health: ~y~%.1f~n~~r~", VHealth[GetPlayerVehicleID(playerid)]/10);
	TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	TextDrawSetString(InfoBox[playerid], IBoxString[playerid]);
	TextDrawShowForPlayer(playerid, InfoBox[playerid]);
}
TRY IT :P I did not test it but i think its alright UPDATED!!!! Should work now with no changes made
Reply
#3

first off, thanks!
i'm getting closer...

changed 'i' to "i" in settimerex: (dubble quotes)
ITimer[playerid] = SetTimerEx("UInfoBox", 250, 1, "i",playerid);

it still shows the health of a car to all players, with the health of the players own car overlapping it every 250 milliseconds. so the playerid to draw the infobox to is not correct yet:

i tried this:

if(playerid == ITimer[playerid])
{
TextDrawShowForPlayer(playerid, InfoBox);
}

no joy
Reply
#4

Remove %, its not soposed to be there, as this is special timer case.

I made a tiny mistake there, i seted 'i' but should been "i" ...
Also seted that Text:Box to [MAX_PLAYERS] althought ther eis no need to be so, it just to be sure for no other player conflict

I updated the script, if you dont make any changes it should work
Reply
#5

right, just saw the % wasn't needed on the wiki
the health is still showing to all players with each its own vehiclehealth overlapping the others
prob should check somewhere if the infobox to draw is being drawed to the correct playerid/...?

Quote:
Originally Posted by TMasters.tk
Remove %, its not soposed to be there, as this is special timer case.

I made a tiny mistake there, i seted 'i' but should been "i" ...
Also seted that Text:Box to [MAX_PLAYERS] althought ther eis no need to be so, it just to be sure for no other player conflict

I updated the script, if you dont make any changes it should work
or would the problem be that the first player has id 0 ?
Reply
#6

ok, found it...
UInfoBox needed a 'return 1;' (those foolish things)

Here's the code for if anyone is interested:
Haven't fully tested it, but seemed to work with 2 players...
(Using it as a filescript)
Edited credits

Код:
#include <a_samp>
#include <a_npc>

new Text:InfoBox[MAX_PLAYERS];
new ITimer[MAX_PLAYERS];
new IBoxString[MAX_PLAYERS][128];
new Float:VHealth[MAX_VEHICLES];

forward UInfoBox(playerid);

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print("     Vehicle Health Script     ");
	print("  Credits to: ExoSanty, TMasters.tk  ");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
 	KillTimer(ITimer[playerid]);
	TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
	if(newstate == PLAYER_STATE_DRIVER)
 	{
 		InfoBox[playerid] = TextDrawCreate(200.0, 360.0, " ");
		TextDrawUseBox(InfoBox[playerid],0);
		TextDrawTextSize(InfoBox[playerid], 440, 410);
		TextDrawBoxColor(InfoBox[playerid],0x00000066);
		TextDrawSetOutline(InfoBox[playerid],1);
		TextDrawLetterSize(InfoBox[playerid],0.5,1.2);
		TextDrawSetShadow(InfoBox[playerid],0);
		ITimer[playerid] = SetTimerEx("UInfoBox", 250, 1,"i",playerid);
	}
	else
	{
	 	KillTimer(ITimer[playerid]);
		TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	}
	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
 	KillTimer(ITimer[playerid]);
	TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	return 1;
}

public UInfoBox(playerid)
{
	GetVehicleHealth(GetPlayerVehicleID(playerid), VHealth[GetPlayerVehicleID(playerid)]);
	format(IBoxString[playerid], sizeof(IBoxString[])-1, "~r~Vehicle Health: ~y~%.1f~n~~r~", VHealth[GetPlayerVehicleID(playerid)]/10);
	TextDrawHideForPlayer(playerid, InfoBox[playerid]);
	TextDrawSetString(InfoBox[playerid], IBoxString[playerid]);
	TextDrawShowForPlayer(playerid, InfoBox[playerid]);
	return 1;
}
i'll get busy on speed now *g*
Reply
#7

if you need any kind of help in future, contact me on MSN: gmx_spirit@live.com
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)