.:`=-~rANdOm~`-=:. Game Servers (Read Only) > Guides

[>Lua] :: Gamemode Creation

(1/5) > >>

Кrаsher:

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/download

For 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.zip

If 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):


--- Code: ---"Gamemode"
{
"name" ""
"version" ""

"author_name" ""
"author_email" ""
"author_url" ""

"icon" ""
"info" ""
"hide" ""
}
--- End code ---

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.


--- Code: ---GM.Name = ""
GM.Author = ""
GM.Email = ""
GM.Website = ""
--- End code ---

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:


--- Code: ---DeriveGamemode( "sandbox" )
--- End code ---

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:


--- Code: ---team.SetUp( 1, "Guest" , Color( 255 , 255 , 100 ) )
--- End code ---

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.


--- Code: ---AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

--- End code ---

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:


--- Code: ---include( "shared.lua" )
--- End code ---

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.


--- Code: ---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

--- End code ---

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.


--- Code: ---function GM:PlayerInitialSpawn( ply )
if (ply:IsAdmin()) then
ply:SetTeam( 2 )
elseif (ply:IsSuperAdmin()) then
ply:SetTeam( 3 )
end
end

--- End code ---

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:


--- Code: ---if (This) then
DoThis
end
--- End code ---

But in better example:


--- Code: ---if (ply:IsAdmin()) then
ply:SetTeam( 2 )
end
--- End code ---

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.


--- Code: ---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
--- End code ---

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'


Кrаsher:
Reserved Space For Tutorial

Rocket50:
This deserves to be pinned to the top part of the section :3

Кrаsher:

--- Quote from: rocket50 on September 08, 2010, 07:18:35 PM ---This deserves to be pinned to the top part of the section :3

--- End quote ---
Not anywhere near finished

Mr. Franklin:
Looks awesome, i cant wait until it is finished xD

Navigation

[0] Message Index

[#] Next page

Go to full version