Activating different tool's context panels?

This is just incredibly confusing, how can I change the context panel’s tool?

What sort of black magic do I need to summon to set this context panel to tool named x (“motor” for example)?
I just spent hours trying to demangle Garry’s ridiculous spawnmenu code and I couldn’t find anything usable.
The panels are somewhere nested in the categories nested in the tool menus and so on.

This is how you can override content of a tools context menu:

local panel = controlpanel.Get( "item_item_crate" )
		if ( !panel ) then MsgN( "Couldn't find item_item_crate panel!" ) return end

		panel:AddControl( "Slider", { Label = "#tool.item_item_crate.desired_health", Max = 100, Command = "item_item_crate_desired_health" } )

What exactly are you trying to do?

He’s talking about showing the corresponding menu for a tool. So when you use “gmod_toolmode weld”, the weld menu appears.

Use gmod_tool x?

That only brings the tool up, it doesn’t activate the menu.

item.DoClick = function( button ) 
			local cp = controlpanel.Get( button.Name )
			if ( !cp:GetInitialized() ) then
				cp:FillViaTable( button )
			spawnmenu.ActivateToolPanel( self:GetTabID(), cp )
			if ( button.Command ) then
				LocalPlayer():ConCommand( button.Command )

This is how it’s done.

It took me literally 3 minutes to look up.

I’ve seen that many times but it doesn’t help a single bit.
What is the button itself and how can I locate the motor tool’s button?

How’s that not helpful? That is exactly what you are looking for. button.Command is the “gmod_tool motor”, button.Name is “motor”, self:GetTabID() is the tab id of the tool tab, it’s either 1 or 0.

You can find out what to put into cp:FillViaTable( button ) as button on your own, if you tried.

Okay, and then how are you supposed to get the button table without looping through every tool?

You expect me to do everything for you?

Hey Robotboy, do you actually know how to do it or are you simply assuming that it’s really easy and they are simply ignorant?

It’s not that simple.

In theory, bringing up a tool’s control panel is as simple as doing this:

[lua]local toolname = “motor”

– Get the control panel associated with this tool
local cp = controlpanel.Get(toolname)
– Activate the control panel
spawnmenu.ActivateToolPanel(1, cp)[/lua]

The 1 in ActivateToolPanel is there because there is one tool panel for each tab in the tools section of the spawnmenu (Tools, Utilities, Options). You obviously want to use the one which holds the list of tools, which is the first one.

In practice it’s not so easy because control panels aren’t populated until you bring them up by using their associated tool. Suddenly, things get a lot more complicated because there’s no easy way to get the information that’s required to fill a control panel. You could go through every button in every category to find the tool you’re looking for but that’s incredibly tedious and you won’t be able to figure it out without some trial and error.

Here’s how I’d do it:

[lua]local toolname = “motor”

– Get the tool’s metatable (which is stored within the toolgun’s metatable)
local tool = weapons.GetStored(“gmod_tool”).Tool[toolname]
– If no metatable was found, it means there’s no such tool
if not tool then return end

– Get the control panel associated with this tool
local cp = controlpanel.Get(toolname)

– If the control panel hasn’t been initialized yet, populate it
if not cp:GetInitialized() then
local item = {
Name = toolname;
Text = tool.Name or “#”…toolname;
Controls = tool.ConfigName or toolname;
Command = tool.Command or "gmod_tool "…toolname;
ControlPanelBuildFunction = tool.BuildCPanel;

– Activate the control panel
spawnmenu.ActivateToolPanel(1, cp)[/lua]

It’s not very fancy but it should do the job. If you want to understand the logic behind this, take a look at how the toolgun is implemented (gamemodes/sandbox/entities/weapons/gmod_tool), especially stool.lua, as well as the spawnmenu and controlpanel modules (lua/includes/modules).

I don’t guarantee that the above code works since I can’t test it at the moment, but if it doesn’t I can try to fix it if you want.

I love you _Killburn, it works like a charm!

It works great. Thanks a lot.