Jump to content

[REL] getVehicleEngineState function


szlend

Recommended Posts

Since there is no getVehicleEngineState function, I decided to code it. Enjoy :)

The function:

function getVehicleEngineState(source) 
    if isElement(source) and getElementType(source) == "vehicle" then 
        return getElementData(source, "engineState") 
    end 
end 

We'll also need a new setVehicleEngineState function:

function setVehicleEngineState2(source, state) 
    if isElement(source) and getElementType(source) == "vehicle" then 
        setElementData(source, "engineState", state) 
        setVehicleEngineState(source, state) 
    end 
end 

And to set vehicle engine off on resource start:

function resourceStart() 
    local vehicles = getElementsByType("vehicle") 
    for index, vehicle in pairs(vehicles) do 
        setElementData(vehicle, "engineState", false) 
    end 
end 

OnVehicleEnter and OnVehicleRespawn fix:

function vehicleEnter() 
    setVehicleEngineState2(source, getVehicleEngineState(source)) 
end 
  
function vehicleRespawn() 
    setVehicleEngineState2(source, false) 
end 

-----------------------------------------

Example:

function getVehicleEngineState(source) 
    if isElement(source) and getElementType(source) == "vehicle" then 
        return getElementData(source, "engineState") 
    end 
end 
  
function setVehicleEngineState2(source, state) 
    if isElement(source) and getElementType(source) == "vehicle" then 
        setElementData(source, "engineState", state) 
        setVehicleEngineState(source, state) 
    end 
end 
  
function toggleEngine(source) 
    local vehicle = getPlayerOccupiedVehicle(source) 
    if (vehicle) then 
        if (getVehicleEngineState(vehicle) == true) then 
            setVehicleEngineState2(vehicle, false) 
        else 
            setVehicleEngineState2(vehicle, true) 
        end 
    end  
end 
  
function resourceStart() 
    local vehicles = getElementsByType("vehicle") 
    for index, vehicle in pairs(vehicles) do 
        setElementData(vehicle, "engineState", false) 
    end 
end 
  
function vehicleEnter() 
    setVehicleEngineState2(source, getVehicleEngineState(source)) 
end 
  
function vehicleRespawn() 
    setVehicleEngineState2(source, false) 
end 
  
addEventHandler("onResourceStart", getRootElement(), resourceStart) 
addEventHandler("onVehicleEnter", getRootElement(), vehicleEnter) 
addEventHandler("OnVehicleRespawn", getRootElement(), vehicleRespawn) 
addCommandHandler("engine", toggleEngine) 

Link to comment
  • 1 month later...

function setVehicleEngineState2(source, state) 
    if isElement(source) and getElementType(source) == "vehicle" then 
        setElementData(source, "engineState", state) 
        setVehicleEngineState(source, state) 
    end 
end 

You could redefine the setVehicleEngineState itself, like

_setVehicleEngineState = setVehicleEngineState 
function setVehicleEngineState(source, state) 
    if isElement(source) and getElementType(source) == "vehicle" then 
        setElementData(source, "engineState", state) 
        _setVehicleEngineState(source, state) 
    end 
end 

Link to comment
  • 10 years later...
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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