How to add new HUD elements

ArtfunkelArtfunkel Join Date: 2013-05-10 Member: 185184Members
This took me hours to work out, so I'm posting it.

Your first thought when trying to add new HUD elements would be to override a function in the relevant GUI class, but this fails because the classes themselves don't exist until the player spawns (<3 loosely-typed languages) but your scripts run when the map starts. The best solution I've found is to hook into GUIManager:CreateGUIScript instead and check the name of each script which is created.

Here is a function which adds a new text item to the marine HUD:
local base_CreateGUIScript = GUIManager.CreateGUIScript

function GUIManager:CreateGUIScript(scriptName)
	script = base_CreateGUIScript(self,scriptName)
	
	if (scriptName == "Hud/Marine/GUIMarineHUD") then
		script.testString = script:CreateAnimatedTextItem()
		script.testString:SetFontName(GUIMarineHUD.kTextFontName)
		script.testString:SetLayer(kGUILayerPlayerHUDForeground1)
		script.testString:SetColor(Color(1,1,1,1))
		script.testString:SetText("TEST TEXT")
		script.testString:SetPosition(Vector(100, 400, 0))
		
		script.background:AddChild(script.testString)
	end
	
	return script
end
Make sure this is only running on the client, obviously!

Comments

  • MendaspMendasp I touch maps in inappropriate places Valencia, Spain Join Date: 2002-07-05 Member: 884Members, NS1 Playtester, Contributor, Constellation, NS2 Playtester, Squad Five Gold, NS2 Map Tester, Reinforced - Shadow, WC 2013 - Shadow, Retired Community Developer
    I was actually thinking of looking into this recently (hooking into the GUIScript creation) so I could do my HUD modifications and make them as forward compatible as possible, as I was "overwriting" files until now, but you saved me the headache of researching it myself :D

    Thanks!
  • McGlaspieMcGlaspie www.team156.com Join Date: 2010-07-26 Member: 73044Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Reinforced - Onos, WC 2013 - Gold, Subnautica Playtester
    In addition to this, take a look at ClientUI.lua

    It contains all of the UI elements specific to a given class (I.e. what the player "is"). Once you have created your GUI script/element, then add it in the appropriate place in ClientUI.lua so it will be auto-loaded.
Sign In or Register to comment.