Game Movement

From Garry's Mod
Jump to: navigation, search

Getting your head around how the game movement system works isn't easy. Here I will try to explain it.

Contents

User Command

The client creates a user command. A user command is created by looking at the player's keys and mouse activity. This is done in the GM:CreateMove hook.

This user command is then sent to the server. In a multiplayer game this is also used locally to predict the player's movement.


SetupMove

The SetupMove hook is used to transfer data from the User Command to the MoveData 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 movedata should be identical too.

This is a good place to change controls. For example - if you don't want a player to jump.. you can remove the jump button from the MoveData here. This has advantages over removing it from the usercommand in CreateMove - because it's controlled by the server - not the client.

Move

The move hook does the brunt of the work.. but it should ONLY change the movedata 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 GAMEMODE: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 FinishMove hook is where you should actually apply the changes from the movedata into 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


Or just use 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.

Personal tools
Navigation