Author Topic: [>Lua] :: Gamemode Creation  (Read 465 times)

0 Members and 1 Guest are viewing this topic.

Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
[>Lua] :: Gamemode Creation
« on: September 08, 2010, 07:07:16 PM »
+2

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



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

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

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:



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



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.



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'


« Last Edit: September 10, 2010, 12:55:21 PM by Кrаsher »





Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: A tutorial By Krasher
« Reply #1 on: September 08, 2010, 07:07:30 PM »
0
Reserved Space For Tutorial





Offline Rocket50

  • ******
  • Posts: 2732
  • Gender: Male
  • My god it's dusty
  • Respect: +946
Re: [>Lua] :: A tutorial By Krasher
« Reply #2 on: September 08, 2010, 07:18:35 PM »
+1
This deserves to be pinned to the top part of the section :3

Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: A tutorial By Krasher
« Reply #3 on: September 08, 2010, 07:32:24 PM »
0
This deserves to be pinned to the top part of the section :3
Not anywhere near finished





Offline Mr. Franklin

  • ******
  • Old Forum MemberWindows UserLinux UserLeague PlayerDonatorCat Lover
    View More Badges!

  • Posts: 2988
  • Gender: Male
  • I'm addicted to sweet tea
  • Respect: +388
Re: [>Lua] :: A tutorial By Krasher
« Reply #4 on: September 08, 2010, 08:23:02 PM »
0
Looks awesome, i cant wait until it is finished xD
Quotes from friends:
 
.:~RND`=- coolzeldad -=: mah mouse is sqeakeh
.:~RND`=- Mr.Franklin -=: tweeeeek it

Twitch: http://www.twitch.tv/mrfranklin1972
Prox: Thread status: memed.

Offline The Ghost Of Anony Mouse

  • hye
  • *****
  • Posts: 711
  • Gender: Male
  • bi
  • Respect: +108
Re: [>Lua] :: A tutorial By Krasher
« Reply #5 on: September 08, 2010, 08:34:42 PM »
0
 :o cant wait to learn LUA :P

Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: A tutorial By Krasher
« Reply #6 on: September 08, 2010, 09:30:48 PM »
0
fff-

Need to sleep, more work will be done tomorrow.





Offline Jman

  • NO FUN ALLOWED
  • ******
  • Old Forum Member
    View More Badges!

  • Posts: 2752
  • Gender: Male
  • Respect: +9999
  • Respect: +805
    • Random Gaming
Re: [>Lua] :: A tutorial By Krasher
« Reply #7 on: September 08, 2010, 11:17:31 PM »
0
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:

Offline fenkeN

  • ΔMi−1 = −∂Σn=1NDi[n][Σj∈C{i}Fji[n − 1] + Fexti[[n−1]]
  • *****
  • Posts: 859
  • Gender: Male
  • Respect: +98
    • Light Amplification by Stimulated Emission of Radiation
Re: [>Lua] :: A tutorial By Krasher
« Reply #8 on: September 08, 2010, 11:35:26 PM »
0
sweet

Offline Killah

  • *
  • Posts: 46
  • Gender: Male
  • --Killahs Back--
  • Respect: +10
    • Annihilation Forums
Re: [>Lua] :: A tutorial By Krasher
« Reply #9 on: September 09, 2010, 12:49:17 AM »
0
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
--Killahs Back People--

==i will fight till the End==

Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: A tutorial By Krasher
« Reply #10 on: September 09, 2010, 01:32:31 PM »
0
Adding more now.





Offline Cake Faice

  • How can society be real
  • ***
  • Windows UserOld Forum Member
    View More Badges!

  • Posts: 4446
  • Gender: Male
  • if our oppresions aren't real?
  • Respect: +1541
Re: [>Lua] :: Gamemode Creation
« Reply #11 on: September 10, 2010, 07:46:19 PM »
0
I coulda sworn I seen this somewhere else... :-[

Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: Gamemode Creation
« Reply #12 on: September 10, 2010, 08:43:06 PM »
0
I coulda sworn I seen this somewhere else... :-[
Everywhere. This is a simple thing people used to make called CakeScripts.





Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: Gamemode Creation
« Reply #13 on: September 23, 2010, 05:30:11 PM »
0
Guide on Hold until Wednesday next week (I am moving)





Offline Кrаsher

  • ****
  • Posts: 436
  • Gender: Male
  • Posts: 9001
  • Respect: +50
Re: [>Lua] :: Gamemode Creation
« Reply #14 on: October 21, 2010, 06:31:11 PM »
0
Sorry for the HUGE delay. More work put into guide this weekend and tomorrow.