• Replacing HUD textures according to the value of a convar
    10 replies, posted
Hello, I have recently made an addon that replaces the UI icon textures for the Camera, Tool Gun and Lua Sweps. I have also created an alternate set of textures with a style closer to HL2, which I would like to add to this same addon, and giving the choice of style to the user with a convar. (Making an options Tab in the Spawn Menu would be cool too, but I have no idea how that works) I'm really bad at Lua and would like to know how to make G-Mod check the value of this convar and replace the style accordingly, but here's what I've got already : [B]/lua/autorun/client/better_icons.lua[/B] [code]//If this is set to 1, use alternate set CreateConVar( "cl_betterhudicons_alt", "0", "128", "" )[/code] [B]/lua/betterhudicons/better_icons_alt.lua[/B] [code]//The materials local mat, mat_alt, mat2, mat2_alt, mat3, mat3_alt mat = Material( "vgui/gmod_camera.vmt" ) mat_alt = Material( "vgui/gmod_camera_alt.vmt" ) mat2 = Material( "vgui/gmod_tool.vmt" ) mat2_alt = Material( "vgui/gmod_tool_alt.vmt" ) mat3 = Material( "weapons/swep.vmt" ) mat3_alt = Material( "weapons/swep_alt.vmt" ) //Switch to HL2 style by replacing mat:SetTexture( "$basetexture", mat_alt:GetTexture( "$basetexture" ) ) mat2:SetTexture( "$basetexture", mat2_alt:GetTexture( "$basetexture" ) ) mat3:SetTexture( "$basetexture", mat3_alt:GetTexture( "$basetexture" ) )[/code] [B]/lua/betterhudicons/better_icons_original.lua[/B] [code]//The materials local mat, mat_ori, mat2, mat2_ori, mat3, mat3_ori mat = Material( "vgui/gmod_camera.vmt" ) mat_ori = Material( "vgui/gmod_camera_original.vmt" ) mat2 = Material( "vgui/gmod_tool.vmt" ) mat2_ori = Material( "vgui/gmod_tool_original.vmt" ) mat3 = Material( "weapons/swep.vmt" ) mat3_ori = Material( "weapons/swep_original.vmt" ) //Switch to original style by replacing mat:SetTexture( "$basetexture", mat_ori:GetTexture( "$basetexture" ) ) mat2:SetTexture( "$basetexture", mat2_ori:GetTexture( "$basetexture" ) ) mat3:SetTexture( "$basetexture", mat3_ori:GetTexture( "$basetexture" ) )[/code] I am aware this code is really awful and incomplete, but would really appreciate the help you guys can bring me. Thanks.
Does the material replacing bit actually work (do the materials actually change)? If they do, then try this for the convar bit (instead of creating a serverside convar it creates a client one and gets the value of it to check whether to replace the materials): [CODE] CreateClientConVar( "cl_betterhudicons_alt", '0', true, false ) local bettericons = GetConVar( "cl_betterhudicons_alt" ) if bettericons:GetBool() then local mat, mat_alt, mat2, mat2_alt, mat3, mat3_alt mat = Material( "vgui/gmod_camera" ) mat_alt = Material( "vgui/gmod_camera_alt" ) mat2 = Material( "vgui/gmod_tool" ) mat2_alt = Material( "vgui/gmod_tool_alt" ) mat3 = Material( "weapons/swep" ) mat3_alt = Material( "weapons/swep_alt" ) mat:SetTexture( "$basetexture", mat_alt:GetTexture( "$basetexture" ) ) mat2:SetTexture( "$basetexture", mat2_alt:GetTexture( "$basetexture" ) ) mat3:SetTexture( "$basetexture", mat3_alt:GetTexture( "$basetexture" ) ) else local mat, mat_ori, mat2, mat2_ori, mat3, mat3_ori mat = Material( "vgui/gmod_camera" ) mat_ori = Material( "vgui/gmod_camera_original" ) mat2 = Material( "vgui/gmod_tool" ) mat2_ori = Material( "vgui/gmod_tool_original" ) mat3 = Material( "weapons/swep" ) mat3_ori = Material( "weapons/swep_original" ) mat:SetTexture( "$basetexture", mat_ori:GetTexture( "$basetexture" ) ) mat2:SetTexture( "$basetexture", mat2_ori:GetTexture( "$basetexture" ) ) mat3:SetTexture( "$basetexture", mat3_ori:GetTexture( "$basetexture" ) ) end [/CODE]
[QUOTE=MPan1;48865301]Does the material replacing bit actually work (do the materials actually change)?[/QUOTE] The material replacing bit works, but only when they were separate files. (Using lua_openscript_cl) I tried using your code and setting "cl_betterhudicons_alt" to 0 and 1, and I fear it doesn't work, sadly. I really appreciate what you've done though, it looks a lot better than what I had. Just need to fix the material switching bit. I don't have any clues though. :S
[QUOTE=MPan1;48865301]Does the material replacing bit actually work (do the materials actually change)? If they do, then try this for the convar bit (instead of creating a serverside convar it creates a client one and gets the value of it to check whether to replace the materials): [CODE] CreateClientConVar( "cl_betterhudicons_alt", '0', true, false ) local bettericons = GetConVar( "cl_betterhudicons_alt" ) if bettericons:GetBool() then local mat, mat_alt, mat2, mat2_alt, mat3, mat3_alt mat = Material( "vgui/gmod_camera" ) mat_alt = Material( "vgui/gmod_camera_alt" ) mat2 = Material( "vgui/gmod_tool" ) mat2_alt = Material( "vgui/gmod_tool_alt" ) mat3 = Material( "weapons/swep" ) mat3_alt = Material( "weapons/swep_alt" ) mat:SetTexture( "$basetexture", mat_alt:GetTexture( "$basetexture" ) ) mat2:SetTexture( "$basetexture", mat2_alt:GetTexture( "$basetexture" ) ) mat3:SetTexture( "$basetexture", mat3_alt:GetTexture( "$basetexture" ) ) else local mat, mat_ori, mat2, mat2_ori, mat3, mat3_ori mat = Material( "vgui/gmod_camera" ) mat_ori = Material( "vgui/gmod_camera_original" ) mat2 = Material( "vgui/gmod_tool" ) mat2_ori = Material( "vgui/gmod_tool_original" ) mat3 = Material( "weapons/swep" ) mat3_ori = Material( "weapons/swep_original" ) mat:SetTexture( "$basetexture", mat_ori:GetTexture( "$basetexture" ) ) mat2:SetTexture( "$basetexture", mat2_ori:GetTexture( "$basetexture" ) ) mat3:SetTexture( "$basetexture", mat3_ori:GetTexture( "$basetexture" ) ) end [/CODE][/QUOTE] That is a very shitty way of doing it, here's what I'd recommend: [CODE] local bettericons = CreateClientConVar( "cl_betterhudicons_alt", '0', true, false ) local mats = { [ 0 ] = { -- When convar is 0 gmod_camera = "vgui/gmod_camera_original", gmod_tool = "vgui/gmod_tool_original", swep = "weapons/swep_original" }, [ 1 ] = { -- When convar is 1 gmod_camera = "vgui/gmod_camera_alt", gmod_tool = "vgui/gmod_tool_alt", swep = "weapons/swep_alt" }, } local id = 0 if ( bettericons:GetBool() ) then id = 1 end --You can also use this for more than 2 variations: --local id = bettericons:GetInt() local replace = mats[ id ] if replace then --If a replacement is found Material( "vgui/gmod_camera" ):SetTexture( "$basetexture", Material( replace.gmod_camera ):GetTexture( "$basetexture" ) ) Material( "vgui/gmod_tool" ):SetTexture( "$basetexture", Material( replace.gmod_tool ):GetTexture( "$basetexture" ) ) Material( "weapons/swep" ):SetTexture( "$basetexture", Material( replace.swep ):GetTexture( "$basetexture" ) ) else --If there is no variation for given convar value --Might want to revert back or something. --replace = mats[ 0 ]; --Material( "vgui/gmod_camera" ):SetTexture( "$basetexture", Material( replace.gmod_camera ):GetTexture( "$basetexture" ) ) --Material( "vgui/gmod_tool" ):SetTexture( "$basetexture", Material( replace.gmod_tool ):GetTexture( "$basetexture" ) ) --Material( "weapons/swep" ):SetTexture( "$basetexture", Material( replace.swep ):GetTexture( "$basetexture" ) ) end [/CODE]
[QUOTE=Robotboy655;48865727]That is a very shitty way of doing it, here's what I'd recommend: [CODE]Code[/CODE][/QUOTE] Thanks, it works perfectly ! Is it normal I have to use "lua_openscript_cl autorun/client/better_icons.lua" in the console to update the textures though ? Shouldn't it update the textures automatically when I change the value of "cl_betterhudicons_alt", as it is inside autorun ? (I'm extremely bad at Lua, sorry :suicide:)
[QUOTE=Burnout6010;48866169]Thanks, it works perfectly ! Is it normal I have to use "lua_openscript_cl autorun/client/better_icons.lua" in the console to update the textures though ? Shouldn't it update the textures automatically when I change the value of "cl_betterhudicons_alt", as it is inside autorun ? (I'm extremely bad at Lua, sorry :suicide:)[/QUOTE] It is, since your code won't update automatically with the convar being changed, just try using a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/cvars/AddChangeCallback]cvars.AddChangeCallback[/url] which should have the function of changing the icons depending on the value of your convar. For example: [CODE] local bettericons = CreateClientConVar( "cl_betterhudicons_alt", '0', true, false ) local mats = { [ 0 ] = { -- When convar is 0 gmod_camera = "vgui/gmod_camera_original", gmod_tool = "vgui/gmod_tool_original", swep = "weapons/swep_original" }, [ 1 ] = { -- When convar is 1 gmod_camera = "vgui/gmod_camera_alt", gmod_tool = "vgui/gmod_tool_alt", swep = "weapons/swep_alt" }, } local function Refresh() local id = 0 if ( bettericons:GetBool() ) then id = 1 end --You can also use this for more than 2 variations: --local id = bettericons:GetInt() local replace = mats[ id ] if replace then --If a replacement is found Material( "vgui/gmod_camera" ):SetTexture( "$basetexture", Material( replace.gmod_camera ):GetTexture( "$basetexture" ) ) Material( "vgui/gmod_tool" ):SetTexture( "$basetexture", Material( replace.gmod_tool ):GetTexture( "$basetexture" ) ) Material( "weapons/swep" ):SetTexture( "$basetexture", Material( replace.swep ):GetTexture( "$basetexture" ) ) else --If there is no variation for given convar value --Might want to revert back or something. --replace = mats[ 0 ]; --Material( "vgui/gmod_camera" ):SetTexture( "$basetexture", Material( replace.gmod_camera ):GetTexture( "$basetexture" ) ) --Material( "vgui/gmod_tool" ):SetTexture( "$basetexture", Material( replace.gmod_tool ):GetTexture( "$basetexture" ) ) --Material( "weapons/swep" ):SetTexture( "$basetexture", Material( replace.swep ):GetTexture( "$basetexture" ) ) end end Refresh() cvars.AddChangeCallback( 'cl_betterhudicons_alt', function( convarname, oldvalue, newvalue ) Refresh() end ) [/CODE] Note I haven't tested that and, and RobotBoy will probably think of a less 'shitty' way to do it. [editline]10th October 2015[/editline] [QUOTE=Robotboy655;48865727]That is a very shitty way of doing it, here's what I'd recommend: [CODE] local bettericons = CreateClientConVar( "cl_betterhudicons_alt", '0', true, false ) [/CODE][/QUOTE] By the way, RobotBoy, does creating a client convar ALWAYS return the convar even if it already exists?
[QUOTE=MPan1;48869303]It is, since your code won't update automatically with the convar being changed, just try using a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/cvars/AddChangeCallback]cvars.AddChangeCallback[/url] which should have the function of changing the icons depending on the value of your convar. For example: [CODE]Code[/CODE] Note I haven't tested that and, and RobotBoy will probably think of a less 'shitty' way to do it.[/QUOTE] Ha, I see. Anyway, it works perfectly, many thanks ! I'll mention you both in the credits of the addon. [DEL] I'll mark this as solved[/DEL] and I'll also make sure to learn some Lua for my future endeavours, I feel so stupid not being able to do these things... :hammered: [editline]a[/editline] Actually, I noticed a small problem. On map reload, the changes disappear. Any particular reason for this ? The convar value is well saved, but somehow the textures themselves go back to their original state. Should I package the two styles in two different addons instead ? I feel like I'm asking too much from Lua.
I haven't been able to fix the issue with the textures going back to their original state upon map reload over this week. Is there any way to fix the issue (probably re-writing the entire code, explaining why I can't find a fix), or should I separate the two styles into two different addons instead ? For reference, I am using this right now :[code]local bettericons = CreateClientConVar( "cl_betterhudicons_alt", '0', true, false ) local mats = { [ 0 ] = { -- When convar is 0 gmod_camera = "vgui/gmod_camera_original", gmod_tool = "vgui/gmod_tool_original", swep = "weapons/swep_original" }, [ 1 ] = { -- When convar is 1 gmod_camera = "vgui/gmod_camera_alt", gmod_tool = "vgui/gmod_tool_alt", swep = "weapons/swep_alt" }, } local function Refresh() local id = 0 if ( bettericons:GetBool() ) then id = 1 end --You can also use this for more than 2 variations: --local id = bettericons:GetInt() local replace = mats[ id ] if replace then --If a replacement is found Material( "vgui/gmod_camera" ):SetTexture( "$basetexture", Material( replace.gmod_camera ):GetTexture( "$basetexture" ) ) Material( "vgui/gmod_tool" ):SetTexture( "$basetexture", Material( replace.gmod_tool ):GetTexture( "$basetexture" ) ) Material( "weapons/swep" ):SetTexture( "$basetexture", Material( replace.swep ):GetTexture( "$basetexture" ) ) else --If there is no variation for given convar value --Might want to revert back or something. --replace = mats[ 0 ]; --Material( "vgui/gmod_camera" ):SetTexture( "$basetexture", Material( replace.gmod_camera ):GetTexture( "$basetexture" ) ) --Material( "vgui/gmod_tool" ):SetTexture( "$basetexture", Material( replace.gmod_tool ):GetTexture( "$basetexture" ) ) --Material( "weapons/swep" ):SetTexture( "$basetexture", Material( replace.swep ):GetTexture( "$basetexture" ) ) end end Refresh() cvars.AddChangeCallback( 'cl_betterhudicons_alt', function( convarname, oldvalue, newvalue) Refresh() end )[/code]
[QUOTE=Burnout6010;48922739]I haven't been able to fix the issue with the textures going back to their original state upon map reload over this week.[/QUOTE] With just a cursory look over your code, it looks like the problem is that the Refresh function is only called when the convar is changed. When the map changes, the textures will be reset to their default GMod textures and Refresh would have to be run again.
My bad, didn't think it'd reset and did a lazy thing - the Refresh() on line 43 should've been in a map change hook.
Thanks for the help ! I managed to fix the issue, here's the new line 43 : [code]hook.Add( "InitPostEntity", "Refresh", Refresh )[/code]I'll be closing the thread definitely, thanks again :happy: . [sp]Sorry for not doing so 2 days ago, restoring my PC to a working state took all the weekend[/sp]
Sorry, you need to Log In to post a reply to this thread.