The HUD
module provides functions and classes for creating and managing Heads-Up Display (HUD) elements in VScripts. It simplifies the process of displaying text, hints, and other UI elements on the screen, allowing developers to easily create custom HUD elements for their game modes and mods.
- HUD/init.nut
- HUD/ScreenText.nut
- HUD/HintInstructor.nut
This file initializes the HUD
module by declaring a global table ::HUD
which will store all HUD-related functions and classes.
This file provides the HUD.ScreenText
class for creating and managing on-screen text elements using the "game_text" entity.
The HUD.ScreenText
class represents an on-screen text element that can be displayed, hidden, and updated dynamically. It provides a convenient way to create and manage "game_text" entities without writing extensive code.
Constructor
Creates a new HUD.ScreenText
object with the specified position, message, hold time, and optional target name.
Parameters:
position
(Vector): The position of the text on the screen (in screen coordinates).message
(string): The text message to display.holdtime
(number, optional): The duration in seconds to display the text (default is 10).targetname
(string, optional): The target name of the "game_text" entity (default is an empty string).
Example:
local textPos = Vector(0.5, 0.5, 0) // Center of the screen
local myText = HUD.ScreenText(textPos, "Hello, world!", 5, "my_text_element") // Display for 5 seconds with a target name
Displays the on-screen text.
Parameters:
holdtime
(number, optional): The duration in seconds to display the text (default is 10).
Example:
myText.Enable() // Make the text visible
myText2.Enable(2) // Make the text visible for 2 seconds
Hides the on-screen text.
Example:
myText.Disable() // Hide the text
Updates and redisplays the on-screen text. This can be used to refresh the text if its properties have been changed.
Example:
myText.SetText("New message").Update() // Change the message and update the display
Changes the message of the text display.
Parameters:
message
(string): The new text message to display.
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetText("Updated message") // Change the text
Sets the channel of the text display. The channel determines the rendering order of the text (higher channels are drawn on top of lower channels).
Parameters:
value
(number): The channel to set.
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetChannel(3) // Set the channel to 3
Sets the primary color of the text display as a string.
Parameters:
string_color
(string): The color string in the format "R G B" (e.g., "255 0 0" for red).
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetColor("0 255 0") // Set the color to green
Sets the secondary color of the text display as a string. The secondary color is used for effects like outlines or shadows.
Parameters:
string_color
(string): The color string in the format "R G B" (e.g., "0 0 0" for black).
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetColor2("0 0 0") // Set the secondary color to black
Sets the effect of the text display.
Parameters:
value
(number): The index of the effect to set (refer to the "game_text" entity documentation for available effects).
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetEffect(1) // Set the effect to a specific index
Sets the fade-in time of the text display.
Parameters:
value
(number): The fade-in time in seconds.
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetFadeIn(1) // Fade in over 1 second
Sets the fade-out time of the text display.
Parameters:
value
(number): The fade-out time in seconds.
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetFadeOut(2) // Fade out over 2 seconds
Sets the hold time (duration) of the text display.
Parameters:
time
(number): The hold time in seconds.
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
myText.SetHoldTime(8) // Display for 8 seconds
Sets the position of the text display.
Parameters:
Vector
(Vector): The new position of the text on the screen (in screen coordinates).
Returns:
- (HUD.ScreenText): The
HUD.ScreenText
object itself, allowing for method chaining.
Example:
local newPos = Vector(0.25, 0.75, 0)
myText.SetPos(newPos) // Move the text to a new position
The methods of HUD.ScreenText
can be chained together using the builder pattern, allowing you to configure the text element in a concise and readable way. Each method returns the HUD.ScreenText
object itself, so you can call multiple methods in sequence.
Example:
HUD.ScreenText(Vector(0, 0.5, 0), "My Text", 999)
.SetChannel(1)
.SetEffect(2)
.SetFadeIn(0.01)
.SetColor("255 125 0")
.SetColor2("100 100 100")
.Enable() // "Build" and display the text
This file provides the HUD.HintInstructor
class for creating and managing hints using the "env_instructor_hint" entity.
The HUD.HintInstructor
class represents a hint element that can be displayed, hidden, and updated dynamically. It provides a convenient way to create and manage "env_instructor_hint" entities without writing extensive code.
Constructor
Creates a new HUD.HintInstructor
object with the specified message, hold time, icon, positioning, and optional target name.
Parameters:
message
(string): The hint message to display.holdtime
(number, optional): The duration in seconds to display the hint (default is 5).icon
(string, optional): The icon to display with the hint (default is "icon_tip").showOnHud
(number, optional): Whether to display the hint on the HUD (1) or at the target entity's position (0) (default is 1).targetname
(string, optional): The target name of the "env_instructor_hint" entity (default is an empty string).
Example:
local myHint = HUD.HintInstructor("Press E to interact", 3, "icon_interact", 1, "my_hint_element") // Display for 3 seconds with a custom icon and target name
Displays the hint.
Example:
myHint.Enable() // Show the hint
Hides the hint.
Example:
myHint.Disable() // Hide the hint
Updates and redisplays the hint. This can be used to refresh the hint if its properties have been changed.
Example:
myHint.SetText("New hint message").Update() // Change the message and update the display
Changes the message of the hint.
Parameters:
message
(string): The new hint message to display.
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetText("Updated hint message") // Change the text
Sets the bind to display with the hint icon and updates the icon to "use_binding".
Parameters:
bind
(string): The bind name to display (e.g., "+use").
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetBind("+use") // Display the "+use" bind with the icon
Sets the positioning of the hint (on HUD or at target entity).
Parameters:
value
(number): 1 to display the hint on the HUD, 0 to display it at the target entity's position.ent
(CBaseEntity, pcapEntity, or null, optional): The target entity to position the hint at (only used ifvalue
is 0).
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
local targetEntity = Entities.FindByName(null, "my_target_entity")
myHint.SetPositioning(0, targetEntity) // Display the hint at the target entity's position
Sets the color of the hint text as a string.
Parameters:
string_color
(string): The color string in the format "R, G, B" (e.g., "255, 0, 0" for red).
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetColor("0, 255, 0") // Set the color to green
Sets the icon to display when the hint is on-screen.
Parameters:
icon
(string): The icon name to display (e.g., "icon_tip").
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetIconOnScreen("icon_interact") // Set a custom icon
Sets the icon to display when the hint is off-screen.
Parameters:
screen
(string): The icon name to display (e.g., "icon_tip").
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetIconOffScreen("icon_arrow") // Set a custom off-screen icon
Sets the hold time (duration) of the hint.
Parameters:
time
(number): The hold time in seconds.
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetHoldTime(10) // Display for 10 seconds
Sets the distance at which the hint is visible.
Parameters:
value
(number): The distance in units.
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetDistance(500) // Make the hint visible within 500 units
Sets the visual effects for the hint.
Parameters:
sizePulsing
(number): The size pulsing option (0 for no pulsing, 1 for pulsing).alphaPulsing
(number): The alpha pulsing option (0 for no pulsing, 1 for pulsing).shaking
(number): The shaking option (0 for no shaking, 1 for shaking).
Returns:
- (HUD.HintInstructor): The
HUD.HintInstructor
object itself, allowing for method chaining.
Example:
myHint.SetEffects(1, 0, 1) // Enable size pulsing and shaking
Similar to HUD.ScreenText
, the methods of HUD.HintInstructor
can also be chained together using the builder pattern, providing a fluent way to configure hint elements.
Example:
local targetEntity = Entities.FindByName(null, "my_target")
HUD.HintInstructor("Interact with this object", 5, "icon_interact", 0, "my_hint")
.SetPositioning(0, targetEntity) // Display at the target entity's position
.SetColor("255, 255, 0") // Set the color to yellow
.SetDistance(200) // Make the hint visible within 200 units
.Enable() // "Build" and display the hint