Client-Server relationship in Lua

From GMod Wiki

Jump to: navigation, search
Lua: Clientside-Serverside Relationship
Page white text.png Description:Aims to ease Lua coders into client/server interaction.
link=User:Garry Original Author:Garry
Calendar.png Created:4th July 2006

Contents

Server-side, Client-side and Prediction

Server-side is the server. The server makes the decisions. Spawning players, giving weapons, deciding whether bullets hit someone etc. Basically anything that all the players need to see is server-side.

Client-side is the client's computer. It draws the world and takes input. The client shouldn't ever be allowed to make important decisions (such as "I killed that guy"). The client should always be sending stuff to the server (such as pressing forward then the server will say "yeah you can walk").

Prediction is the fine line in between. When you shoot your weapon in multiplayer it doesn't wait for a ping time before firing. The client-side predicts whether you're allowed to fire your weapon (based on the amount of ammo left) and if you can it fires it straight away on your client. Then the server gets the signal that you fired and hopefully is in agreement with the prediction, and fires the gun.

In Garry's Mod 9...

In Garry's Mod 9 Lua was totally server side. This had the benefit of simplicity but also had a lot of downfalls. You couldn't have any kind of reliable and quick HUD drawing, you couldn't really use VGUI, you couldn't predict the weapons, you couldn't draw in the world.

In Garry's Mod 10

In Garry's Mod 10 Lua exists on both the server and the client. We can now do prediction, draw on the screen and draw in the world all in real-time. Clientside files get sent down to the clients on connection.

Clientside Abilities

For the most part clientside interaction with entities is read only. You can read the location and velocity of an entity but you can't set either of them - because that's for the server to decide. The client can draw to the screen though - which is more than redeeming.

Gamemodes

Gamemodes are both clientside and serverside. The clientside side is used to draw to the screen (scoreboards, HUD, GUI) and take input (orientating the camera, reversing controls). The serverside is used to make gameplay decisions.

An example of this is the Crazy-Golf gamemode. To hit the ball a player presses jump. What happens when a player presses jump is this

  1. C: ??PlayerBindPress?? is called on the clientside gamemode by the engine
  2. C: +jump is detected by the gamemode and the "hitball" console command is called
  3. S: "hitball" is a Lua created serverside command - so it's called on the server
  4. S: The server detects which player said hit and decides whether they can hit the ball
  5. S: If the player can hit the ball (it's their turn) then velocity is placed on that player's golf ball
  6. S: Next player's turn
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox