LUA:Gamemode Basics

From GMod Wiki

Jump to: navigation, search

To start off download this base: http://garrysmod.org/downloads/?a=view&id=39584 As you will see it's empty.

Before the rest of the tutorial, I presume that you know how the Garry's Mod file hierarchy works.

Extract this skeleton to garrysmod\garrysmod\Gamemodes\. Create a folder in there (Say Skeleton_Gamemode) and extract the 3 files from the zip into this.

Contents

How to add teams

Setting up teams is done pretty easily:

 
team.SetUp( 1, "Guests", Color(125, 255, 125, 255) ) 
 

There are 3 parts:

This goes in \gamemode\shared.lua

Changing Default Functions

We will start by changing the default weapons of the player. This is also placed into \gamemode\init.lua

 
function GM:PlayerLoadout(ply)
	local team = ply:Team() --Here we retrieve the player's team.
 
	if (team == 1) then --Is the player on the first team?
	   ply:Give("weapon_smg1")
	   ply:GiveAmmo(255, "smg1")
	end
 
	if (ply:IsAdmin()) then --Is the player an admin?
	   ply:Give("weapon_rpg") --Give him a the rpg
	   ply:GiveAmmo(999, "RPG_Round") --Give him almost infinite munition
	end
 
	ply:Give("weapon_crowbar") --When is neither in group 1 or an admin give him only a crowbar
end
 

So here is a lot:

 
function GM:PlayerLoadout(ply)
 

This is called every time a player spawns and is given his weapons.

 
ply:Team() 
 

This returns the player's team index.

 
ply:Give("weapon_rpg")
 

This gives the player the RPG. The parameters are: Player:Give(classname)

 
ply:GiveAmmo(255, "smg1")
 

This gives the player some ammunition. The parameters are: Player:GiveAmmo(amount, ammotype) For the ammo types, see [1].

 
ply:IsAdmin()
 

This returns a bool (true or false); true if the player is an admin and false if he isn't.


Our First Project

So now we will make a simple game. It will consist of:

Well let's start, we will need a lot of functions, here are some (If you don't know what they do then look for it yourself I will not explain it here.):

team.SetUp
Player:IsAdmin()
Player:IsSuperAdmin
etc...

Okay, now we will set up teams

 
--Shared File, this one goes into the shared.lua
 
team.SetUp(1, "Team Blue", Color(0, 0, 255, 255) ) --As the names says it Team Blue
 
team.SetUp(2, "Team Red", Color(255, 0, 0, 255) )
 

As you see this will make 2 teams: "Team Blue" and "Team Red"

And here the default weapons and the spawn:

 
--Server File this goes into init.lua
 
function GM:PlayerLoadout(ply)
    if (ply:Team() == 1) then --Is he a Blue guy?
        ply:Give("weapon_smg1")
        ply:GiveAmmo(255, "smg1")
    else --Covers the rest; most likely red team
        ply:Give("weapon_crowbar")
        ply:Give("weapon_pistol")
        ply:GiveAmmo(255, "pistol")
    end
end
 
 

As you see I check if the player is on the blue team. If he is, then I give him a SMG; if he's on the red team then I give him a crowbar and pistol.

 
--Server file still goes into init.lua
 
function GM:PlayerInitialSpawn(ply)
    local teamn = math.random(1, 2) --take a random number between 1 or 2
    math.randomseed(os.time()) --This makes sure the teams will always be random
 
    if team.NumPlayers(1) < team.NumPlayers(2) and teamn == 1 then
    ply:SetTeam(1) --Set it to our Player
    else
    ply:SetTeam(2) --Set it to our Player
    end
end
 
 
 


To be continued.

Gamemode from scratch explained

Lua: How to make a Gamemode from the Basics
Page white text.png Description:Shows people how to make a Gamemode from the basics.
link=User:Philip Dyplin Original Author:Philip Dyplin
Calendar.png Created:7th March, 2009

Files and folders

Before we start you need to need the files/folder hierarchy for Garry's Mod. Go to your gamemode folder, create a new folder and name it like you want your gamemode to be named. Go inside the folder and add another folder called "gamemode" or "Gamemode" if it's not named like sad the gamemode won't load. Inside the gamemode folder create three files and save them like "init.lua", "shared.lua" and "cl_init.lua". Now you are done with the folders and files needed for this part.

Add the basics

AddCSLuaFile( "cl_init.lua" ) --Tell the server that the client need to download cl_init.lua
AddCSLuaFile( "shared.lua" ) --Tell the server that the client need to download shared.lua
 
