Jump to content

Flashing car Lights


DakiLLa

Recommended Posts

Hi there! I want to make a flashing ligts on vehicles when toggled L, and when L toggled again, stop flashing. But I dont understand what i need to do. I think, it should be a timer for function of toggle lights or not ? :?:

Script from vehicleligts resource

  
... 
function toggleVehicleLights ( player, key, state ) 
    if ( getPlayerOccupiedVehicleSeat ( player ) == 0 ) then 
        local veh = getPlayerOccupiedVehicle ( player ) 
        if ( getVehicleOverrideLights ( veh ) ~= 2 ) then 
            setVehicleOverrideLights ( veh, 2 ) 
        else 
            setVehicleOverrideLights ( veh, 1 ) 
        end 
    end 
end 
  

It will be effectly for cop-cars, like in Most Wanted.

Thank's for replies :wink:

Edited by Guest
Link to comment

When i use a timer to update a text (e.g. Health) i let him write the information, then i let him clear all and ket him write again. Then i set this function into a timer (e.g. to 50ms )

Then he updates the Health each 50 ms ;)

But i dont know if that works with your project. Updating a function without clearing the event before .. lol. When i did that, my MTA used 2 GB RAM after10 seconds of playing :D

Link to comment
When i use a timer to update a text (e.g. Health) i let him write the information, then i let him clear all and ket him write again. Then i set this function into a timer (e.g. to 50ms )

Then he updates the Health each 50 ms ;)

But i dont know if that works with your project. Updating a function without clearing the event before .. lol. When i did that, my MTA used 2 GB RAM after10 seconds of playing :D

50 ms repeadetly? 1000:50=20 times/second. If your script then is approx 5 lines each time (example) then your PC will lag! I think like 100ms is even too fast! Although when you set it higher, players will tell it lag. So what? Do they want the GUI, or their PC to lag?

Link to comment

what about a clientside script using the onClientRender event like this?

function set () 
    if flashing == false or not flashing then 
        flashing == true 
    else 
        flashing == false 
    end 
end 
  
addCommandHandler ("setflash", set) 
  
function flash () 
    if flashing == true then 
        if getTickCount () - previousTickCount >= 250 or not previousTickCount then 
            previousTickCount = getTickCount () 
            if ( getVehicleOverrideLights ( playerVehicle ) ~= 2 ) then 
                setVehicleOverrideLights ( playerVehicle, 2 ) 
            else 
                setVehicleOverrideLights ( playerVehicle, 1 ) 
            end 
        end 
    end 
end 
  
addEventHandler ("onClientRender", getLocalPlayer(), flash) 

i could not test the script yet so there might be typing mistakes and i hope turning on/off the light 4 times per second is fast enough ;)

Link to comment
what about a clientside script using the onClientRender event like this?
function set () 
    if flashing == false or not flashing then 
        flashing == true 
    else 
        flashing == false 
    end 
end 
  
addCommandHandler ("setflash", set) 
  
function flash () 
    if flashing == true then 
        if getTickCount () - previousTickCount >= 250 or not previousTickCount then 
            previousTickCount = getTickCount () 
            if ( getVehicleOverrideLights ( playerVehicle ) ~= 2 ) then 
                setVehicleOverrideLights ( playerVehicle, 2 ) 
            else 
                setVehicleOverrideLights ( playerVehicle, 1 ) 
            end 
        end 
    end 
end 
  
addEventHandler ("onClientRender", getLocalPlayer(), flash) 

i could not test the script yet so there might be typing mistakes and i hope turning on/off the light 4 times per second is fast enough ;)

I LOLed @ line 2: "if flashing == false or not flashing then". Or not flashing... xD

Anyway, onClientRender is executed every frame, right? If so, you might want to set something like a var that counts how many times it had been executed. Or does getTickCount serve for that? =/

And note that this doesn't seem to be synched... I think others want to see these flashing headlights too.

@Shigawire: Still, is that a piece of code to check their ammo etc?!? That's just way too big! Also, try out some of the events I gave you once! Might decrease it's amount of lines to... Dunno... Alot less?

Link to comment

Based on Mr. Hankey's code:

  
addCommandHandler("setflash", 
    function() 
        flashing = not flashing 
    end 
) 
  
addEventHandler("onClientRender",getLocalPlayer(), 
    function() 
        local v = getPlayerOccupiedVehicle(getLocalPlayer()) 
  
        if v and flashing and getTickCount () > (previousTickCount or 0) then 
            previousTickCount = getTickCount() + 250 
            setVehicleOverrideLights(v,(getVehicleOverrideLights ( v ) == 2) and 1 or 2 ) 
        end 
    end 
) 
  

Link to comment

and / or:

timing = { } -- make the variable a table 
  
function toggleFlashingLights ( thePlayer ) -- thePlayer who initiated the command 
        if timing[thePlayer] then 
                killTimer ( timing[thePlayer] ) 
                timing[thePlayer] = nil -- remove the old variable 
        else 
                timing[thePlayer] = setTimer ( toggleVehicleLights, 250, 500, thePlayer ) -- set a timer every 250ms for 500 times, in case the timer overruns, for thePlayer 
        end 
end 
addCommandHandler ( "flash", toggleFlashingLights ) 
  
function onPlayerQuit ( reason ) 
        if timing[source] then -- if the timer is active when a player quits 
                killTimer ( timing[source] ) 
                timing[source] = nil 
        end 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), onPlayerQuit ) 

bindKey ( source, "I", "down", "toggleFlashingLights" ) -- assuming its onJoin 

u make a variable for each individual ( timing[thePlayer] ) so if someone kills their timer, it won't kill it for everyone. u need timing = { } so u can use timing[whateverulikeinhereincludingtextbuttextneedstobeinquations] otherwise u can only use timing = 0 or it will error.

You may also like to do it for the vehicle, so if a player hops out it will continue flashing:

function toggleFlashingLights ( thePlayer ) 
        if getPlayerOccupiedVehicleSeat ( thePlayer ) == 0 then 
                if vehicles[getPlayerOccupiedVehicle ( thePlayer )] then 
                        killTimer ( vehicles[getPlayerOccupiedVehicle ( thePlayer )] ) 
                        vehicles[getPlayerOccupiedVehicle ( thePlayer )] = nil 
                else 
                        vehicles[getPlayerOccupiedVehicle ( thePlayer )] = setTimer ( toggleVehicleLights, 250, 0, getPlayerOccupiedVehicle ( thePlayer ) ) 
                end 
        end 
end 
addCommandHandler ( "flash", toggleFlashingLights ) 
  
function toggleVehicleLights ( car ) 
    if ( getVehicleOverrideLights ( car ) ~= 2 ) then 
        setVehicleOverrideLights ( car, 2 ) 
    else 
        setVehicleOverrideLights ( car, 1 ) 
    end 
end 

vehicles = { } 

Link to comment
wow, it's really great, thanks guys, but if i want to use effect like this:

https://www.youtube.com/watch?v=QyhZtajA ... re=related

i think that it will be setVehicleLightState ?

Lol!

http://development.mtasa.com/index.php? ... LightState

"A whole number determining the new state of the light. 0 represents normal lights, and 1 represents broken lights."

I presume you want it to flash, not to be broken? :P

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...