Garry's Mod Wiki


This is the list of utility functions.


number util.AddNetworkString( string str )
Adds the specified string to a string table, which will cache it and network it to all clients automatically. Whenever you want to create a net message with net.Start, you must add the name of that message as a networked string via this function. If the passed string already exists, nothing will happen and the ID of the existing item will be returned. Each unique network name needs to be pooled once - do not put this function call into any other functions if you're using a constant string. Preferable plac..
Vector util.AimVector( Angle ViewAngles, number ViewFOV, number x, number y, number scrWidth, number scrHeight )
Function used to calculate aim vector from 2D screen position. It is used in SuperDOF calculate Distance. Essentially a generic version of gui.ScreenToVector.
string util.Base64Decode( string str )
Decodes the specified string from base64.
string util.Base64Encode( string str )
Encodes the specified string to base64.
util.BlastDamage( Entity inflictor, Entity attacker, Vector damageOrigin, number damageRadius, number damage )
Applies explosion damage to all entities in the specified radius.
util.BlastDamageInfo( CTakeDamageInfo dmg, Vector damageOrigin, number damageRadius )
Applies spherical damage based on damage info to all entities in the specified radius.
string util.Compress( string str )
Compresses the given string using the LZMA algorithm. Use with net.WriteData and net.ReadData for networking and util.Decompress to decompress the data.
string util.CRC( string stringToHash )
Generates the CRC Checksum of the specified string.
string util.DateStamp()
Returns the current date formatted like '2012-10-31 18-00-00'
util.Decal( string name, Vector start, Vector end, Entity filter = NULL )
Performs a trace and paints a decal to the surface hit.
util.DecalEx( IMaterial material, Entity ent, Vector position, Vector normal, table color, number w, number h )
Performs a trace and paints a decal to the surface hit. This function has trouble spanning across multiple brushes on the map.
string util.DecalMaterial( string decalName )
Gets the full material path by the decal name. Used with util.DecalEx.
string util.Decompress( string compressedString, number maxSize = nil )
Decompresses the given string using LZMA algorithm. Used to decompress strings previously compressed with util.Compress.
number, Vector, number util.DistanceToLine( Vector lineStart, Vector lineEnd, Vector pointPos )
Gets the distance between a line and a point in 3d space.
util.Effect( string effectName, CEffectData effectData, boolean allowOverride = true, any ignorePredictionOrRecipientFilter = nil )
Creates an effect with the specified data. You can find a list of built-in engine effects here. You can create your own. Example effects can be found here and here. When dispatching an effect from the server, some values may be clamped for networking optimizations. Visit the Set accessors on CEffectData to see which ones are affected.You will need to couple this function with IsFirstTimePredicted if you want to use it in predicted hook.
table util.GetModelInfo( string mdl )
Returns a table containing the info about the model. This function will silently fail if used on models with following strings in them: _shared _anims _gestures _anim _postures _gst _pst _shd _ss _anm _include
table util.GetModelMeshes( string model, number lod = 0, number bodygroupMask = 0 )
Returns a table of visual meshes of given model. This does not work on brush models (*number models) See also ENTITY:GetRenderMesh.
string util.GetPData( string steamID, string name, string default )
Gets PData of an offline player using their SteamID This function internally uses Player:UniqueID, which can cause collisions (two or more players sharing the same PData entry). It's recommended that you don't use it. See the related wiki page for more information.
pixelvis handle t util.GetPixelVisibleHandle()
Creates a new PixVis handle. See util.PixelVisible.
table util.GetPlayerTrace( Player ply, Vector dir = ply:GetAimVector() )
Utility function to quickly generate a trace table that starts at the players view position, and ends 32768 units along a specified direction.
table util.GetSunInfo()
Gets information about the sun position and obstruction or nil if there is no sun.
table util.GetSurfaceData( number id )
Returns data of a surface property at given ID.
number util.GetSurfaceIndex( string surfaceName )
Returns the matching surface property index for the given surface property name. See also util.GetSurfaceData and util.GetSurfacePropName for opposite function.
string util.GetSurfacePropName( number id )
Returns the name of a surface property at given ID. See also util.GetSurfaceData and util.GetSurfaceIndex for opposite function.
table util.GetUserGroups()
Returns a table of all SteamIDs that have a usergroup. This returns the original usergroups table, changes done to this table are not retroactive and will only affect newly connected users
Vector, Vector, number util.IntersectRayWithOBB( Vector rayStart, Vector rayDelta, Vector boxOrigin, Angle boxAngles, Vector boxMins, Vector boxMaxs )
Performs a "ray" box intersection and returns position, normal and the fraction.
Vector util.IntersectRayWithPlane( Vector rayOrigin, Vector rayDirection, Vector planePosition, Vector planeNormal )
Performs a ray-plane intersection and returns the hit position or nil.
boolean util.IsInWorld( Vector position )
Checks if a certain position is within the world bounds.
boolean util.IsModelLoaded( string modelName )
Checks if the model is loaded in the game.
boolean util.IsSkyboxVisibleFromPoint( Vector position )
Check whether the skybox is visibile from the point specified. This will always return true in fullbright maps
boolean util.IsValidModel( string modelName )
Checks if the specified model is valid. A model is considered invalid in following cases: Starts with a space or maps Doesn't start with models Contains any of the following: _gestures _animations _postures _gst _pst _shd _ss _anm .bsp cs_fix On server: If the model isn't precached, if the model file doesn't exist on the disk If precache failed Model is the error model
boolean util.IsValidPhysicsObject( Entity ent, number physobj )
Checks if given numbered physics object of given entity is valid or not. Most useful for ragdolls.
boolean util.IsValidProp( string modelName )
Checks if the specified prop is valid.
boolean util.IsValidRagdoll( string ragdollName )
Checks if the specified model name points to a valid ragdoll.
table util.JSONToTable( string json )
Converts a JSON string to a Lua table. This function converts keys to numbers whenever possible. It also has a limit of 15000 keys total.This will attempt cast the string keys "inf", "nan", "true", and "false" to their respective Lua values. This completely ignores nulls in arrays.Colors will not have the color metatable.
table util.KeyValuesToTable( string keyValues, boolean usesEscapeSequences = false, boolean preserveKeyCase = false )
Converts a KeyValue string to a Lua table. Table keys will not repeat, see util.KeyValuesToTablePreserveOrder.
table util.KeyValuesToTablePreserveOrder( string keyvals, boolean usesEscapeSequences = false, boolean preserveKeyCase = false )
Similar to util.KeyValuesToTable but it also preserves order of keys.
Vector util.LocalToWorld( Entity ent, Vector lpos, number bonenum )
Returns a vector in world coordinates based on an entity and local coordinates
string util.NetworkIDToString( number stringTableID )
Returns the networked string associated with the given ID from the string table.
number util.NetworkStringToID( string networkString )
Returns the networked ID associated with the given string from the string table.
string util.NiceFloat( number float )
Formats a float by stripping off extra 0's and .'s
util.ParticleTracer( string name, Vector startPos, Vector endPos, boolean doWhiz )
Creates a tracer effect with the given parameters.
util.ParticleTracerEx( string name, Vector startPos, Vector endPos, boolean doWhiz, number entityIndex, number attachmentIndex )
Creates a tracer effect with the given parameters.
number util.PixelVisible( Vector position, number radius, pixelvis handle t PixVis )
Returns the visibility of a sphere in the world.
number util.PointContents( Vector position )
Returns the contents of the position specified.
util.PrecacheModel( string modelName )
Precaches a model for later use. Model is cached after being loaded once. Modelprecache is limited to 4096 unique models. When it reaches the limit the game will crash.
util.PrecacheSound( string soundName )
Precaches a sound for later use. Sound is cached after being loaded once. Soundcache is limited to 16384 unique sounds.Positively broken on purpose because it fills stringtablesUltimately does nothing on client, and only works with sound scripts, not direct paths
table util.QuickTrace( Vector origin, Vector endpos, Entity filter = nil )
Performs a trace with the given origin, direction and filter.
string util.RelativePathToFull( string file )
Returns the absolute system path the file relative to /garrysmod/.
util.RemovePData( string steamID, string name )
Removes PData of offline player using his SteamID. This function internally uses Player:UniqueID, which can cause collisions (two or more players sharing the same PData entry). It's recommended that you don't use it. See the related wiki page for more information.
util.ScreenShake( Vector pos, number amplitude, number frequency, number duration, number radius )
Makes the screen shake
util.SetPData( string steamID, string name, any value )
Sets PData for offline player using his SteamID This function internally uses Player:UniqueID, which can cause collisions (two or more players sharing the same PData entry). It's recommended that you don't use it. See the related wiki page for more information.
number util.SharedRandom( string uniqueName, number min, number max, number additionalSeed = 0 )
Generates a random float value that should be the same on client and server. This function is best used in a Predicted Hook
Entity util.SpriteTrail( Entity ent, number attachmentID, table color, boolean additive, number startWidth, number endWidth, number lifetime, number textureRes, string texture )
Adds a trail to the specified entity.
Stack util.Stack()
Returns a new Stack object
string util.SteamIDFrom64( string id )
Given a 64bit SteamID will return a STEAM_0: style Steam ID
string util.SteamIDTo64( string id )
Given a STEAM_0 style Steam ID will return a 64bit Steam ID
any util.StringToType( string str, string typename )
Converts a string to the specified type. This can be useful when dealing with ConVars.
string util.TableToJSON( table table, boolean prettyPrint = false )
Converts a table to a JSON string. All integers will be converted to decimals (5 -> 5.0).All keys are strings in the JSON format, so all keys will be converted to strings!This will produce invalid JSON if the provided table contains nan or inf values.Issue Tracker: 3561
string util.TableToKeyValues( table table, string parentKey = "TableToKeyValues" )
Converts the given table into a key value string.
table util.Timer( number startdelay = 0 )
Creates a timer object.
number util.TimerCycle()
Returns the time since this function has been last called
boolean util.tobool( any input )
We advise against using this. It may be changed or removed in a future update. You should use tobool instead. Converts string or a number to a bool, if possible. Alias of tobool.
table util.TraceEntity( table tracedata, Entity ent )
Runs a trace using the entity's collisionmodel between two points. This does not take the entity's angles into account and will trace its unrotated collisionmodel. Clientside entities will not be hit by traces.
table util.TraceEntityHull( Entity ent1, Entity ent2 )
We advise against using this. It may be changed or removed in a future update. This function is broken and returns the same values all the time Traces from one entity to another.
table util.TraceHull( table TraceData )
Performs an AABB hull (axis-aligned bounding box, aka not rotated) trace with the given trace data. Clientside entities will not be hit by traces.This function may not always give desired results clientside due to certain physics mechanisms not existing on the client. Use it serverside for accurate results.
table util.TraceLine( table TraceData )
Performs a trace with the given trace data. Clientside entities will not be hit by traces.
string util.TypeToString( any input )
Converts a type to a (nice, but still parsable) string
boolean util.worldpicker.Active()
Returns if the user is currently picking an entity.
util.worldpicker.Finish( table tr )
This is used internally - although you're able to use it you probably shouldn't. Finishes the world picking. This is called when a user presses their mouse after calling util.worldpicker.Start.
util.worldpicker.Start( function callback )
Starts picking an entity in the world. This will suppress the next mouse click, and instead use it as a direction in the trace sent to the callback.

Page Links

Special Pages



Render Time: 34ms

DB GetPage 5
Generate Html 4
SaveChanges (1) 9
Render Body 0
Render Sidebar 12