Garry's Mod Wiki

Game Movement

Getting your head around how the game movement system works isn't easy. This page will try to clarify on what goes on.

User Command

The client creates a user command (CUserCmd) which is created by looking at the player's keys and mouse activity. This is done in the GM:CreateMove and GM:StartCommand hooks.

This user command is then sent to the server where StartCommand is again run. In a multiplayer game the user command is also used directly locally to predict the player's movement.

StartCommand is a good place to change controls (prevent movement, etc) since everything else down the line depends on what happens there and it is run on both the client and server which is necessary to prevent clients from bypassing your changes.

SetupMove

The GM:SetupMove hook is used to transfer data from the user command to a CMoveData structure, and set the origin/velocity of the entity you're moving (probably the player). The user command's job is then finished.

This is called on the server - and also in multiplayer - on the client. If prediction is happening properly then given an identical user command on client/server - the outputted move data should be identical too.

Move

The GM:Move hook does the brunt of the work. It should only change the move data structure. This is because SetupMove and Move can be called multiple times to work out prediction errors.

So in this hook you would look at the MoveData structure, move its origin based on its other factors (buttons down, velocity etc). You'd also be doing things like collision detection here.

function GM:Move( ply, mv ) -- Get some variables for easy access local ang = mv:GetMoveAngles() local pos = mv:GetOrigin() -- If Jump is pressed, move pos forwards if ( mv:KeyDown( IN_JUMP ) ) then pos:Add( ang:Forward() * FrameTime() * 0.001 ) end -- Save the calculations back into the origin mv:SetOrigin( pos ) -- Don't do the default return true end

FinishMove

The GM:FinishMove hook is where you should apply the changes from the move data onto your entity.

function GM:FinishMove( ply, mv ) -- -- Move the player to the worked out position -- ply:SetNetworkOrigin( mv:GetOrigin() ) -- Don't do the default return true end

The Drive System

The drive system was invented to make this all more modular, and make it so you can control not only the player - but other objects too.