include( 'shared.lua' ) --Tell the server to load shared.lua
 

Now we have told the server that the clint need to download cl_init.lua and shared.lua, and that the server need to load shared.lua.

include( 'shared.lua' ) --Tell the client to load shared.lua
 

Here we have told the client to load shared.lua

GM.Name 	= "Test" --Set the gamemode name
GM.Author 	= "Pdkm931" --Set the author name
GM.Email 	= "N/A" --Set the author email
GM.Website 	= "N/A" --Set the author website
 

Here we tell both the server and client who the author is and the gamemode name.

So now you have the basic layout for the gamemode, now we just need to add a team.

Add a team

team.SetUp( 1, "Guest", Color( 125, 125, 125, 255 ) ) --Here we make the team Guests

Now we have made a team, and now I will explain the parts in the code.

team.SetUp is the basic function to make a team. Without it we will not have a team in the gamemode. 1 stands for the team number, this will be needed later on and in other files that you can add by yourself later. "Guest" stand for the team name, with this Guest will show up as the team in the scoreboard. Color will let us set the colour for the gamemode, ( 125, 125, 125, 255 ) specifies the colour we want. The setup in colour is ( red, green, blue, alpha ), Alpha is the value for how translucent or opaque the colour is, 0 is fully translucent and 255 is fully opaque.

GM.Name 	= "Test" --Set the gamemode name
GM.Author 	= "Pdkm931" --Set the author name
GM.Email 	= "N/A" --Set the author email
GM.Website 	= "N/A" --Set the author website
 
team.SetUp( 1, "Guest", Color( 125, 125, 125, 255 ) )
 

Now we will make the players join our team and give them the Gravity Gun.

Set the team on first spawn and team loadout

function GM:PlayerInitialSpawn( ply ) --"When the player first joins the server and spawns" function
 
    ply:SetTeam( 1 ) --Add the player to team 1
 
end --End the "when player first joins server and spawn" function
 
function GM:PlayerLoadout( ply ) --Weapon/ammo/item function
 
    if ply:Team == 1 then --If player team equals 1
 
        ply:Give( "weapon_physcannon" ) --Give them the Gravity Gun
 
    end --Here we end the if condition
 
end --Here we end the Loadout function
 

With this code we will force the player to join team 1 ("Guest") and then give the player the Gravity Gun.

The "GM:PlayerInitialSpawn" is called the first time the player spawn in the server. And the "GM:PlayerLoadout" is called every spawn (i think) and gives the player/team the weapon/ammo/items that is stated under it.

Add a menu to chose team

function set_team
 
local frame = vgui.Create( "DFrame" )
frame:SetPos( ScrH() / 2, ScrW() / 2 ) --Set the wondow in the middle of the players screen/game window
frame:SetSize( 200, 210 ) --Set the size
frame:SetTitle( "Change Team" ) --Set title
frame:SetVisible( true )
frame:SetDraggable( false )
frame:ShowCloseButton( true )
frame:MakePopup()
 
team_1 = vgui.Create( "DButton", frame )
team_1:SetPos( frame:GetTall() / 2, 5 ) --Place it half way on the tall and 5 units in hirizontal
team_1:SetSize( 50, 100 )
team_1:SetText( "Team 1" )
team_1.DoClick = function() --Make the player join team 1
 
    RunConsoleCommand( "team_1" )
 
end
 
team_2 = vgui.Create( "DButton", frame )
team_2:SetPos( frame:GetTall() / 2, 105 ) --Place it next to our previous one
team_2:SetSize( 50, 100 )
team_2:SetText( "Team 2" )
team_2.DoClick = function() --Make the player join team 2
 
    RunConsoleCommand( "team_2" )
 
end
 
end
concommand.Add( "team_menu", set_team )
 

Now we have made a simple VGUI window that can be used to change team.

Now add another team like we did before.

Add another team

team.SetUp( 2, "Another Guest", Color( 225, 225, 0 , 225 ) )

Now we add so that when the player first spawn, run the console command "team_menu".

Chose team menu shows on spawn

function GM:PlayerInitialSpawn( ply ) --"When the player first joins the server and spawns" function
 
    ply:RunConsoleCommand( "team_menu" ) --Run the console command when the player first spawns
 
end --End the "when player first joins server and spawn" function
 
function GM:PlayerLoadout( ply ) --Weapon/ammo/item function
 
    if ply:Team == 1 then --If the player is in team 1
 
        ply:Give( "weapon_physcannon" ) --Give them the Gravity Gun
 
    elseif ply:Team == 2 then --If the player is in team 2
 
        ply:Give( "weapon_physgun" ) --Give the player the Physics Gun
 
    end --Here we end the if condition
 
