LUA:Gamemode from scratch
From GMod Wiki
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 -> | |
- | + | 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 -> | gamemode -> |
Latest revision as of 14:12, 20 July 2011
Lua: How to make a Gamemode from the Basics |
Description: | Shows people how to make a Gamemode from the basics. |
Original Author: | Philip Dyplin |
Created: | 7th March, 2009 |
Contents |
Gamemode from scratch
Files and folders
- I made this tutorial because the other one didn't have all the basic information. It just said, "download this, then do this and this", but here you will learn it all from the basics.
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.
- Now we will create the simple and basic parts for the gamemode.
Add the basics
- In init.lua add
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.
- In cl_init.lua add
include( 'shared.lua' ) --Tell the client to load shared.lua
Here we have told the client to load shared.lua.
- In shared.lua add
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
- In shared.lua add
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.
- And now the shared.lua file should look like this
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
- In init.lua add
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.
- Now we will add a simple VGUI window to change team and we will another team. And we will also add two console command to change team.
- In cl_init.lua add
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
- In shared.lua
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".
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
- Now add the other teams loadout.
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.
- So, now init.lua should look like
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.
- Now we need to add the function to set the players team.
Add commands to change team
- In init.lua add
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
- Init.lua should now look like this
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
- Cl_init.lua should look like
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 )
- Shared.lua should look like
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