Garry's Mod Wiki

game

The game library provides functions to access various features in the game's engine, most of it's functions are related to controlling the map.

Methods

game.AddAmmoType( table ammoData )
Adds a new ammo type to the game. You can find a list of default ammo types here. This function must be called on both the client and server in GM:Initialize or you will have unexpected problems. There is a limit of 256 ammo types, including the default ones.
game.AddDecal( string decalName, string materialName )
Registers a new decal. There's a rather low limit of around 256 for decal materials that may be registered and they are not cleared on map load.
game.AddParticles( string particleFileName )
Loads a particle file. Individual particle systems will still need to be precached with PrecacheParticleSystem. You will still need to call this function clientside regardless if you create the particle effects serverside.
This is used internally - although you're able to use it you probably shouldn't. Consider using game. GetAmmoTypes and game. GetAmmoData instead. Called by the engine to retrieve the ammo types.
game.CleanUpMap( boolean dontSendToClients = false, table extraFilters = {}, function callback = nil )
Removes most entities, and then respawns entities created by the map, as if the map was just loaded. There are certain exclusions, such as players or weapons held by players, soundscapes and others. EFL_KEEP_ON_RECREATE_ENTITIES can be set on entities to preserve them through a map cleanup. On the client it will remove decals, sounds, gibs, dead NPCs, and entities created via ents. CreateClientProp. This function is ran on all clients from server automatically, when it is called on the server. This function calls GM:PreCleanupMap before cleaning up the map and GM:PostCleanupMap after cleaning up the map. Beware of calling this function in hooks that may be called on map clean up (such as ENTITY:StartTouch) to avoid infinite loops. Calling this destroys all BASS streams. Issue Tracker: 2874This can crash when removing _firesmoke entities. You can use the example below to workaround this issue. Issue Tracker: 3637
game.ConsoleCommand( string stringCommand )
Runs a console command. Make sure to add a newline ("\n") at the end of the command. If you use data that were received from a client, you should avoid using this function because newline and semicolon (at least) allow the client to run arbitrary commands! For safety, you are urged to prefer using RunConsoleCommand in this case.
Returns the damage type of given ammo type.
Returns the AmmoData structure for given ID.
Returns the ammo bullet force that is applied when an entity is hit by a bullet of given ammo type.
number game.GetAmmoID( string name )
Returns the ammo type ID for given ammo type name. See game. GetAmmoName for reverse.
Returns the real maximum amount of ammo of given ammo ID, regardless of the setting of gmod_maxammo convar.
Returns the ammo name for given ammo type ID. See game. GetAmmoID for reverse.
Returns the damage given ammo type should do to NPCs.
Returns the damage given ammo type should do to players.
Returns a list of all ammo types currently registered.
Returns the counter of a Global State. See Global States for more information.
Returns whether a Global State is off, active or dead ( inactive ) See Global States for more information.
Returns the public IP address and port of the current server. This will return the IP/port that you are connecting through when ran clientside. Returns "loopback" in singleplayer. Returns "0. 0. 0. 0:port" on the server when called too early, including in GM:Initialize and GM:InitPostEntity. This bug seems to only happen the first time a server is launched, and will return the correct value after switching maps. Issue Tracker: 3001
string game.GetMap()
Returns the name of the current map, without a file extension. On the menu state, returns "menu". In Multiplayer this does not return the current map in the CLIENT realm before GM:Initialize.
Returns the next map that would be loaded according to the file that is set by the mapcyclefile convar.
Returns the revision (Not to be confused with VBSP Version) of the current map.
Returns the difficulty level of the game. TIP: You can use this function in your scripted NPCs or Nextbots to make them stronger, however, it is a good idea to lock powerful attacks behind the highest difficulty instead of just increasing the health. Internally this is tied to the gamerules entity, so you'll have to wait until GM:InitPostEntity is called to return the skill level
Returns the time scale set with game. SetTimeScale. If you want to get the value of host_timescale use local timescale = GetConVar( "host_timescale" ):GetFloat()
Returns the worldspawn entity.
Returns true if the server is a dedicated server, false if it is a listen server or a singleplayer game.
game.KickID( string id, string reason = "No reason given" )
Kicks a player from the server. This can be ran before the player has spawned.
game.LoadNextMap()
Loads the next map according to the nextlevel convar, or from the current mapcyclefile set by the respective convar.
Returns the map load type of the current map. After changing the map with the console command changelevel, "newgame" is returned. With changelevel2 (single player only), "transition" is returned.
Returns the maximum amount of players (including bots) that the server can have.
Mounts a GMA addon from the disk. Can be used with steamworks. DownloadUGC Any error models currently loaded that the mounted addon provides will be reloaded. Any error materials currently loaded that the mounted addon provides will NOT be reloaded. That means that this cannot be used to fix missing map materials, as the map materials are loaded before you are able to call this.
Removes all the clientside ragdolls.
game.SetGlobalCounter( string name, number count )
Sets the counter of a Global State. See Global States for more information.
game.SetGlobalState( string name, number state )
Sets whether a Global State is off, active or dead ( inactive ) See Global States for more information.
game.SetSkillLevel( number level )
Sets the difficulty level of the game, can be retrieved with game. GetSkillLevel. This will automatically change whenever the "skill" convar is modified serverside.
game.SetTimeScale( number timeScale )
Sets the time scale of the game. This function is supposed to remove the need of using the host_timescale convar, which is cheat protected. To slow down or speed up the movement of a specific player, use Player:SetLaggedMovementValue instead. Like host_timescale, this method does not affect sounds, if you wish to change that, look into GM:EntityEmitSound. The true timescale will be host_timescale multiplied by game. GetTimeScale
Returns whether the current session is a single player game.
Returns position the player should start from, this is not the same thing as spawn points, it is used to properly transit the player between maps.