end --Here we end the Loadout function
 
AddCSLuaFile( "cl_init.lua" ) --Tell the server that the client need to download cl_init.lua
AddCSLuaFile( "shared.lua" ) --Tell the server that the client need to download shared.lua
 
include( 'shared.lua' ) --Tell the server to load shared.lua
 
function GM:PlayerInitialSpawn( ply ) --"When the player first joins the server and spawns" function
 
    ply:RunConsoleCommand( "team_menu" ) --Run the console command when the player first spawns
 
end --End the "when player first joins server and spawn" function
 
function GM:PlayerLoadout( ply ) --Weapon/ammo/item function
 
    if ply:Team == 1 then --If the player is in team 1
 
        ply:Give( "weapon_physcannon" ) --Give them the Gravity Gun
 
    elseif ply:Team == 2 then --If the player is in team 2
 
        ply:Give( "weapon_physgun" ) --Give the player the Physics Gun
 
    end --Here we end the if condition
 
end --Here we end the Loadout function
 

Add commands to change team

function team_1( ply )
 
    ply:SetTeam( 1 )
 
end
 
function team_2( ply )
 
    ply:SetTeam( 2 )
end
 
concommand.Add( "team_1", team_1 )
concommand.Add( "team_2", team_2 )
 

Now when the player prees the button for team 1 or 2 the player will join team 1 or 2.

How the files should look like now

AddCSLuaFile( "cl_init.lua" ) --Tell the server that the client need to download cl_init.lua
AddCSLuaFile( "shared.lua" ) --Tell the server that the client need to download shared.lua
 
include( 'shared.lua' ) --Tell the server to load shared.lua
 
function GM:PlayerInitialSpawn( ply ) --"When the player first joins the server and spawns" function
 
    ply:RunConsoleCommand( "team_menu" ) --Run the console command when the player first spawns
 
end --End the "when player first joins server and spawn" function
 
function GM:PlayerLoadout( ply ) --Weapon/ammo/item function
 
    if ply:Team == 1 then --If the player is in team 1
 
        ply:Give( "weapon_physcannon" ) --Give them the Gravity Gun
 
    elseif ply:Team == 2 then --If the player is in team 2
 
        ply:Give( "weapon_physgun" ) --Give the player the Physics Gun
 
    end --Here we end the if condition
 
end --Here we end the Loadout function
 
function team_1( ply )
 
    ply:SetTeam( 1 ) --Make the player join team 1
 
end
 
function team_2( ply )
 
    ply:SetTeam( 2 ) --Make the player join team 2
end
 
concommand.Add( "team_1", team_1 ) --Add the command to set the players team to team 1
concommand.Add( "team_2", team_2 ) --Add the command to set the players team to team 2
 


include( 'shared.lua' ) --Tell the client to load shared.lua
 
function set_team
 
local frame = vgui.Create( "DFrame" )
frame:SetPos( ScrH() / 2, ScrW() / 2 ) --Set the wondow in the middle of the players screen/game window
frame:SetSize( 200, 210 ) --Set the size
frame:SetTitle( "Change Team" ) --Set title
frame:SetVisible( true )
frame:SetDraggable( false )
frame:ShowCloseButton( true )
frame:MakePopup()
 
team_1 = vgui.Create( "DButton", frame )
team_1:SetPos( frame:GetTall() / 2, 5 ) --Place it half way on the tall and 5 units in hirizontal
team_1:SetSize( 50, 100 )
team_1:SetText( "Team 1" )
team_1.DoClick = function() --Make the player join team 1
 
    RunConsoleCommand( "team_1" )
 
end
 
team_2 = vgui.Create( "DButton", frame )
team_2:SetPos( frame:GetTall() / 2, 105 ) --Place it next to our previous one
team_2:SetSize( 50, 100 )
team_2:SetText( "Team 2" )
team_2.DoClick = function() --Make the player join team 2
 
    RunConsoleCommand( "team_2" )
 
end
 
end
concommand.Add( "team_menu", set_team )
 
GM.Name 	= "Test" --Set the gamemode name
GM.Author 	= "Pdkm931" --Set the author name
GM.Email 	= "N/A" --Set the author email
GM.Website 	= "N/A" --Set the author website
 
team.SetUp( 1, "Guest", Color( 125, 125, 125, 255 ) )
 
team.SetUp( 2, "Another Guest", Color( 225, 225, 0 , 225 ) )
 

Now we have two teams, a menu to chose team and we gave the teams a loadout.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox