LUA:Gamemode from scratch

From GMod Wiki

(Difference between revisions)
Jump to: navigation, search
m (Chosen team menu shows on spawn)
(Gamemode File Structure: Removed Caps)
 
Line 323: Line 323:
Then your gamemodes folder should look like this.
Then your gamemodes folder should look like this.
<lua>
<lua>
-
Content ->
+
content ->
-
           Data -- Put data files in here
+
           data -- Put data files in here
-
           Materials -- Put all your materials here.
+
           materials -- Put all your materials here.
-
           Models -- Put all your models here.
+
           models -- Put all your models here.
-
           Resource -- Don't need to touch this.
+
           resource -- Don't need to touch this.
-
           Scripts -- Don't need to touch this.
+
           scripts -- Don't need to touch this.
-
           Settings -- Server settings are in here.
+
           settings -- Server settings are in here.
-
Entities ->  
+
entities ->  
-
             Effects -- All effects you made go here.
+
             effects -- All effects you made go here.
-
             Entities -- All SENTs go here.
+
             entities -- All SENTs go here.
-
             Weapons -- All SWEPs go here.
+
             weapons -- All SWEPs go here.
gamemode ->  
gamemode ->  

Latest revision as of 14:12, 20 July 2011

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

Contents

Gamemode from scratch

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 as what 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 such, the gamemode won't load. Inside the gamemode folder, create three files and save them as "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 needs to download cl_init.lua
AddCSLuaFile( "shared.lua" ) --Tell the server that the client needs to download shared.lua
 
include( 'shared.lua' ) --Tell the server to load shared.lua
 

Now we have told the server that the client needs to download cl_init.lua and shared.lua, and that the server needs 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 color for the gamemode, and ( 125, 125, 125, 255 ) specifies the color that we want. The setup in color is ( red, green, blue, alpha ). Alpha is the value for how translucent or opaque the color is, and 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 spawns" 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. The "GM:PlayerLoadout" is called every time someone spawns and gives the player/team the weapon/ammo/items that is stated under it.

Add a menu to choose team

function set_team()
 
local frame = vgui.Create( "DFrame" )
frame:SetPos( ScrW() / 2, ScrH() / 2 ) --Set the window 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 horizontal
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 spawns, run the console command "team_menu".

Chosen team menu shows on spawn

function GM:PlayerInitialSpawn( ply ) --"When the player first joins the server and spawns" function
 
    ply:ConCommand( "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 ) --"The weapons/items that the player spawns with" function
 
	ply:StripWeapons() -- This command strips all weapons from the player.
 
	if ply:Team() == 1 then --If the player is on team "Guest"...
		ply:Give( "weapon_physcannon" ) -- ...then give them the Gravity Gun.
 
	elseif ply:Team() == 2 then -- Otherwise, if the player is on team "Another Guest"...
		ply:Give( "weapon_physgun" ) -- ...then give them the Phys Gun.
 
	end -- This ends the if/elseif.
 
end -- This ends the 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
 
    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) --"The weapons/items that the player spawns with" function
 
	ply:StripWeapons() -- This command strips all weapons from the player.
 
	if ply:Team() == 1 then --If the player is on team "Guest"...
		ply:Give("weapon_physcannon") -- ...then give them the Gravity Gun.
 
	elseif ply:Team() == 2 then -- Otherwise, if the player is on team "Another Guest"...
		ply:Give("weapon_physgun") -- ...then give them the Phys Gun.
 
	end -- This ends the if/elseif.
 
end -- This ends the 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 presses 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:ConCommand( "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) --"The weapons/items that the player spawns with" function
 
	ply:StripWeapons() -- This command strips all weapons from the player.
 
	if ply:Team() == 1 then --If the player is on team "Guest"...
		ply:Give("weapon_physcannon") -- ...then give them the Gravity Gun.
 
	elseif ply:Team() == 2 then -- Otherwise, if the player is on team "Another Guest"...
		ply:Give("weapon_physgun") -- ...then give them the Phys Gun.
 
	end -- This ends the if/elseif.
 
end -- This ends the function.
 
 function team_1( ply ) 
 
     ply:SetTeam( 1 ) //Make the player join team 1 
	 ply:Spawn()
 end 
 
 function team_2( ply ) 
 
     ply:SetTeam( 2 ) //Make the player join team 2 
	 ply:Spawn()
 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" )
 
 function set_team() 
 
 frame = vgui.Create( "DFrame" ) 
 frame:SetPos( 100, ScrH() / 2 ) //Set the window 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( 30, 30 )
 team_1:SetSize( 100, 50 ) 
 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( 30, 85 ) //Place it next to our previous one 
 team_2:SetSize( 100, 50 ) 
 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.

Gamemode Files/Folders Structure

In a basic gamemode folder you will have the following folders/subfolders and files. These folders/subfolders and foles will start off in this order.

steam -> steamapps -> <your name> -> garrysmod -> garrysmod -> gamemodes

Gamemode File Structure

Then your gamemodes folder should look like this.

 
content ->
           data -- Put data files in here
           materials -- Put all your materials here.
           models -- Put all your models here.
           resource  -- Don't need to touch this.
           scripts -- Don't need to touch this.
           settings -- Server settings are in here.
 
entities -> 
            effects -- All effects you made go here.
            entities -- All SENTs go here.
            weapons -- All SWEPs go here.
 
gamemode -> 
            shared.lua
            cl_init.lua
            init.lua
            -- You can add more files by using "include("your_lua_file_here.lua")" in your init.lua
 
 

In the gamemodes/ folder, create a new folder. Name it "GM_test" or something- it's the main folder. Go into "GM_test" and create another folder called "gamemode". This holds your base code. Then create one called "entities". This holds all your SWEPs, SENTs, and effects. Then create one called "content". This will hold all the sounds... Models... Materials et cetera.

Make another folder called content and another called entities.

Content represents a virtual file tree of your GMod folder I.E. content/cfg/lol.txt would appear in GMod to be in cfg/lol.txt if you were running your gamemode

info.txt

This text file in the gamemodes/mymod folder tells GMod about your gamemode. Here's an example of what it should look like:

"Gamemode"
 {
 	"name"		"GM_test"
 	"version"	"0.1" 
 	"up_date"	"02/12/2006" // The date this version was published. Set before release!
 
 	"author_name"	"Me."
 	"author_email"	"[email protected]"
 	"author_url"	"http://www.mywebsite.com/"
 
 	"info"		"This is my modification's description. It's not very good."
 	"icon" 	 	"" // Icon to show in the Mods list inside Garry's Mod
 	"hide"		"0" // Do not hide this gamemode from the Mods list inside Garry's Mod
 
	"mappattern" // This sets all maps with gt_ before the name to default the map gamemode to "GM_test"
	{
		"1"	"^gt_"
	}
  }
 

Note that you can force GMod to load your new gamemode by going to the options tab when creating a single or multiplayer game, and under 'Gamemode', choose your mod in the 'Override' drop down.

Links

Official SVN link: http://gamemode-from-scratch.googlecode.com/svn/trunk/r7/

Skeleton Gamemode: http://dl.dropbox.com/u/3590255/Downloads/Garry%27s%20Mod/Skeleton-Gamemode.zip

Next part in the tutorial

Part two.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox