This tutorial will be focusing on the basics of GMod Lua Script.
In the progress we will be creating a basic SandBox GameMod.The tutorial will cover:- Functions
- If Statements
- VGUI
- GMod Instances
I am going to assume you have Notepad++
If not you can get it here:
http://notepad-plus-plus.org/downloadFor this we will need the plugin name GmodLua Lexer.
You can download it by clicking on this link:
http://npp-gmod-lua.googlecode.com/files/NppGmodLuaPlugin-v1.3.zipIf you don't know how to install Google it, this is not a NP++ Plugin Tut.
When in NP++ Set 'Language' at the top to 'Gmod Lua', For this Tutorial.
Let me start at this point, I must say this won't be the nicest script you will make, as many of yo are Beginners at lua. Don't expect us to be making a PERP, DarkRP, ZS, or TTT type of gamemode.
We will be starting on creating the proper folders.
Navigate to C:\<STEAM_DIRECTORY>\steamapps\brad_pwns\garrysmod\garrysmod\gamemodes\Create a new folder there, name it what you want the name of your gamemode to be.
Now, once you are in there, make this folder and this .txt file:Open up info.txt in Notepad++Type out this (Or just copy it from the code below):
"Gamemode"
{
"name" ""
"version" ""
"author_name" ""
"author_email" ""
"author_url" ""
"icon" ""
"info" ""
"hide" ""
}
And edit it to your liking inside the quotes.
Here is what mine looks like:Now with that completed, Save Info.txt and close it.Go inside the folder 'gamemode' and create these .lua files to start with:'cl_init.lua' is our Client Side Script, All code in this one will be ran Client Sided.
'init.lua' is our Server Side Script, All code in this one will be ran Server Sided.
'shared.lua' is our Shared Script, Information in this script will be shared between the two (ServerSide and ClientSide).Open up 'shared.lua' now.For now we are going to tell GMod what this gamemode's info is. Such as its Name, Author, Author Email, and Author Website.
Add this to the top of the file.
GM.Name = ""
GM.Author = ""
GM.Email = ""
GM.Website = ""
This is what mine looks like:
In 'shared.lua', we will have to derive our gamemode from something. To Derive a gamemode, simply down from the information code type:
DeriveGamemode( "sandbox" )
Mine looks like this incase you need to see it:
Now we need to set-up teams, so we can organize our players more efficiently.
Below that add this:
team.SetUp( 1, "Guest" , Color( 255 , 255 , 100 ) )
All team.SetUp states is that we are making a team. '1' is our teams index, so we can get to them faster later in the code. 'Guest' is the name I chose for our team. 'Color' is just an RGB format of what our teams color will be mine is the basic GMod player color, but you can have anything. (Remember the format is R,G,B from 0 - 255)
Now just make more teams, make at least 5 (for these classes).
My code looks like this, but you can name them whatever you want:
Thats it for 'shared.lua' for now. We might be back here later to add some things :b.
Save it, and exit that.
Now open up 'init.lua'NOTICE: init.lua is most likely the longest script you will make in a gamemode.We need to tell the gamemode to make the client download some of our scripts. So we need to add this at the top to state we are making the client download the files.
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
Translated this means Add Client Side Lua File. Thus we are just making the client download these. init.lua will not be downloaded by the client, so don't add it. Only add files that are shared between Client and Server (shared) or any Client Side scripts (cl_init)
This is what we have so far:
Now below this type this out:
include( "shared.lua" )
Easy amirite? What this does is allow information from 'shared.lua' to be used and sent in-between the server-side and the shared file. Just like we did in 'cl_init.lua'.
Now below this we will need to set up our functions for the gamemode, Functions are the most essential part of any gamemode hands down. What a function does is organize a code to be ran at a certain time, such as an achievement at 5 kills, The Player Settings, What weapons the player spawns with, Makes the player admin or not, Bans the player, Decides what you can and can't do, along with what you have and don't.
I always add GM:PlayerSpawn( ply ) first in my lua codes.
Simply add this a couple lines below our include.
function GM:PlayerSpawn( ply )
self.BaseClass:PlayerSpawn( ply )
ply:SetGravity( 0.80 )
ply:SetMaxHealth( 100 , true )
ply:SetWalkSpeed( 325 )
ply:SetRunSpeed( 400 )
end // End our function
Of course you can set the settings to w/e you want them to be. Just remember Gravity is on a normal scale from 0 - 1, above 1 adds more gravity than normal, less than 1 decreases it.
Also that for speed, 325 is the normal walk speed. You will just want to raise up the speed for running or you can just put it as the same amount so you can't sprint.
As for what GM: prefix means, GM is a gamemode ran function. These do not have to be called (ran) as they will be auto called by events in-game, such as the player spawning.
This is my 'init.lua' so far.Of Course PlayerSpawn is every time the player spawns. So lets do the one for the first time the player spawns.
function GM:PlayerInitialSpawn( ply )
if (ply:IsAdmin()) then
ply:SetTeam( 2 )
elseif (ply:IsSuperAdmin()) then
ply:SetTeam( 3 )
end
end
PlayerInitialSpawn, is like the name suggests, the Players Initial (First) Spawn. Here we will be setting the players team most of the time, never really used it for much else, maybe some VGUI.
In this one I show a basic if statement. All if statements must have three things. If, Then, and End.
Basically saying:
if (This) then
DoThis
end
But in better example:
if (ply:IsAdmin()) then
ply:SetTeam( 2 )
end
Translated from its lua form, this means:
If The Player IS an admin, Set the Players Team to 2.
Just a note here, you can use 3 things for player in lua scripts. Pl, Ply, and Player (Which would look like pl:, ply:, and player:)
I use ply: because being shorter than player: it is just an abbreviation. I don't use pl: because I like to organize more efficient. It is all really personal preference.
Now to tell the gamemode what to give our players when they spawn.
function GM:PlayerLoadout( ply )
ply:StripWeapons() // Call this for safety
ply:Give( "weapon_physcannon" ) // The Grav-Gun
ply:Give( "weapon_physgun" ) // The Phys-Gun to move Props
ply:Give( "gmod_tool" ) // This is the GMod Tool, this is just a SWEP for each tool in the Spawn Menu. They are just ran through this.
end
As always you can add whatever. I just added the basic GMod Building Tools.
Btw, StripWeapons() will take all the players weapons, StripWeapon( "weapon_name" ), will strip a specific weapon.
I am going to take a moment to explain why I put ( ply ) after these main functions.
As I call it, ply, is our target in the function. The Function is targeted to the player, so we just add in the Functions arguments (or target list) ply.
This allows us to use ply: in our code, because it will call back and find that ply: is a variable for our player.
By now I have this in 'init.lua'