• Weapon Model on HUD
    16 replies, posted
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. [B]Such as the image below[/B] [IMG]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[/IMG]
Take a look at model panels, you can set those to any model you want. [url]http://wiki.garrysmod.com/page/Category:DModelPanel[/url] and to parent the panel to the hud: [url]http://wiki.garrysmod.com/page/Panel/ParentToHUD[/url]
[QUOTE=Lolle;49807028]Take a look at model panels, you can set those to any model you want. [url]http://wiki.garrysmod.com/page/Category:DModelPanel[/url] and to parent the panel to the hud: [url]http://wiki.garrysmod.com/page/Panel/ParentToHUD[/url][/QUOTE] That did not show anything about weapon models
[QUOTE='[FSG] ToXiiC;49807819']That did not show anything about weapon models[/QUOTE] 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.
[QUOTE=Tupac;49808258]Hint: Set the model to the weapon the player is currently holding.[/QUOTE] So like iconmodel = vgui.Create("DWeaponPanel")?
pretty much what it says on the gmod wiki [CODE] 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[/CODE]
[QUOTE='[FSG] ToXiiC;49807819']That did not show anything about weapon models[/QUOTE] 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: [url]https://gist.github.com/Infinitynill/4fd88503ba5f54a3d9e7[/url]
[QUOTE=funnygamemake;49808643]pretty much what it says on the gmod wiki [CODE] 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[/CODE][/QUOTE] That did not work for me. Here is the code I used [code] 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) [/code] 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
[QUOTE='[FSG] ToXiiC;49812162']That did not work for me. Here is the code I used [code] 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) [/code] 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[/QUOTE] Correct me if I'm wrong, but I think the 'E' in 'Entity' must be lowercase
[QUOTE=Tupac;49812872]Correct me if I'm wrong, but I think the 'E' in 'Entity' must be lowercase[/QUOTE] 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]
[QUOTE=Lolle;49814105]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][/QUOTE] Works with physgun and stuff, but keys, arrest batton give errors and delete HUD AKA anything other than default HL2 stuff or weapons
[QUOTE='[FSG] ToXiiC;49814159']Works with physgun and stuff, but keys, arrest batton give errors and delete HUD AKA anything other than default HL2 stuff or weapons[/QUOTE] Check if the Active weapon is valid and if the model is valid before you change it. [B]EDIT:[/B] And in case the model is not valid try [url]http://wiki.garrysmod.com/page/Weapon/GetWeaponWorldModel[/url] instead of :GetModel(). Just print GetWeaponWorldModel and GetModel and see which one gives better results.
[QUOTE='[FSG] ToXiiC;49814159']Works with physgun and stuff, but keys, arrest batton give errors and delete HUD AKA anything other than default HL2 stuff or weapons[/QUOTE] for example... [CODE] if LocalPlayer():GetActiveWeapon() == "keys" then continue end [/CODE] But the method given above is better.
[QUOTE=Lolle;49814237]Check if the Active weapon is valid and if the model is valid before you change it. [B]EDIT:[/B] And in case the model is not valid try [url]http://wiki.garrysmod.com/page/Weapon/GetWeaponWorldModel[/url] instead of :GetModel(). Just print GetWeaponWorldModel and GetModel and see which one gives better results.[/QUOTE] So how would I put that into this code: [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) [/code]
[QUOTE='[FSG] ToXiiC;49814994']So how would I put that into this code: [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) [/code][/QUOTE] 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.
Sorry, you need to Log In to post a reply to this thread.