Basic STOOL

From GMod Wiki

Jump to: navigation, search
Lua: Quick and Dirty STOOL tutorial
Page white text.png Description:Tutorial covering making a STOOL for the tool gun
link=User: Richard Croxford Original Author: Richard Croxford
Calendar.png Created:16 January 2007


Contents

How To Create a STOOL

Step 1. Create a new .lua file in the STOOLS directory.

Either you can create it in the gamemode stool directory: \garrysmod\gamemodes\sandbox\entities\weapons\gmod_tool\stools\

Or if you were creating an addon you can place here: \garrysmod\addons\<youraddon>\lua\weapons\gmod_tool\stools\

Do not copy the gmod_tool folder form the game mode directory unless you are changing the functionality of the tool gun.

Step 2. Naming your tool

Edit the file you just created. Add the following code to the file and edit as appropriate

 
TOOL.Category = "Examples"
TOOL.Name = "Your Tools name"
TOOL.Command = nil
TOOL.ConfigName = "" --Setting this means that you do not have to create external configuration files to define the layout of the tool config-hud 
 

Step 3. Making it do something useful

The following functions should be self explanatory:

 
function TOOL:LeftClick( trace )
end
 
function TOOL:RightClick( trace )
end
 

Hint: The variable trace is a TraceRes

Step 4. Configuring your tool

Configuration parameters are set through the TOOL.ClientConVar table, you can set default values for parameters with the following code:

 
TOOL.ClientConVar[ "myparameter" ] = "fubar"
 

This should be used out side of any method decelerations so it is run when the tool is loaded.

You can then access the value that your user has chosen with these functions:

 
a = self:GetClientNumber("somenumber", defaultvalue) --Gets number called "somenumber"
a = self:GetClientInfo("somestring", defaultvalue) --Gets a string
 

Step 5. Creating the Configuration Hud

Okay, all of the "core" garrys mod tools have their huds defined in external text files stored at: garrysmod\settings\controls This has the disadvantage that these file will not be downloaded to clients, it is also rather messy to have additional files that are not needed.

You can how ever create your hud from within the lua script by defining TOOL.BuildCPanel(panel) but remember in step one I said to set TOOL.ConfigName = "", this variable seems to override the file that the hud should look at when drawing the hud, setting it to "" means it will look to the code.

Here is an example Build CPanel, this will create a config panel with 3 options a check box, a slider and a color picker.

 
 
function TOOL.BuildCPanel(panel)
  panel:AddControl("Header", { Text = "Example TOOL", Description = "Just an little example" })
 
  panel:AddControl("CheckBox", {
    Label = "A Boolean Value",
    Command = "example_bool"
  })
  panel:AddControl("Slider", {
    Label = "Example Number",
    Type = "Float",
    Min = "0",
    Max = "10000",
    Command = "example_number"
  })
 
  panel:AddControl("Color", {
    Label = "A Color",
    Red = "example_color_r",
    Blue = "example_color_b",
    Green = "example_color_g",
    Alpha = "example_color_a",
    ShowHSV = 1,
    ShowRGB = 1,
    Multiplier = 255 --You can change this to make the rgba values go up to any value
  })
 
end
 
 

Many other config panel widgets are possible apart from those listed here, have a look at the settings files for the default tools.

Please note: when you are defining your config panel you should affix "<toolname>_" to the beginning of you variables so if you tool was called 'fubar' and you had a variable called 'ftw' it would be called 'fubar_ftw', also the toolname refers to the file name of the stool with out the .lua not the contents of TOOL.Name

Whole script for copy pasting

 
TOOL.Category = "Examples"
TOOL.Name = "Your Tools name"
TOOL.Command = nil
TOOL.ConfigName = "" --Setting this means that you do not have to create external configuration files to define the layout of the tool config-hud 
 
TOOL.ClientConVar[ "myparameter" ] = "fubar"
 
function TOOL:LeftClick( trace )
end
 
function TOOL:RightClick( trace )
end
 
function TOOL.BuildCPanel( panel )
	panel:AddControl("Header", { Text = "Example TOOL", Description = "Just an little example" })
 
	panel:AddControl("CheckBox", {
	    Label = "A Boolean Value",
	    Command = "example_bool"
	})
	panel:AddControl("Slider", {
	    Label = "Example Number",
	    Type = "Float",
	    Min = "0",
	    Max = "10000",
	    Command = "example_number"
	})
 
	panel:AddControl("Color", {
	    Label = "A Color",
	    Red = "example_color_r",
	    Blue = "example_color_b",
	    Green = "example_color_g",
	    Alpha = "example_color_a",
	    ShowHSV = 1,
	    ShowRGB = 1,
	    Multiplier = 255 --You can change this to make the rgba values go up to any value
	})
end
 
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox