vehicle auto-repair timer
#1

Hi,

With the vehicle auto-repair code below, is it ok to have the timer starting OnGameModeInit and running all the time? I know the timer is stopping at the if statements, i just wonder if there is a better way.

Код:
public OnGameModeInit()
{
  SetTimer("autorepair", 2000, 1);
  return 1;
}

// Function AUTOREPAIR (Includes Bodywork)

forward autorepair();
public autorepair()
{
  for(new playerid=0; playerid<MAX_PLAYERS; playerid++)
	{
	SendClientMessage(playerid, 0xFFFFFFFF, "Vehicle Repair");
    if(GetPlayerState(playerid) == 2)
		{
		SendClientMessage(playerid, 0xFFFFFFFF, "Vehicle Repair: Driver In Vehicle");
		new Float:health, cid;
		cid = GetPlayerVehicleID(playerid);
		GetVehicleHealth(cid, health);
		if (health < 990)
		    {
			RepairVehicle(cid);
			SendClientMessage(playerid, 0xFFFFFFFF, "Vehicle Repair: Vehicle Repaired");
			}
		}
	}
	return 1;
}
// End of AUTOREPAIR (Includes Bodywork)
Reply
#2

Start the timer when a player enters a vehicle and use KillTimer to stop it when they exit a vehicle.
Reply
#3

Quote:
Originally Posted by V1ceC1ty
Start the timer when a player enters a vehicle and use KillTimer to stop it when they exit a vehicle.
SetPlayerVoice(playerid,Elvis) Thank you very much
Reply
#4

Not only is this running a command every 2 seconds of your MAX_PLAYERS variable, which is very uneconomical for your server; but every player in a car is going to be spammed with "Vehicle Repair: Vehicle Repaired" every 2 seconds.
Reply
#5

Quote:
Originally Posted by Kinetic
Not only is this running a command every 2 seconds of your MAX_PLAYERS variable, which is very uneconomical for your server; but every player in a car is going to be spammed with "Vehicle Repair: Vehicle Repaired" every 2 seconds.
Why would vehicle repair need MAX_PLAYERS? Also i put the comments in so i can see when the code does something cos im a lil clucker.
Reply
#6

Ok it works, thanks heaps V1ceC1ty. Had to use OnPlayerStateChange instead of OnPlayerEnterVehicle because i am spawning into my cars through a dialog menu.

Код:
// Function AUTOREPAIR (Includes Bodywork)


forward autorepair();
public autorepair()
{
  for(new playerid=0; playerid<MAX_PLAYERS; playerid++)
	{
 		SendClientMessage(playerid, 0xFFFFFFFF, "Vehicle Repair: Driver In Vehicle");
		new Float:health, cid;
		cid = GetPlayerVehicleID(playerid);
		GetVehicleHealth(cid, health);
		if (health < 990)
		{
			RepairVehicle(cid);
			SendClientMessage(playerid, 0xFFFFFFFF, "Vehicle Repair: Vehicle Repaired");
		}
 	}
	return 1;
}

new artimer;
public OnPlayerStateChange(playerid,newstate,oldstate)
{
	if(GetPlayerState(playerid) == 2)
  	{
  	artimer=SetTimer("autorepair", 2000, true);
  }
  
  return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
  KillTimer(artimer);
  return 1;
}

// End of AUTOREPAIR (Includes Bodywork)
Reply
#7

OK i found a bug already with the code above, when i teleport out of car it doesnt register OnPlayerExitVehicle command so i had to use all player states. Istill would like opinions on the new code please.

Код:
// Function AUTOREPAIR (Includes Bodywork)


forward autorepair();
public autorepair()
{
  for(new playerid=0; playerid<MAX_PLAYERS; playerid++)
	{
 		new Float:health, cid;
		cid = GetPlayerVehicleID(playerid);
		GetVehicleHealth(cid, health);
		if (health < 990)
		{
			RepairVehicle(cid);
			SendClientMessage(playerid, 0xFFFFFFFF, "Vehicle Repair: Vehicle Repaired");
		}
 	}
	return 1;
}

new artimer;
public OnPlayerStateChange(playerid,newstate,oldstate)
{
	if(newstate == 2)
  	{
  	artimer=SetTimer("autorepair", 2000, true);
  	SendClientMessage(playerid,0xFFFFFFFF,"ST");
  }
  if(oldstate == 2)
  	{
  	KillTimer(artimer);
  	SendClientMessage(playerid,0xFFFFFFFF,"KT");
  }

  new string[48];
  format(string, sizeof(string), "You changed from state %d to state %d!",oldstate,newstate);
  SendClientMessage(playerid,0xFFFFFFFF,string);
  return 1;
}

// End of AUTOREPAIR (Includes Bodywork)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)