SA-MP Forums Archive
Starting Engine dures twice :? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Starting Engine dures twice :? (/showthread.php?tid=516050)



Starting Engine dures twice :? - Mey6155 - 29.05.2014

My super duper awesome code:
pawn Код:
CMD:engine(playerid, params[])
{
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new vehicle = GetPlayerVehicleID(playerid);
        new engine,lights,alarm,doors,bonnet,boot,objective;
        GetVehicleParamsEx(vehicle,engine,lights,alarm,doors,bonnet,boot,objective);
        if(engine == 0)
        {
            SetVehicleParamsEx(vehicle,1,1,alarm,doors,bonnet,boot,objective);
            new string[256], name[MAX_PLAYER_NAME+1];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turn the ignition and turns off the engine", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
        }
        else
        {
            SetVehicleParamsEx(vehicle,0,0,alarm,doors,bonnet,boot,objective);
            new string[256], name[MAX_PLAYER_NAME+1];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turn the ignition and starts the engine", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
        }
    }
    return 1;
}
Also pressing submission(2):
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_SUBMISSION))
    {
        if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
        {
            new vehicle = GetPlayerVehicleID(playerid);
            new engine,lights,alarm,doors,bonnet,boot,objective;
            GetVehicleParamsEx(vehicle,engine,lights,alarm,doors,bonnet,boot,objective);
            if(engine == 0)
            {
                SetVehicleParamsEx(vehicle,1,1,alarm,doors,bonnet,boot,objective);
                new string[256], name[MAX_PLAYER_NAME+1];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "* %s turn the ignition and start the engine", name);
                SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            }
            else
            {
                SetVehicleParamsEx(vehicle,0,0,alarm,doors,bonnet,boot,objective);
                new string[256], name[MAX_PLAYER_NAME+1];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "* %s turn the ignition and turns off the engine", name);
                SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            }
        }
    }
    return 1;
}

Thx


Re: Starting Engine dures twice :? - R0 - 29.05.2014

well,it's because you dont define the engine as turned on after turning it on,and you must add [vehicle] to engine,code should be: and do the same for the other code
pawn Код:
CMD:engine(playerid, params[])
{
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new vehicle = GetPlayerVehicleID(playerid);
        new engine,lights,alarm,doors,bonnet,boot,objective;
        GetVehicleParamsEx(vehicle,engine,lights,alarm,doors,bonnet,boot,objective);
        if(engine[vehicle] == 0)
        {
            SetVehicleParamsEx(vehicle,1,1,alarm,doors,bonnet,boot,objective);
            new string[256], name[MAX_PLAYER_NAME+1];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turn the ignition and turns off the engine", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            engine[vehicle] = 1;
        }
        else
        {
            SetVehicleParamsEx(vehicle,0,0,alarm,doors,bonnet,boot,objective);
            new string[256], name[MAX_PLAYER_NAME+1];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turn the ignition and starts the engine", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            engine[vehicle] = 0;
        }
    }
    return 1;
}



Re: Starting Engine dures twice :? - d3ll - 29.05.2014

pawn Код:
CMD:engine(playerid, params[])
{
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new vehicle = GetPlayerVehicleID(playerid);
        new engine,lights,alarm,doors,bonnet,boot,objective;
        GetVehicleParamsEx(vehicle,engine,lights,alarm,doors,bonnet,boot,objective);
        if(engine == 0)
        {
            SetVehicleParamsEx(vehicle,1,1,alarm,doors,bonnet,boot,objective);
            new string[256], name[MAX_PLAYER_NAME+1];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turn the ignition and turns off the engine", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            return 1;
        }
        else
        {
            SetVehicleParamsEx(vehicle,0,0,alarm,doors,bonnet,boot,objective);
            new string[256], name[MAX_PLAYER_NAME+1];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turn the ignition and starts the engine", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            return 1;
        }
    }
    return 1;
}
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_SUBMISSION))
    {
        if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
        {
            new vehicle = GetPlayerVehicleID(playerid);
            new engine,lights,alarm,doors,bonnet,boot,objective;
            GetVehicleParamsEx(vehicle,engine,lights,alarm,doors,bonnet,boot,objective);
            if(engine == 0)
            {
                SetVehicleParamsEx(vehicle,1,1,alarm,doors,bonnet,boot,objective);
                new string[256], name[MAX_PLAYER_NAME+1];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "* %s turn the ignition and start the engine", name);
                SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            return 1;
            }
            else
            {
                SetVehicleParamsEx(vehicle,0,0,alarm,doors,bonnet,boot,objective);
                new string[256], name[MAX_PLAYER_NAME+1];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "* %s turn the ignition and turns off the engine", name);
                SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            return 1;
            }
        }
    }
    return 1;
}
You needed to add a return value.


Re: Starting Engine dures twice :? - Mey6155 - 29.05.2014

