Entity Driving
Entity driving allows you to control other entities. This isn't particularly only cars - but any move type you want. It could be a simple ball that rolls around, noclip, a helicopter, a dog. It doesn't have to even move. Anything you can control.
The Drive Class
A drive class is simply a table of functions. Drive classes exist on both the client and the server - and are fully predicted - which means that driving feels the same in multiplayer as in singleplayer. Nice and responsive.
A simple Drive Class
Here's a simple drive class
This might look complicated but it's actually pretty simple. It gives you a simple velocity based noclip mode.
Some things to note. When you register your drive mode you should register them in order. What this means is that if you're deriving your mode from a class you should register that class first.
Making the player drive
To make the player start driving, on the server call
for example
To stop driving you can call
Or if you're inside the actual drive class you can call
Example
For a more in depth example you can look at lua/drive/drive_sandbox.lua. This is the drive mode used to drive around entities in Sandbox mode.
It's worth checking out lua/drive/drive_base.lua too - as this is the common base shared by all of the default drive modes and displays all the hookable functions.
Internals
The class is created once and cached. On server it's pretty much guaranteed that this object will stick around for the duration of the drive - so it's safe to store variables on it. But on the client if there's lag - or prediction errors - it's possible that the object will be re-created.
The controlling player and entity aren't passed to each function because they're saved on the object itself. They're accessible via: