.:`=-~rANdOm~`-=:. Game Servers

.:`=-~rANdOm~`-=:. Game Servers (Read Only) => Guides => Topic started by: Кrаsher on September 08, 2010, 07:07:16 PM

Title: [>Lua] :: Gamemode Creation
Post by: Кrаsher on September 08, 2010, 07:07:16 PM
(http://img824.imageshack.us/img824/1396/intro1a.png)
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 (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 (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.
(http://img541.imageshack.us/img541/334/chap1a.png)
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:

(http://img825.imageshack.us/img825/6002/folder1a.png)

Open up info.txt in Notepad++

Type out this (Or just copy it from the code below):

Code: [Select]
"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:

(http://img828.imageshack.us/img828/9769/fileinfo1a.png)

Now with that completed, Save Info.txt and close it.

Go inside the folder 'gamemode' and create these .lua files to start with:

(http://img695.imageshack.us/img695/5783/folder2a.png)

'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).
(http://img225.imageshack.us/img225/3613/chap2a.png)
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: [Select]
GM.Name = ""
GM.Author = ""
GM.Email = ""
GM.Website = ""

This is what mine looks like:

(http://img94.imageshack.us/img94/8105/fileshared1a.png)

In 'shared.lua', we will have to derive our gamemode from something. To Derive a gamemode, simply down from the information code type:

Code: [Select]
DeriveGamemode( "sandbox" )
Mine looks like this incase you need to see it:

(http://img830.imageshack.us/img830/8399/fileshared2a.png)

Now we need to set-up teams, so we can organize our players more efficiently.

Below that add this:

Code: [Select]
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:

(http://img27.imageshack.us/img27/4026/fileshared3a.png)

Thats it for 'shared.lua' for now. We might be back here later to add some things :b.
Save it, and exit that.
(http://img46.imageshack.us/img46/9755/chap3a.png)
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: [Select]
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:

(http://img706.imageshack.us/img706/6267/fileinit1a.png)

Now below this type this out:

Code: [Select]
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.

Code: [Select]
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.

(http://img534.imageshack.us/img534/3228/fileinit2a.png)

Of Course PlayerSpawn is every time the player spawns. So lets do the one for the first time the player spawns.

Code: [Select]
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:

Code: [Select]
if (This) then
DoThis
end

But in better example:

Code: [Select]
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.

Code: [Select]
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'

(http://img836.imageshack.us/img836/9136/fileinit3a.png)
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Кrаsher on September 08, 2010, 07:07:30 PM
Reserved Space For Tutorial
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Rocket50 on September 08, 2010, 07:18:35 PM
This deserves to be pinned to the top part of the section :3
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Кrаsher on September 08, 2010, 07:32:24 PM
This deserves to be pinned to the top part of the section :3
Not anywhere near finished
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Mr. Franklin on September 08, 2010, 08:23:02 PM
Looks awesome, i cant wait until it is finished xD
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: The Ghost Of Anony Mouse on September 08, 2010, 08:34:42 PM
 :o cant wait to learn LUA :P
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Кrаsher on September 08, 2010, 09:30:48 PM
fff-

Need to sleep, more work will be done tomorrow.
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Jman on September 08, 2010, 11:17:31 PM
lol figures you make a guide since "the only thing you can't do in lua is pull a unicorn out of ur ass and have it give  :cake: "




:thumbsup: so far. can't wait for more  :thumbsup:
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: fenkeN on September 08, 2010, 11:35:26 PM
sweet
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Killah on September 09, 2010, 12:49:17 AM
Looks good for the LUA Newbies.

ive Done C# and C++ so im quite Familiar with LUA.

the Anti RDM script was probably my first Private Release. Other that or AE3

Anyways Good Job

~~Killah
Title: Re: [>Lua] :: A tutorial By Krasher
Post by: Кrаsher on September 09, 2010, 01:32:31 PM
Adding more now.
Title: Re: [>Lua] :: Gamemode Creation
Post by: Cake Faice on September 10, 2010, 07:46:19 PM
I coulda sworn I seen this somewhere else... :-[
Title: Re: [>Lua] :: Gamemode Creation
Post by: Кrаsher on September 10, 2010, 08:43:06 PM
I coulda sworn I seen this somewhere else... :-[
Everywhere. This is a simple thing people used to make called CakeScripts.
Title: Re: [>Lua] :: Gamemode Creation
Post by: Кrаsher on September 23, 2010, 05:30:11 PM
Guide on Hold until Wednesday next week (I am moving)
Title: Re: [>Lua] :: Gamemode Creation
Post by: Кrаsher on October 21, 2010, 06:31:11 PM
Sorry for the HUGE delay. More work put into guide this weekend and tomorrow.
Title: Re: [>Lua] :: Gamemode Creation
Post by: Sexist on October 21, 2010, 09:23:25 PM
Hold on. I'm looking at this code you've demonstrated, and the syntax of Lua seems strikingly similar to Expression 2 coding. This is occurring in such a manner to deny the postulate that such would be coincidence.
Title: Re: [>Lua] :: Gamemode Creation
Post by: Awesomeslayer2010G on November 07, 2010, 01:35:06 PM
Nice tut.. Im new here but i was invited from a friend.
Title: Re: [>Lua] :: Gamemode Creation
Post by: Peetah on November 07, 2010, 02:33:22 PM
The thread is done. RIP Krasher.
Title: Re: [>Lua] :: Gamemode Creation
Post by: » Magic « on November 08, 2010, 07:39:52 AM
The thread is done. RIP Krasher.

:'(
Title: Re: [>Lua] :: Gamemode Creation
Post by: ursus on November 22, 2010, 09:50:34 PM
The thread is done. RIP Krasher.

He ran fast and was banned a virgin.
Title: Re: [>Lua] :: Gamemode Creation
Post by: StartedBullet on December 22, 2010, 09:46:06 PM
Krasher was a pal of mine, but someone needs to finish this thread.
Title: Re: [>Lua] :: Gamemode Creation
Post by: » Magic « on December 25, 2010, 09:14:27 AM
If I knew gamemode lua I would

but I stick to calling ops n shit lua
Title: Re: [>Lua] :: Gamemode Creation
Post by: Tomcat on December 25, 2010, 10:19:53 AM
If I knew gamemode lua I would

but I stick to calling ops n shit lua
:trollface:
Title: Re: [>Lua] :: Gamemode Creation
Post by: » Magic « on December 25, 2010, 10:30:28 AM
:trollface:


oicwutudidthar

DO IT FAGGOT :trollface:
Title: Re: [>Lua] :: Gamemode Creation
Post by: Monttumopo on January 09, 2011, 04:43:00 AM
Thx u really fucked my brains up! :thumbsup: :thumbsup: :thumbsup: