SetVehicleEngineState: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (OOP)
(added example)
Line 16: Line 16:


==Example==
==Example==
<section name="Clientside example" class="client" show="true">
This example lets the driver toggle vehicle engine on/off.
<syntaxhighlight lang="lua">
function toggleEngine()
local vehicle = getPedOccupiedVehicle(localPlayer)
if vehicle and getVehicleController(vehicle) == localPlayer then
setVehicleEngineState(vehicle, not getVehicleEngineState(vehicle))
end
end
addCommandHandler("engine", toggleEngine)
</syntaxhighlight>
</section>
<section name="Serverside example" class="server" show="true">
<section name="Serverside example" class="server" show="true">
This example will turn off a vehicle's engine when the driver gets out of the car.
This example will turn off a vehicle's engine when the driver gets out of the car.

Revision as of 00:33, 15 April 2019

This function turns a vehicle's engine on or off. Note that the engine will always be turned on when someone enters the driver seat, unless you override that behaviour with scripts.

Syntax

bool setVehicleEngineState ( vehicle theVehicle, bool engineState )

OOP Syntax Help! I don't understand this!

Method: vehicle:setEngineState(...)
Variable: .engineState
Counterpart: getVehicleEngineState


Required Arguments

  • theVehicle: The vehicle you wish to change the engine state of.
  • engineState: A boolean value representing whether the engine will be turned on (true) or off (false).

Returns

Returns true if the vehicle's engine state was successfully changed, false otherwise.

Example

Click to collapse [-]
Clientside example

This example lets the driver toggle vehicle engine on/off.

function toggleEngine()
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if vehicle and getVehicleController(vehicle) == localPlayer then
		setVehicleEngineState(vehicle, not getVehicleEngineState(vehicle))
	end
end
addCommandHandler("engine", toggleEngine)
Click to collapse [-]
Serverside example

This example will turn off a vehicle's engine when the driver gets out of the car.

function turnEngineOff ( theVehicle, leftSeat, jackerPlayer )
    -- if it's the driver who got out, and he was not jacked,
    if leftSeat == 0 and not jackerPlayer then
        -- turn off the engine
        setVehicleEngineState ( theVehicle, false )
    end
end
-- add 'turnEngineOff' as a handler for "onPlayerExitVehicle"
addEventHandler ( "onPlayerVehicleExit", getRootElement ( ), turnEngineOff )

See Also