SA-MP Forums Archive
What am I doing Wrong? (Vehicle params) - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: What am I doing Wrong? (Vehicle params) (/showthread.php?tid=194916)



What am I doing Wrong? (Vehicle params) - Hiitch - 30.11.2010

Ok, so heres what I wanted to do, I wanted to create a command where when i type /onlights, a timer is run that loops the lights so they turn on and off continuously, but that failed, and i'm not so sure why.

Here's the code:

pawn Код:
//this is what I use to set the lights on and off (flashing)
forward SetVehicleFlashingLights(playerid, vehicleid);
public SetVehicleFlashingLights(playerid, vehicleid)
{
    new
        engine,
        lights,
        alarm,
        doors,
        bonnet,
        boot,
        objective;

    if(IsPlayerInAnyVehicle(playerid))
    {
        if(engine != VEHICLE_PARAMS_ON)
            {
                SetVehicleParamsEx(vehicleid, VEHICLE_PARAMS_ON, lights, alarm, doors, bonnet, boot, objective);
            }
            else
                {
                    SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_OFF, alarm, doors, bonnet, boot, objective);
                    SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_ON, alarm, doors, bonnet, boot, objective);
                }
        GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_OFF, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_ON, alarm, doors, bonnet, boot, objective);
            }
}
pawn Код:
//this turns off the flashing lights, by running itself once, you'll see in the command, what I did.

forward TurnOffFlashingLights(playerid, vehicleid);
public TurnOffFlashingLights(playerid, vehicleid)
{
    new
        engine,
        lights,
        alarm,
        doors,
        bonnet,
        boot,
        objective;

    while(IsPlayerInAnyVehicle(playerid))
    {
        GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_OFF, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_ON, alarm, doors, bonnet, boot, objective);
    }
}

COMMANDS

pawn Код:
new kmiTimer; //for timer, outside (global var)

//this is supposed to start the timer
dcmd_OnLights(playerid, params[])
{
    #pragma unused params
    new
        vehID;
       
    vehID = GetPlayerVehicleID(playerid);
   
    if(IsPlayerInAnyVehicle(playerid))
    {
        kmiTimer = SetTimerEx("SetVehicleFlashingLights", 30000, true, "d", vehID);
        SendClientMessage(playerid, 0xFFFFFFFF, "[ ! ] Vehicle Lights turned on. If nothing happens, there is something wrong with your code.");
    }
    else
    {
        SendClientMessage(playerid, 0xFF0000FF, "[ ! ] You aren't in a vehicle!");
    }
    return true;
}


//this is supposed to turn them off.
dcmd_OffLights(playerid, params[])
{
    #pragma unused params
    new
        vehID;
       
    vehID = GetPlayerVehicleID(playerid);
   
    if(IsPlayerInAnyVehicle(playerid))
    {
        KillTimer(kmiTimer);
        SendClientMessage(playerid, 0xFF0000FF, "[ ! ] Lights killed successfully.");
        SetTimerEx("TurnOffFlashingLights", 1, true, "d", vehID);
    }
    else
    {
        SendClientMessage(playerid, 0xFF0000FF, "[ ! ] You aren't in a vehicle!");
    }
    return true;
}



Re: What am I doing Wrong? (Vehicle params) - Grim_ - 30.11.2010

You know that this code will run forever until the player gets out?
pawn Код:
while(IsPlayerInAnyVehicle(playerid))
    {
        GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_OFF, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_ON, alarm, doors, bonnet, boot, objective);
    }



Re: What am I doing Wrong? (Vehicle params) - Hiitch - 30.11.2010

Quote:
Originally Posted by Grim_
Посмотреть сообщение
You know that this code will run forever until the player gets out?
pawn Код:
while(IsPlayerInAnyVehicle(playerid))
    {
        GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_OFF, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(vehicleid, engine, VEHICLE_PARAMS_ON, alarm, doors, bonnet, boot, objective);
    }
Mh, thats kinda what I want it to do though, I want the lights to flash on and off, as if it were like a cop car (yeah, I know it goes on and on), but I can't seem to get anything to when when i type /onlights.

Anyways, if I did something wrong, please help me and try to correct it.


Re: What am I doing Wrong? (Vehicle params) - Grim_ - 30.11.2010

I honestly do not feel like writing new code, or even looking at it, so I'll give you what is a better approach than "hanging" your server:

- Make a variable to store the vehicle's lights status
- Run a 1 second timer checking the variable. If the lights are one, set them off, and vice-verse.

A one second timer would be fine for this situation. The problem with your code is it was re-setting the lights on/off multiple times a second, so you probably couldn't notice the differences. I'm also sure there are other ways for doing this, more efficiently, but like I said, not in the mood.


Re: What am I doing Wrong? (Vehicle params) - Hiitch - 30.11.2010

Quote:
Originally Posted by Grim_
Посмотреть сообщение
I honestly do not feel like writing new code, or even looking at it, so I'll give you what is a better approach than "hanging" your server:

- Make a variable to store the vehicle's lights status
- Run a 1 second timer checking the variable. If the lights are one, set them off, and vice-verse.

A one second timer would be fine for this situation. The problem with your code is it was re-setting the lights on/off multiple times a second, so you probably couldn't notice the differences. I'm also sure there are other ways for doing this, more efficiently, but like I said, not in the mood.
Ah ok, thanks for the explanation. I didn't even know I would be hanging my server with all that code I presented.


Re: What am I doing Wrong? (Vehicle params) - bn102 - 01.12.2010

https://sampforum.blast.hk/showthread.php?tid=194636
Take a look at that.
look at the /lightsflash command...

Hope it helped!