Error:
Код:
F:\Mustafa\Dutch-RealLife\gamemodes\Haci.pwn(1047) : error 028: invalid subscript (not an array or too many subscripts): "engine"
F:\Mustafa\Dutch-RealLife\gamemodes\Haci.pwn(1047) : warning 215: expression has no effect
F:\Mustafa\Dutch-RealLife\gamemodes\Haci.pwn(1047) : error 001: expected token: ";", but found "]"
F:\Mustafa\Dutch-RealLife\gamemodes\Haci.pwn(1047) : error 029: invalid expression, assumed zero
F:\Mustafa\Dutch-RealLife\gamemodes\Haci.pwn(1047) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


4 Errors.
Line with: if(engine[vehicle] == 0)


Re: Starting Engine dures twice :? - Mey6155 - 29.05.2014

Well @d3ll its not working, I press 2 or /engine it says you started the engine nothing started then I did it again engine is off then it started I had to press twice.


Re: Starting Engine dures twice :? - nickdodd25 - 29.05.2014

Looks like you have the engine variable wrong. GetVehicleParamsEx will show engine as 1 when its running and 0 when its off, so it keeps starting the engine with your code.

https://sampwiki.blast.hk/wiki/GetVehicleParamsEx


Re: Starting Engine dures twice :? - Mey6155 - 29.05.2014

Its not wrong if its engine == 0 it would set it too 1 and else engine == 1 then it would make it 0.


Re: Starting Engine dures twice :? - R0 - 29.05.2014

you have defined engine as:
pawn Код:
new engine;
right?if ye change it to
pawn Код:
new engine[MAX_VEHICLES];



Re: Starting Engine dures twice :? - bogdyutzu - 29.05.2014

PHP код:
CMD:engine(playeridparams[])
{
    
#define charsmax(%1) sizeof(%1)-1
    
    
if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new 
tmpbEnginevehicleid GetPlayerVehicleID(playerid);
        
GetVehicleParamsEx(vehicleidbEnginetmptmptmptmptmptmp);
       
        new 
bState = (bEngine == VEHICLE_PARAMS_OFF || bEngine == VEHICLE_PARAMS_UNSET);
        
SetVehicleParamsEx(vehicleidbStatebState, -1, -1, -1, -1, -1);
       
        new 
string[64];
        
GetPlayerName(playeridstringcharsmax(string));
        
format(stringcharsmax(string), "* %s turn the ignition and %s the engine."stringbState ? ("starts") : ("turns off"));
        
        
SendNearbyMessage(playerid15stringCOLOR_PURPLECOLOR_PURPLECOLOR_PURPLECOLOR_PURPLECOLOR_PURPLE)       
    }
    return 
1;

Use this code. It don't need any extra variables.


Re: Starting Engine dures twice :? - [NWA]Hannes - 29.05.2014

Quote:
Originally Posted by R0
Посмотреть сообщение
you have defined engine as:
pawn Код:
new engine;
right?if ye change it to
pawn Код:
new engine[MAX_VEHICLES];
The engine variable is created and used within the command for GetVehicleParamsEx, he shouldn't need to redefine it for the vehicle.

As you can read here: https://sampwiki.blast.hk/wiki/GetVehicleParamsEx
Quote:

"If a parameter is unset (SetVehicleParamsEx not used beforehand) the value will be -1 ('unset')."

Therefore you need to check if the engine parameter is unset aswell, not just 0 or 1.

Read the changes I made carefully and learn from them, also read the notes on the Wiki before you use a function that you do not know anything about.
pawn Код:
CMD:engine(playerid, params[])
{
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new vehicle = GetPlayerVehicleID(playerid);
        new engine, lights, alarm, doors, bonnet, boot, objective;
        GetVehicleParamsEx(vehicle, engine, lights, alarm, doors, bonnet, boot, objective);
        if(engine == 0 || engine == -1)
        {
            SetVehicleParamsEx(vehicle, 1, 1, alarm, doors, bonnet, boot, objective);
            new string[128], name[24];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turns the ignition and starts the engine.", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
        }
        else if(engine == 1)
        {
            SetVehicleParamsEx(vehicle, 0, 0, alarm, doors, bonnet, boot, objective);
            new string[128], name[24];
            GetPlayerName(playerid, name, sizeof(name));
            format(string, sizeof(string), "* %s turns the ignition and turns off the engine.", name);
            SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
        }
    }
    return 1;
}
     
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_SUBMISSION))
    {
        if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
        {
            new vehicle = GetPlayerVehicleID(playerid);
            new engine, lights, alarm, doors, bonnet, boot, objective;
            GetVehicleParamsEx(vehicle, engine, lights, alarm, doors, bonnet, boot, objective);
            if(engine == 0 || engine == -1)
            {
                SetVehicleParamsEx(vehicle, 1, 1, alarm, doors, bonnet, boot, objective);
                new string[128], name[24];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "* %s turns the ignition and starts the engine.", name);
                SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            }
            else
            {
                SetVehicleParamsEx(vehicle, 0, 0, alarm, doors, bonnet, boot, objective);
                new string[128], name[24];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "* %s turns the ignition and turns off the engine.", name);
                SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
            }
        }
    }
    return 1;
}