Can anyone explain this?
#1

Код:
YCMD:engine(playerid, params[], help)
{
	new engine, lights, alarm, doors, bonnet, boot, objective;
	if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_RED, "Error: You are not inside a vehicle!");
	new vehicleid = GetPlayerVehicleID(playerid);
	if(IsPlayerInAnyVehicle(playerid) && engine == 0)
	{
		GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
		SetVehicleParamsEx(vehicleid, 1, lights, alarm, doors, bonnet, boot, objective);
		SendClientMessage(playerid, COLOR_GREEN, "Engine on!");
	}
	if(IsPlayerInAnyVehicle(playerid) && engine == 1)
	{
	    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
	    SetVehicleParamsEx(vehicleid, 0, lights, alarm, doors, bonnet, boot, objective);
	    SendClientMessage(playerid, COLOR_RED, "Engine off!");
	}
	return 1;
}
When I type this command in-game, it works fine to start the vehicle, however when I attempt to turn the vehicle off, it's shows both the "Engine on!" and then "Engine off!" in that order.
Reply
#2

Just replace
Код:
if(IsPlayerInAnyVehicle(playerid) && engine == 1)
with
Код:
else if(IsPlayerInAnyVehicle(playerid) && engine == 1)
Or you can simply just use "else". But in this case, else if makes it clear.
Reply
#3

Now it just continuously sends "Engine on!" and never turns the engine off. I have had this but for months and tried EVERYTHING.
Reply
#4

pawn Код:
CMD:engine(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    if(!vehicleid) return SendClientMessage(playerid, COLOR_RED, "Error: You are not inside a vehicle!");

    new engine, lights, alarm, doors, bonnet, boot, objective;
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);

    engine = !engine;
    SetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);

    if(engine) SendClientMessage(playerid, COLOR_GREEN, "Engine on!");
    else SendClientMessage(playerid, COLOR_RED, "Engine off!");

    return 1;
}
Reply
#5

I see how this code is working, and it does work for the most part, but whenever you get into the vehicle at first and type /engine, it sends the "Engine off!" client message first before starting it and sending "Engine on!".
Reply
#6

This is the code that ended up working incase anyone has a problem like this in the future.
Код:
YCMD:engine(playerid, params[], help)
{
	static engine, lights, alarm, doors, bonnet, boot, objective;
	new vehicleid = GetPlayerVehicleID(playerid);
	if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_RED, "Error: You are not inside a vehicle!");
	GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
	if(engine == 1)
	{
	    engine = 0;
     	    SCM(playerid, COLOR_RED, "Engine off!");
	}
	else
	{
	    engine = 1;
	    SCM(playerid, COLOR_GREEN, "Engine on!");
	}
 	SetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
	return 1;
}
Very sorry for the double post, should have edited my old one.
Reply
#7

^ When vehicles are first created their parameters are -1 (unset). Now, anything that is not 0 is considered to be 'true', so one must verify that engine is exactly equal to 1 before turning it off rather than checking if it "not 0".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)