I'll assume you've already read the Prediction page and you've got a vague idea of what it is, most of these prediction examples assume you already know the base of coding a weapon/entity and know what the drive system is.
This page is here to make use of that giant wall of text and to explain what the heck is going on in practice.
The main principle of prediction as explained earlier is to have a simulation of what's going to happen before the server does, aka you just call everything shared( with a few exceptions ).
If it wasn't clear enough, prediction breaks easily, and in case of entities ( and weapons ) only one can be predicted by a player at any time.
Now, using normal variables is fine, but in case of prediction they're not sufficient because of prediction errors.
In which case you have to use Network Vars ( called dt vars internally ) , all the engine entities use these for networking and prediction.
In fact, if you've made a scripted weapon before, chances are you're already using them, functions such as Weapon:SetNextPrimaryFire , Player:RemoveAmmo access predicted DT vars, defined in the engine.
Can't stress this enough, due to prediction all the functions shown here are defined on both states, sans a few exceptions.
I'm not going to add all the SWEP/ENT variables here, this is merely an example.
This is pretty straight forward, to start with, you'll define your needed Network vars as explained in the page above.
--TODO function SWEP:SetupDataTables() end function SWEP:Think() end function SWEP:PrimaryAttack() end
As good as the drive system might be, with the way it's coded it only allows for one movement override at a time.
Directly hooking the movement hooks allows us to modify the behaviour and make it somewhat compatible with others.
Now, it's best to actually couple this with a predicted entity ( such as a weapon, or look below for another example ), seeing as for instance, on a jetpack we may want to handle fuel.
Saving variables onto the player ( entity specific stuff such as fuel, entity references are fine ) should only used as a last resort, especially if it can be avoided.
hook.Add( "Move" , "Movehook test", function( ply , mv ) end)
Note, this stuff is kind of advanced
This kind of prediction is already used internally for weapons, but here we can have more freedom, such as being able to create a weapon-like entity that can be used at anytime.
The way we're gonna do the prediction is to run the logic in the Player's Tick hook, and not trough the entity's Think.
There's currently no getter to know if an entity is already predictable, and we need it to avoid the DT variables from being reset everytime the function is called, so excuse the crude hack below.
This is an example for a predicted entity base, which you can derive from or use directly.
The main checks are done on the controlling player DT var because Set/Get Owner/Parent are used mainly for collisions, and external tools or addons might override them ( for instance the gravity gun sets the owner on anything it holds )
--TODO: link to my github with the predicted entity base
This can also be coupled with the movement hooks, allowing us to have predicted jetpacks or other movement modifying entities.
Drive System Prediction
This might get considered as a duplicate of Entity Prediction, with the difference being that the drive system is only good to control an entity as if it was a vehicle, nothing can be ran along with the player as of YET.
With drive, no player movement logic is ran from the engine, and thus we will need to handle the collisions on our own.
Also stuff like enabling prediction on the entity is already handled internally.
This is much easier than Entity Prediction as you don't have to do as much stuff as that.
TODO: Visit Entity_Driving in the mean time.
Emitting Sounds during prediction
If you've used the predicted entity example you might've noticed that sounds still emit twice, as if prediction didn't cull them.