Weapon Model on HUD

I want to add a weapon model, basically just like the playermodel viewer.
I’m making a Starwars Battlefront themed HUD, and I want a
weapon viewer like it has.

Such as the image below

http://images-cdn.moviepilot.com/image/upload/v1426601579/battlefrontii-20100320-1751476-star-wars-battlefront-these-awesome-features-just-need-to-happen-star-wars-battlefront-can-it-beat-these-sta-jpeg-214337.jpg

Take a look at model panels, you can set those to any model you want.

and to parent the panel to the hud:

That did not show anything about weapon models

You should be able to get the players primary and secondary weapon, then draw them onto the hud (not in HUDPaint) using a DModelPanel.

Hint: Set the model to the weapon the player is currently holding.

So like iconmodel = vgui.Create(“DWeaponPanel”)?

pretty much what it says on the gmod wiki



local icon = vgui.Create( "DModelPanel")
icon:SetSize( 200, 200 )
icon:SetModel( LocalPlayer():GetActiveWeapon():GetModel() ) -- this should be the model, now I may be wrong, but all you need to do is set the model to the player weapon model
function icon:LayoutEntity( Entity ) return end -- disables default rotation

First of all ninety percent of coding is finding what you need, researching and looking around. I looked at your thread, after that first post. I was able to print my selected weapon. Though I am having problems because I have never done this before just like you. I will have to do further research into this. But the example below will help you, but you need to learn to research more. Best of luck to you. Open the script first as server, then client. Type “!menu” to execute the script, after you have done what I said before.

Lolle, thank you for the links.

Example: https://gist.github.com/Infinitynill/4fd88503ba5f54a3d9e7

That did not work for me.
Here is the code I used



hook.Add("InitPostEntity", "DrawPlayerModel", function()
	iconmodel = vgui.Create("DModelPanel")
	iconmodel:SetModel( LocalPlayer():GetActiveWeapon():GetModel() )
        function iconmodel:LayoutEntity( Entity ) return end
	iconmodel:SetPos(290, ScrH() - 160)
	iconmodel:SetAnimated(false)
	iconmodel:SetSize(100,100)
	iconmodel:SetCamPos( Vector( 18, 0, 65))
	iconmodel:SetLookAt( Vector( 0, 0, 66.5 ) )
	
	timer.Create("RefreshAvatar", 1, 0, function()
		if LocalPlayer():GetActiveWeapon():GetModel() ~= iconmodel.Entity:GetModel() then
			iconmodel:Remove()
			
			iconmodel = vgui.Create("DModelPanel")
			iconmodel:SetModel( LocalPlayer():GetActiveWeapon():GetModel() )
			function iconmodel:LayoutEntity( Entity ) return end
			iconmodel:SetPos(290, ScrH() - 160)
			iconmodel:SetAnimated(false)
			iconmodel:SetSize(100,100)
			iconmodel:SetCamPos( Vector( 18, 0, 60))
			iconmodel:SetLookAt( Vector( 0, 0, 66.5 ) )
		end
	end)
end)


and it gave me this error:

[ERROR] addons/blurryvision/lua/autorun/client/cl_main.lua:83: attempt to index field ‘Entity’ (a nil value)

  1. unknown - addons/blurryvision/lua/autorun/client/cl_main.lua:83

Correct me if I’m wrong, but I think the ‘E’ in ‘Entity’ must be lowercase

Still not showing the weapons model

Try something like this. Rather than initializing the model panel once at post init. This will check every tick if a client has a weapon model drawn on his screen, if not it will create one, but only once. After a panel exists it will just check if the model has changed. It will not create a new model panel every tick, that is important since it would be pretty fps hungry otherwise.

[lua]
–store the model outside any hooks/timers
local iconmodel

hook.Add(“HUDPaint”, “DrawPlayerModel”, function()
if !IsValid(iconmodel) then
print(“Setting up hud model”)
–this is used to create the model panel only once and not every tick (or every x seconds)
iconmodel = vgui.Create(“DModelPanel”)
iconmodel:SetModel( LocalPlayer():GetActiveWeapon():GetModel() )
function iconmodel:LayoutEntity( ent ) return end
iconmodel:SetPos(290, ScrH() - 160)
iconmodel:SetAnimated(false)
iconmodel:SetSize(100,100)
iconmodel:ParentToHUD()
–iconmodel:SetCamPos( Vector( 18, 0, 65)) --change them after you see the model.
–iconmodel:SetLookAt( Vector( 0, 0, 66.5 ) )
else
–change the model panel’s model if the player’s current weapon model changed
if LocalPlayer():GetActiveWeapon():GetModel() ~= iconmodel.Entity:GetModel() then
iconmodel:SetModel( LocalPlayer():GetActiveWeapon():GetModel() )
end
end
end)
[/lua]

Works with physgun and stuff, but keys, arrest batton give errors and delete HUD

AKA anything other than default HL2 stuff or weapons

Check if the Active weapon is valid and if the model is valid before you change it.

EDIT:

And in case the model is not valid try http://wiki.garrysmod.com/page/Weapon/GetWeaponWorldModel instead of :GetModel(). Just print GetWeaponWorldModel and GetModel and see which one gives better results.

for example…



    if LocalPlayer():GetActiveWeapon() == "keys" then continue end


But the method given above is better.

So how would I put that into this code:



hook.Add("HUDPaint", "DrawPlayerModel", function()
	if !IsValid(iconmodel) then
            --this is used to create the model panel only once and not every tick (or every x seconds)
            iconmodel = vgui.Create("DModelPanel")
	    iconmodel:SetModel( LocalPlayer():GetActiveWeapon():GetModel() )
            function iconmodel:LayoutEntity( ent ) return end
	    iconmodel:SetPos(0, ScrH() - 490)
	    iconmodel:SetAnimated(false)
	    iconmodel:SetSize(500,500)
            iconmodel:ParentToHUD()
	    iconmodel:SetCamPos( Vector( 0, 100, 0)) --change them after you see the model.
	    --iconmodel:SetLookAt( Vector( 0, 0, 66.5 ) )
        else
            --change the model panel's model if the player's current weapon model changed
            if LocalPlayer():GetActiveWeapon():GetModel() ~= iconmodel.Entity:GetModel() then
	        iconmodel:SetModel( LocalPlayer():GetActiveWeapon():GetModel() )
            end
        end
end)


Just try to replace GetModel() with GetWorldModel() and see if you get all models with that. And add an additional if condition before every model change.
[lua]
if IsValid(LocalPlayer():GetActiveWeapon()) then
–replace/set model
end
[/lua]

Honestly I gave you 90% of the code needed. The rest is just a bit trial and error, I am not going to do that for you.