LUA:RD2Tool

From GMod Wiki

Revision as of 15:31, 10 February 2011 by garry :D (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Lua: RD2 Toolguns
Page white text.png Description:How to create RD2 Toolguns.
link=User:KillerLUA Original Author:KillerLUA
Calendar.png Created:March 12, 2010

Contents


Introduction

RD2 has made it extremely easy for users to add entites to toolguns, you only have to set a few things and your away.

In this tutorial. I will show you how to add the black hole cache, however. It will work with just about any other entity you throw at it!

As in all my other tutorials, you will need a gamemode or pre-coded addon to throw this in with.

Let's create the toolgun!

Setting up the files

Gamemode

Create a new lua file called admin_spawnable.lua in YourGamemode/entites/weapons/gmod_tool/stools/admin_spawnable.lua

Now, let's move on. First we are going to start out with some base code.

Addon

Create a new lua file called admin_spawnable.lua in YourAddon/lua/weapons/gmod_tool/stools/admin_spawnable.lua

Now, let's move on. First we are going to start out with some base coded.

Coding it

 
TOOL.Category = '(Admin Spawnables)' --Name of catagory
TOOL.Name = '#Admin Spawnables' --Name of toolgun
TOOL.Command = nil --Just set this to nil
TOOL.ConfigName = '' --Set it to ''
TOOL.Tab = "Life Support 2" --The tab we want the catagory and tool to appear in
 
TOOL.ClientConVar['type'] = 'black_hole_cache' --Default entity selected
TOOL.ClientConVar['model'] = 'models/Combine_Helicopter/helicopter_bomb01.mdl' --Default model of entity selected
 
cleanup.Register('admin_spawn') --Add a cleanup
 

As explained in the comments, we have set a few key things to the tool. If we don't set TOOL.Tab it will appear in the default tool tab.

Now, we're going to add the language. An important thing is that the name of the lua file, excluding the .lua extension is the lua name of the toolgun. So while adding language, you must use the lua name in the middle like this:

 
if ( CLIENT ) then
	language.Add( 'Tool_admin_spawnable_name', 'Admin Spawnables' )
	language.Add( 'Tool_admin_spawnable_desc', 'Create an admin spawnable sent on a surface.' )
	language.Add( 'Tool_admin_spawnable_0', 'Left-Click: Spawn a SENT.' )
 
	language.Add( 'Undone_admin_spawnable', 'Admin Spawnable undone' )
	language.Add( 'Cleanup_admin_spawnable', 'Admin Spawnables' )
	language.Add( 'Cleaned_admin_spawnable', 'Cleaned up all admin spawnables.' )
	language.Add( 'SBoxLimit_admin_spawnable', 'Maximum Admin Spawnables Reached' )
end
 

The if client statement checks that the lua file is being run on the client, this way you won't get clientside code being run on the server and causing heavy errors.

Next, we are going to add the rest of the code:

 
if not ( RES_DISTRIB == 2 ) then Error("Please Install Resource Distribution 2 Addon.'" ) return end --Checks if the RD version is 2. If it's not two, don't run the code.
 
local admin_spawnables = {} --Create a new table
 
local admin_spawnables_models = {
	{ 'Black Hole Cache', 'models/Combine_Helicopter/helicopter_bomb01.mdl', 'black_hole_cache' } --Add the Black Hole Cache to the list
}
 

Now, and the easy bit of the toolgun. Is registering it, this will make the toolgun usable:

 
RD2_ToolRegister( TOOL, admin_spawnables_models, nil, "admin_spawnable", 30, admin_spawnable )
--RD2_ToolRegister( "The toolgun", "Models table", "Always set to nil", "Unique name", "Maximum of the entites", "Toolgun LUA Name")
 

Final code

This is all the code done, put it together and get:

 
TOOL.Category = '(Admin Spawnables)' --Name of catagory
TOOL.Name = '#Admin Spawnables' --Name of toolgun
TOOL.Command = nil --Just set this to nil
TOOL.ConfigName = '' --Set it to ''
TOOL.Tab = "Life Support 2" --The tab we want the catagory and tool to appear in
 
TOOL.ClientConVar['type'] = 'black_hole_cache' --Default entity selected
TOOL.ClientConVar['model'] = 'models/Combine_Helicopter/helicopter_bomb01.mdl' --Default model of entity selected
 
cleanup.Register('admin_spawn') --Add a cleanup
 
if ( CLIENT ) then
	language.Add( 'Tool_admin_spawnable_name', 'Admin Spawnables' )
	language.Add( 'Tool_admin_spawnable_desc', 'Create an admin spawnable sent on a surface.' )
	language.Add( 'Tool_admin_spawnable_0', 'Left-Click: Spawn a SENT.' )
 
	language.Add( 'Undone_admin_spawnable', 'Admin Spawnable undone' )
	language.Add( 'Cleanup_admin_spawnable', 'Admin Spawnables' )
	language.Add( 'Cleaned_admin_spawnable', 'Cleaned up all admin spawnables.' )
	language.Add( 'SBoxLimit_admin_spawnable', 'Maximum Admin Spawnables Reached' )
end
 
if not ( RES_DISTRIB == 2 ) then Error("Please Install Resource Distribution 2 Addon.'" ) return end --Checks if the RD version is 2. If it's not two, don't run the code.
 
local admin_spawnables = {} --Create a new table
 
local admin_spawnables_models = {
	{ 'Black Hole Cache', 'models/Combine_Helicopter/helicopter_bomb01.mdl', 'black_hole_cache' } --Add the Black Hole Cache to the list
}
 
RD2_ToolRegister( TOOL, admin_spawnables_models, nil, "admin_spawnable", 30, admin_spawnable )
--RD2_ToolRegister( "The toolgun", "Models table", "Always set to nil", "Unique name", "Maximum of the entites", "Toolgun LUA Name")
 

Yarin Kaul Icon ArrowSquare32.png Back to KillerLUA Tutorials


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox