How to properly add items to the spawnmenu when right clicking

This is quite the incentive to fix this bug.

A few months ago, I uploaded an addon called EGS.

With EGS, you have the ability to right click an item to load it into the tool- similar to the creator tool. See example:

While this works, it causes an issue with other spawnmenu addons, and breaks searching functionality.

Here’s a snippet of how models are given this context menu option.


    spawnmenu.AddContentType( "model", function( container, obj )

        local icon = vgui.Create( "SpawnIcon", container )

        if ( obj.body ) then
            obj.body = string.Trim( tostring(obj.body), "B" )
        end

        if ( obj.wide ) then
            icon:SetWide( obj.wide )
        end

        if ( obj.tall ) then
            icon:SetTall( obj.tall )
        end

        icon:InvalidateLayout( true )

        icon:SetModel( obj.model, obj.skin or 0, obj.body )

        icon:SetTooltip( string.Replace( string.GetFileFromFilename(obj.model), ".mdl", "" ) )

        icon.DoClick = function( icon ) surface.PlaySound( "ui/buttonclickrelease.wav") RunConsoleCommand( "gm_spawn", icon:GetModelName(), icon:GetSkinID() or 0, icon:GetBodyGroup() or "" ) end
        icon.OpenMenu = function( icon )

            function checkInstalledPropLauncher()
              if file.Exists( "addons/prop_launcher_(launch_any_prop)_491910658", "GAME" ) == true then
                return true
              elseif file.Exists( "addons/ds_491910658.gma", "GAME" ) == true then
                return true
              elseif file.Exists( "weapons/prop_launcher.lua", "LUA" ) == true then
                return true
              else
                return false
              end
            end

            local menu = DermaMenu()
            menu:AddOption( "Copy to Clipboard", function() SetClipboardText( string.gsub(obj.model, "\\", "/") ) end ):SetImage( copy_icon )
            menu:AddOption( "Spawn Group using EGS", function() RunConsoleCommand( "gmod_tool", "egs" ) RunConsoleCommand( "egs_spawnmenu_incompatible", "0" )  RunConsoleCommand( "egs_ent_type", "4" ) RunConsoleCommand( "egs_ent_name", obj.model ) end ):SetImage( egs_icon )
            menu:AddOption( "Spawn using Toolgun", function() RunConsoleCommand( "gmod_tool", "creator" ) RunConsoleCommand( "creator_type", "4" ) RunConsoleCommand( "creator_name", obj.model ) end ):SetImage( creator_icon )
            if checkInstalledPropLauncher() then
              menu:AddOption( "Load into Prop Launcher", function() RunConsoleCommand( "pl_model", obj.model ) RunConsoleCommand( "give", "prop_launcher" ) RunConsoleCommand("use", "prop_launcher") end ):SetImage( pl_icon )
            end

            local submenu = menu:AddSubMenu( "Re-Render", function() icon:RebuildSpawnIcon() end )
                submenu:AddOption( "This Icon", function() icon:RebuildSpawnIcon() end )
                submenu:AddOption( "All Icons", function() container:RebuildAll() end )

            menu:AddOption( "Edit Icon", function()

                local editor = vgui.Create( "IconEditor" )
                editor:SetIcon( icon )
                editor:Refresh()
                editor:MakePopup()
                editor:Center()

            end ):SetImage( "icon16/wrench.png" )

            local ChangeIconSize = function( w, h )

                icon:SetSize( w, h )
                icon:InvalidateLayout( true )
                container:OnModified()
                container:Layout()
                icon:SetModel( obj.model, obj.skin or 0, obj.body )

            end

            local submenu = menu:AddSubMenu( "Resize", function() end )
                submenu:AddOption( "64 x 64 (default)", function() ChangeIconSize( 64, 64 ) end )
                submenu:AddOption( "64 x 128", function() ChangeIconSize( 64, 128 ) end )
                submenu:AddOption( "64 x 256", function() ChangeIconSize( 64, 256 ) end )
                submenu:AddOption( "64 x 512", function() ChangeIconSize( 64, 512 ) end )
                submenu:AddSpacer()
                submenu:AddOption( "128 x 64", function() ChangeIconSize( 128, 64 ) end )
                submenu:AddOption( "128 x 128", function() ChangeIconSize( 128, 128 ) end )
                submenu:AddOption( "128 x 256", function() ChangeIconSize( 128, 256 ) end )
                submenu:AddOption( "128 x 512", function() ChangeIconSize( 128, 512 ) end )
                submenu:AddSpacer()
                submenu:AddOption( "256 x 64", function() ChangeIconSize( 256, 64 ) end )
                submenu:AddOption( "256 x 128", function() ChangeIconSize( 256, 128 ) end )
                submenu:AddOption( "256 x 256", function() ChangeIconSize( 256, 256 ) end )
                submenu:AddOption( "256 x 512", function() ChangeIconSize( 256, 512 ) end )
                submenu:AddSpacer()
                submenu:AddOption( "512 x 64", function() ChangeIconSize( 512, 64 ) end )
                submenu:AddOption( "512 x 128", function() ChangeIconSize( 512, 128 ) end )
                submenu:AddOption( "512 x 256", function() ChangeIconSize( 512, 256 ) end )
                submenu:AddOption( "512 x 512", function() ChangeIconSize( 512, 512 ) end )

            menu:AddSpacer()
            menu:AddOption( "Delete", function() icon:Remove() hook.Run( "SpawnlistContentChanged" ) end ):SetImage( delete_icon )
            menu:Open()

        end

        icon:InvalidateLayout( true )

        if ( IsValid( container ) ) then
            container:Add( icon )
        end

        if ( iSkin != 0 ) then return end

        local iSkinCount = NumModelSkins( strModel )
        if ( iSkinCount <= 1 ) then return end

        for i=1, iSkinCount-1, 1 do

            self:AddModel( strModel, i )

        end

        return icon

    end )

I got this code from the spawnmenu.lua on Github, and I modified it for EGS. It has a compatability option for my other addon that hooks into the spawn menu- since only one addon can hook into the menu like this.
This code is terribly hacked in.
I’ve spent hours looking for how to do this right, but I’m not experienced enough with the gmod libraries to know what to use.

What I’d like is a hook that lets me simply add an item to the right-click menu.
What I might do realistically is hook into this the same way, but add it to an existing right-click menu(which I don’t know how to do either.)
There has to be a better way.

My addons EGS and Prop Launcher both greatly benefit from having this easy-accessible option.

How should I approach this to allow multiple addons to add entries to the right-click menu?