Jump to content

Deafult engine state


Nich

Recommended Posts

If I spawn a vehicle, then by deafult the engine is always on.

I want to develop a system, that when I enter a car, the engine would be off and when I exit a vehicle with a running engine it would still work.

At the moment all the engines are running and when I shut down the engine with a command, it would be running again when I enter it next time.

Link to comment
function turnOffEngine ( theVehicle )
setVehicleEngineState ( getRootElement(), false )
end
addEventHandler ( "onResourceStart", getResourceFromName ( "RESOURCE NAME HERE" ), turnOffEngine )
 
function toggleEngine ( playerSource )
local theVehicle = getPlayerOcuppiedVehicle ( playerSource )
if theVehicle and getVehicleController ( theVehicle ) == playerSource then
local state = getVehicleEngineState ( theVehicle )
setVehicleEngineState ( theVehicle, not state )
end
end
addCommandHandler ( "engine", toggleEngine )

Edited by Guest
Link to comment
function turnOffEngine ( theVehicle )
setVehicleEngineState ( theVehicle, false )
end
addEventHandler ( "onVehicleEnter", getRootElement(), turnOffEngine )

That means that I can't let the engine running, am I right?

I need it for my roleplay GM, and it wouldn't be a good thing, if car's engine would start/stop by it-self, all has to be done by an in game character. :)

Link to comment
function turnOffEngine ( theVehicle )
setVehicleEngineState ( getRootElement(), false )
end
addEventHandler ( "onResourceStart", getResourceFromName ( "RESOURCE NAME HERE" ), turnOffEngine )
 
function toggleEngine ( playerSource )
local theVehicle = getPlayerOcuppiedVehicle ( playerSource )
if theVehicle and getVehicleController ( theVehicle ) == playerSource then
local state = getVehicleEngineState ( theVehicle )
setVehicleEngineState ( theVehicle, not state )
end
end
addCommandHandler ( "engine", toggleEngine )

Don't use getResorceFromName in addEventHandler unless you do it in function. It's useless. Why would you want to add the event handler to a different resource not "this"?

Link to comment
function turnOffEngine()
for i, vehicle in ipairs( getElementsByType( 'vehicle' ) ) do
setVehicleEngineState ( vehicle, false )
end
end
addEventHandler ( "onResourceStart", getResourceRootElement( getThisResource() ), turnOffEngine )
 
function toggleEngine ( player )
local theVehicle = getPlayerOcuppiedVehicle ( player )
if theVehicle and getVehicleController ( theVehicle ) == player then
local state = getVehicleEngineState ( theVehicle )
setVehicleEngineState ( theVehicle, not state )
end
end
addCommandHandler ( "engine", toggleEngine )

^ should work

Link to comment
function turnOffEngine()
for i, vehicle in ipairs( getElementsByType( 'vehicle' ) ) do
setVehicleEngineState ( vehicle, false )
end
end
addEventHandler ( "onResourceStart", getResourceRootElement( getThisResource() ), turnOffEngine )
 
function toggleEngine ( player )
local theVehicle = getPlayerOcuppiedVehicle ( player )
if theVehicle and getVehicleController ( theVehicle ) == player then
local state = getVehicleEngineState ( theVehicle )
setVehicleEngineState ( theVehicle, not state )
end
end
addCommandHandler ( "engine", toggleEngine )

^ should work

Didn't work for me :(

Link to comment

Just a small example of how it might work:

function setStateToData()
for index,vehicle in ipairs(getElementsByType("vehicle")) do -- Do this for every vehicle existing:
local state=getVehicleEngineState(vehicle)       -- See if the engine is on
setElementData(vehicle,"engine",state,false)   -- Store the result
end
end
addEventHandler("onResourceStart",getResourceRootElement(),setStateToData)
 
function vehicleEnterStuff()
local state=getElementData(source,"engine") -- Get the stored data
setVehicleEngineState(source,state)  -- Set it to that
end
addEventHandler("onVehicleEnter",getRootElement(),vehicleEnterStuff) -- Make sure this happens if a player enters a vehicle
 
function vehicleExitStuff()  -- Exact opposite
local state=getVehicleEngineState(source)
setElementData(source,"engine",state,false)
end
addEventHandler("onVehicleExit",getRootElement(),vehicleExitStuff)

There might be several issues with this, but well, I guess it should help out a bit.

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...