Jump to content

[SOLVED] Get initial vehicle engine state?


Tails

Recommended Posts

Hello everybody,

I'm working on a script that lets you control the state of the engine. Is there any way to get a vehicle's initial engine state?

For example, I need to know whether the engine of the vehicle you're entering is on or off. So when the engine is off and you're entering it, the engine should stay off and vice versa. I can easily manage this but it won't work as soon as I enter another vehicle. So if we had some sort of function to detect a vehicle's initial engine state then that would solve the problem.

Any help is much appreciated,

Tails

Edited by Guest
Link to comment

That's not getting the initial state. When a player gets in a vehicle it turns on the engine by itself so it's always going to be true.

I could set the engine state to false through onPlayerVehicleEnter but that's not what I'm looking for. Because with my code, I can choose to leave the engine on when I exit a vehicle. But when I choose to leave the engine off and exit en re-enter the vehicle it'll turn the engine back on.

So to be more clear: When I leave the engine off and exit, when I enter again and use getVehicleEngineState then it will return true because the engine turns back on by it self. I hope you understand what I mean.

Link to comment
  • 2 weeks later...
That's not getting the initial state. When a player gets in a vehicle it turns on the engine by itself so it's always going to be true.

I could set the engine state to false through onPlayerVehicleEnter but that's not what I'm looking for. Because with my code, I can choose to leave the engine on when I exit a vehicle. But when I choose to leave the engine off and exit en re-enter the vehicle it'll turn the engine back on.

So to be more clear: When I leave the engine off and exit, when I enter again and use getVehicleEngineState then it will return true because the engine turns back on by it self. I hope you understand what I mean.

I remember there is an event that is triggered when the player starts entering a vehicle, you can use this one and if the engine is off then after the player enter the vehicle use setEngineState to turn it off. When the player exit you can just easily use the onPlayerVehicleExit trigger and turn the engine off with setEngineState

Link to comment
  
function setEngineState(source) 
 if (source) then 
  local car = getPedOccupiedVehicle(source) 
  if (car) then 
   local state = getVehicleEngineState ( car) 
   setVehicleEngineState ( car, not state ) 
   setElementData(car,"enginestate", not state) 
  end 
 end 
end 
  
function enterVehicle ( player, seat, jacked ) --when a player enters a vehicle 
 local state = getElementData(source, "enginestate") 
 if (state) then 
  setVehicleEngineState(source, state) 
 end 
end 
addEventHandler ( "onVehicleStartEnter", getRootElement(), enterVehicle ) 
  

Link to comment

Thank you both for your help!

I managed to do it the way you described it jingzhi. Never knew about that function.

addEventHandler("onVehicleStartEnter",root, 
    function(player,seat) 
        local e_state = getVehicleEngineState(source) 
        if seat == 0 and e_state == false then 
            function onEnter(player) 
                setVehicleEngineState(source,false) 
                removeEventHandler("onVehicleEnter",root,onEnter) 
            end 
            addEventHandler("onVehicleEnter",root,onEnter) 
        end 
    end 
) 

This way you don't have to use tables or setElementData, JanriKallas.

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