cityrp.util
Various helper functions ported from HL2RP, seperate for now due to potential conflict.
Functions
cityrp.util.DimColor(color, multiplier, alpha)
View source »Returns a dimmed version of the given color by the given scale.
Parameters
-
color
Color
Color to dim
-
multiplier
number
What to multiply the red, green, and blue values by
-
alpha
number
default: 255
Alpha to use in dimmed color
Returns
-
color
Dimmed color
Example Usage
print(cityrp.util.DimColor(Color(100, 100, 100, 255), 0.5))
> 50 50 50 255
cityrp.util.DrawBlur(panel, amount, passes, alpha)
View source »Blurs the content underneath the given panel. This will fall back to a simple darkened rectangle if the player has blurring disabled.
Parameters
-
panel
panel
Panel to draw the blur for
-
amount
number
default: 5
Intensity of the blur. This should be kept between 0 and 10 for performance reasons
-
passes
number
default: 0.2
Quality of the blur. This should be kept as default
-
alpha
number
default: 255
Opacity of the blur
Example Usage
function PANEL:Paint(width, height)
cityrp.util.DrawBlur(self)
end
cityrp.util.DrawBlurAt(x, y, width, height, amount, passes, alpha)
View source »Draws a blurred rectangle with the given position and bounds. This shouldn't be used for panels, see cityrp.util.DrawBlur instead.
Parameters
-
x
number
X-position of the rectangle
-
y
number
Y-position of the rectangle
-
width
number
Width of the rectangle
-
height
number
Height of the rectangle
-
amount
number
default: 5
Intensity of the blur. This should be kept between 0 and 10 for performance reasons
-
passes
number
default: 0.2
Quality of the blur. This should be kept as default
-
alpha
number
default: 255
Opacity of the blur
Example Usage
hook.Add("HUDPaint", "MyHUDPaint", function()
cityrp.util.DrawBlurAt(0, 0, ScrW(), ScrH())
end)
cityrp.util.DrawText(text, x, y, color, alignX, alignY, font, alpha)
View source »Draws some text with a shadow.
Parameters
-
text
string
Text to draw
-
x
number
X-position of the text
-
y
number
Y-position of the text
-
color
Color
Color of the text to draw
-
alignX
number
default: TEXT_ALIGN_LEFT
Horizontal alignment of the text, using one of the
TEXT_ALIGN_*
constants
-
alignY
number
default: TEXT_ALIGN_LEFT
Vertical alignment of the text, using one of the
TEXT_ALIGN_*
constants
-
font
string
default: "ixGenericFont"
Font to use for the text
-
alpha
number
default: color.a * 0.575
Alpha of the shadow
cityrp.util.EmitQueuedSounds(sounds, delay, spacing, volume, pitch)
View source »Emits sounds one after the other from an entity.
Parameters
-
sounds
tab
Sound paths to play
-
delay
number
[opt=0] How long to wait before starting to play the sounds
-
spacing
number
[opt=0.1] How long to wait between playing each sound
-
volume
number
[opt=75] The sound level of each sound
-
pitch
number
[opt=100] Pitch percentage of each sound
Returns
-
number
How long the entire sequence of sounds will take to play
cityrp.util.ExpandCamelCase(input, bNoUpperFirst)
View source »Returns a string that is the given input with spaces in between each CamelCase word. This function will ignore any words
that do not begin with a capital letter. The words ooc
, looc
, afk
, and url
will be automatically transformed
into uppercase text. This will not capitalize non-ASCII letters due to limitations with Lua's pattern matching.
Parameters
-
input
string
String to expand
-
bNoUpperFirst
bool
default: false
Whether or not to avoid capitalizing the first character. This is useful for lowerCamelCase
Returns
-
string
Expanded CamelCase string
Example Usage
print(cityrp.util.ExpandCamelCase("HelloWorld"))
> Hello World
cityrp.util.FindPlayer(identifier, bAllowPatterns)
View source »Attempts to find a player by matching their name or Steam ID.
Parameters
-
identifier
string
Search query
-
bAllowPatterns
bool
default: false
Whether or not to accept Lua patterns in
identifier
Returns
-
player
Player that matches the given search query - this will be
nil
if a player could not be found
cityrp.util.FormatStringNamed(format, Arguments)
View source »Returns a string that has the named arguments in the format string replaced with the given arguments.
Parameters
-
format
string
Format string
-
Arguments
tab or ...
to pass to the formatted string. If passed a table, it will use that table as the lookup table for the named arguments. If passed multiple arguments, it will replace the arguments in the string in order.
Example Usage
print(cityrp.util.FormatStringNamed("Hi, my name is {name}.", {name = "Bobby"}))
> Hi, my name is Bobby.
print(cityrp.util.FormatStringNamed("Hi, my name is {name}.", "Bobby"))
> Hi, my name is Bobby.
cityrp.util.GetMaterial(materialPath)
View source »Returns a cached copy of the given material, or creates and caches one if it doesn't exist. This is a quick helper function
if you aren't locally storing a Material()
call.
Parameters
-
materialPath
string
Path to the material
Returns
-
material
The cached material
-
nil
If the material doesn't exist in the filesystem
cityrp.util.GetStringTime(text)
View source »
Gets the amount of seconds from a given formatted string. If no time units are specified, it is assumed minutes. The valid values are as follows:
s
- Secondsm
- Minutesh
- Hoursd
- Daysw
- Weeksmo
- Monthsy
- Years
Parameters
-
text
string
Text to interpret a length of time from
Returns
-
number
Amount of seconds from the length interpreted from the given string
-
0
If the given string does not have a valid time
Example Usage
print(cityrp.util.GetStringTime("5y2d7w"))
> 162086400 -- 5 years, 2 days, 7 weeks
cityrp.util.GetTypeFromValue(value)
View source »Returns the cityrp.type
of the given value.
Parameters
-
value
Value to get the type of
Returns
-
ixtype
Type of value
Example Usage
print(cityrp.util.GetTypeFromValue("hello"))
> 2 -- i.e the value of cityrp.type.string
See Also
cityrp.util.GetUTCTime()
View source »Gets the current time in the UTC time-zone.
Returns
-
number
Current time in UTC
cityrp.util.Include(fileName, realm)
View source »Includes a lua file based on the prefix of the file. This will automatically call include
and AddCSLuaFile
based on the
current realm. This function should always be called shared to ensure that the client will receive the file from the server.
Parameters
-
fileName
string
Path of the Lua file to include. The path is relative to the file that is currently running this function
-
realm
string
optional
Realm that this file should be included in. You should usually ignore this since it will be automatically be chosen based on the
SERVER
andCLIENT
globals. This value should either be"server"
or"client"
if it is filled in manually
cityrp.util.IncludeDir(directory, bFromLua)
View source »Includes multiple files in a directory.
Parameters
-
directory
string
Directory to include files from
-
bFromLua
bool
optional
Whether or not to search from the base
lua/
folder, instead of contextually basing fromschema/
orgamemode/
See Also
cityrp.util.IsColor(input)
View source »Returns true
if the given input is a color table. This is necessary since the engine IsColor function only checks for
color metatables - which are not used for regular Lua color types.
Parameters
-
input
Input to check
Returns
-
bool
Whether or not the input is a color
cityrp.util.NumberToHex(num)
View source »Converts a number to a hexadecimal representation as a string.
Parameters
-
num
number
Number to convert
Returns
-
string
Hexadecimal representation of the number
cityrp.util.PushBlur(drawFunc)
View source »Pushes a 3D2D blur to be rendered in the world. The draw function will be called next frame in the
PostDrawOpaqueRenderables
hook.
Parameters
-
drawFunc
func
Function to call when it needs to be drawn
cityrp.util.ResetStencilValues()
View source »Resets all stencil values to known good (i.e defaults)
cityrp.util.SanitizeType(input)
View source »Sanitizes an input value with the given type. This function ensures that a valid type is always returned. If a valid value could not be found, it will return the default value for the type. This only works for simple types - e.g it does not work for player, character, or Steam ID types.
Parameters
-
input
Value to sanitize
Returns
-
any
Sanitized value
Example Usage
print(cityrp.util.SanitizeType(cityrp.type.number, "123"))
> 123
print(cityrp.util.SanitizeType(cityrp.type.bool, 1))
> true
See Also
cityrp.util.StaffOnline(bAfk)
View source »Checks if a staff member is online
Parameters
-
bAfk
Returns
-
bool
Staff online
cityrp.util.StringMatches(a, b)
View source »Checks to see if two strings are equivalent using a fuzzy manner. Both strings will be lowered, and will return true
if
the strings are identical, or if b
is a substring of a
.
Parameters
-
a
string
First string to check
-
b
string
Second string to check
Returns
-
bool
Whether or not the strings are equivalent
cityrp.util.StripRealmPrefix(name)
View source »Removes the realm prefix from a file name. The returned string will be unchanged if there is no prefix found.
Parameters
-
name
string
String to strip prefix from
Returns
-
string
String stripped of prefix
Example Usage
print(cityrp.util.StripRealmPrefix("sv_init.lua"))
> init.lua
cityrp.util.WrapText(text, maxWidth, font)
View source »Wraps text so it does not pass a certain width. This function will try and break lines between words if it can, otherwise it will break a word if it's too long.
Parameters
-
text
string
Text to wrap
-
maxWidth
number
Maximum allowed width in pixels
-
font
string
default: "cityrp_chatBox_MainText"
Font to use for the text
table.FullCopy(tab)
View source »Returns a copy of the table. I don't know the benefits of this, it's just been used in many different sweps so I'm moving it here and deleting those
Parameters
-
tab
tab
Table to copy
Returns
-
table
Copy of the table
table.QuickCopy(tab)
View source »Returns a quick copy of the table. This won't do stuff like vectors or angles, it's just quick for basic tables
Parameters
-
tab
tab
Table to copy
Returns
-
table
Copy of the table