Its time to learn how to setup our gamemode. Im assuming you downloaded skeleton gamemode. Extract it somewhere, desktop, and you should have a folder called skeleton. This needs to go into garrysmod/gamemodes. Im assuming you alread understand the gmod file structure, for gamemodes, addons, etc. Inside is info.txt. This is our first stop. Its fairly self explanitory. You can ignore info, icon, hide for now.
- Code:
"Gamemode"
{
"name" "TUTORIAL GM"
"version" "0.2"
"author_name" "Companion Cube"
"author_email" "N/A"
"author_url" "www.bams-server-forum.com"
"icon" ""
"info" ""
"hide" ""
}
This is very important to modify, as it will look stupid if you submit it un modified.
Now we get to the code. Go into gamemode (Relative to your gamemode's directory.) Open up shared.lua. Modify the part with the author info:
- Code:
GM.Name = "Tutorial GM"
GM.Author = "Companion Cube"
GM.Email = "N/A"
GM.Website = "www.bams-server-forum.com"
Go ahead and delete the GM:Initialize. We wont be needing this here.
Teams
The first thing we will do with our freshly created GM (Gamemode) is add teams. Two teams, plus a spectator, seems good. This is your first piece of code! Go to here and read about teams. You dont have to understand it 100%, but Its nice to see a bit of example code.
Gmod lua wiki
As you can see, the setup code for teams is part of the team library.
Library
A library is just that. It is a code followed by a "." that contains multiple functions. team.SetUp() is just one of many.
Function, Arguments
Functions are bits of code in lua that do things. They make our lives easier. Functions usually take arguments. Not like fighting over a bottle of Jack Daniels. These send info the the function so that it can work. For example, when Gmod calls PlayerConnected(), it passes the player that connected to the function, so that it can do things to him.
team.SetUp() has 4 arguments, but we will only use 3 for this point. They are:
Team number, Team name, Team color
Lets go over one last thing before we jump in and create our teams. When ever we want to work with these teams, we will need to specify which team. Remembering that 1 is red, 2 is blue, etc can be confusing. Instead, we will create something similar to a constant (non changing) in Lua. To let our selves know that this is NOT to be modified, we will put it in CAPS. Then, we will assign these CAPITAL variables the number of their team. Then, whenever we want to access team 1, which is the red team, we can just use TEAM_RED. Add this to your code:
- Code:
//Some "Constants" for our teams
TEAM_RED = 1
TEAM_BLUE = 2
TEAM_SPEC = 3
Now, we can setup our teams using our new "constants".
Add this:
- Code:
team.SetUp (TEAM_RED, "Red", Color (255, 0, 0, 255))
team.SetUp (TEAM_BLUE, "Blu", Color (0, 0, 255, 255))
team.SetUp (TEAM_SPEC, "Spectator", Color (255, 255, 255, 255))
Notice how I used Color() to define my color?
Teh wiki of win
When gmod asks for a color, it usually wants the "color" data type. The function Color(r,g,b,a) creates a color data and returns it to us. So, we could use things like:
- Code:
mycolor = Color(1,1,1,255)
- Code:
ply:SetColor(Color(123,123,123,123))
Data types
There are many Data Types in garrysmod. Some common ones are:
String,
Integer,
Float,
Color
Now, I think the correct term for these is object, but for the purposes of this tutorial, we will call them data types.
So, whenever you want to use a Color, you use Color(red,green,blue,alpha). Alpha is the transperency.
To recap:
Our code should look like this so far:
- Code:
GM.Name = "Tutorial GM"
GM.Author = "Companion Cube"
GM.Email = "N/A"
GM.Website = "www.bams-server-forum.com"
TEAM_RED = 1
TEAM_BLUE = 2
TEAM_SPEC = 3
team.SetUp (TEAM_RED, "Red", Color (255, 0, 0, 255))
team.SetUp (TEAM_BLUE, "Blu", Color (0, 0, 255, 255))
team.SetUp (TEAM_SPEC, "Spectator", Color (255, 255, 255, 255))
We now have three teams, a red, a blue, and a white spectator.
Next we will begin to work on classes.