Weapon selector with Derma
From GMod Wiki
Line 18: | Line 18: | ||
In this tutorial I will guide you through the making of a working Derma menu that will allow us to give ourselves a weapon when we click a button. | In this tutorial I will guide you through the making of a working Derma menu that will allow us to give ourselves a weapon when we click a button. | ||
+ | We'll make use of console commands, "why would you need a console command for such thing as a weapon selection menu?" you ask, the answer is that console commands are the only client->server functions that we can use, and we can't create or give weapons clientside (imagine people on CSS or TF2 giving weapons to themself). | ||
== Coding our "weapon_give" console command == | == Coding our "weapon_give" console command == | ||
What we want our "weapon_take" console command is to give us a certain weapon that's specified in the 1st argument: | What we want our "weapon_take" console command is to give us a certain weapon that's specified in the 1st argument: | ||
- | |||
<lua> | <lua> | ||
function GivePlayerAWeapon( ply, cmd, args ) --Starting our function, ply is the player who ran the console command, cmd is the command name (in this case "weapon_give"), args is the arguments of the console command | function GivePlayerAWeapon( ply, cmd, args ) --Starting our function, ply is the player who ran the console command, cmd is the command name (in this case "weapon_give"), args is the arguments of the console command |
Latest revision as of 19:44, 12 February 2011
Lua: Weapon selection menu |
Description: | This is a tutorial about making a weapon selector menu. |
Original Author: | JohnnyThunders |
Created: | October 24, 2010 |
Contents |
Introduction
Knowledge prior to using this tutorial:
- basic Lua knowledge
- A brain
------------------
In this tutorial I will guide you through the making of a working Derma menu that will allow us to give ourselves a weapon when we click a button. We'll make use of console commands, "why would you need a console command for such thing as a weapon selection menu?" you ask, the answer is that console commands are the only client->server functions that we can use, and we can't create or give weapons clientside (imagine people on CSS or TF2 giving weapons to themself).
Coding our "weapon_give" console command
What we want our "weapon_take" console command is to give us a certain weapon that's specified in the 1st argument:
function GivePlayerAWeapon( ply, cmd, args ) --Starting our function, ply is the player who ran the console command, cmd is the command name (in this case "weapon_give"), args is the arguments of the console command if args[1] == "pistol" then --if the 1st argument is "pistol" then do: ply:Give("weapon_pistol") --give the player a pistol ply:ChatPrint("You got a pistol!") --print "You got a pistol!" in the chat end --close our if loop if args[1] == "smg" then --if the 1st argument is "smg" then do: ply:Give("weapon_smg") --give the player an SMG ply:ChatPrint("You got an SMG!") --print "You got an SMG!" in the chat end--close our if loop end --close our function concommand.Add("weapon_take", GivePlayerAWeapon) --make the console command "weapon_take" run the GivePlayerAWeapon function
Place the code above in a file named "weapongive.lua" and place it in "garrysmod/lua/autorun/server" (if you don't have the folders create them)
Our derma menu will be a frame with 2 buttons in it, one that will give us a pistol and one that will give us an SMG
function WeaponSelectorDerma() local WeaponFrame = vgui.Create("DFrame") --create a frame WeaponFrame:SetSize(250, 80) --set its size WeaponFrame:Center() --position it at the center of the screen WeaponFrame:SetTitle("Take the weapon that you want") --set the title of the menu WeaponFrame:SetDraggable(true) --can you move it around WeaponFrame:SetSizable(false) --can you resize it? WeaponFrame:ShowCloseButton(true) --can you close it WeaponFrame:MakePopup() --make it appear local PistolButton = vgui.Create("DButton", WeaponFrame) PistolButton:SetSize(100, 30) PistolButton:SetPos(10, 35) PistolButton:SetText("Pistol") PistolButton.DoClick = function() RunConsoleCommand("weapon_take", "pistol") WeaponFrame:Close() end --make it run our "weapon_take" console command with "pistol" as the 1st argument and then close the menu local SMGButton = vgui.Create("DButton", WeaponFrame) SMGButton:SetSize(100, 30) SMGButton:SetPos(140, 35) SMGButton:SetText("SMG") --Set the name of the button SMGButton.DoClick = function() RunConsoleCommand("weapon_take", "smg") WeaponFrame:Close() end end concommand.Add("selectweapon", WeaponSelectorDerma) --make the console command to make this menu popup
Place this script in garrysmod/lua/autorun/client (if you don't have the folders create them)