Common Code Snippets Entities

From GMod Wiki

Revision as of 19:02, 10 October 2011 by TheFreeman193 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page contains code snippets for handling entities.

Contents

Picking up a Weapon on use

This snippet makes it so that you can only pick up a weapon when holding the use key and looking directly at the weapon within a certain distance. It also has a pickup interval to make sure you don't pick up a whole pile of weapons all at once.

 
hook.Add( "PlayerSpawn", "PickupTimeout", function( ply )
	ply.PickupTimeout = CurTime() + 0.5
end )
function GM:PlayerCanPickupWeapon( ply, entity )
	if ( ply.PickupTimeout or 0 ) > CurTime() then return true end
	if ( ply.NextPickup or 0 ) > CurTime() then return false end
 
	if ply:KeyDown( IN_USE ) then
		local tr = ply:GetEyeTrace()
		if ValidEntity( tr.Entity ) and tr.Entity:GetPos():Distance( ply:GetShootPos() ) < 92 and tr.Entity == entity then
			ply.NextPickup = CurTime() + 0.5
			return true
		end
	end
	return false
end
 

Dropping a Weapon

Dropping a weapon, along with a table of weapons you may not drop.

function DropWeapon(ply, cmd, args)
	local undroppableWeapons = { "weapon_physcannon", "weapon_physgun", "gmod_camera", "gmod_tool" }
	local weapon = ply:GetActiveWeapon()
	if table.HasValue(undroppableWeapons, weapon:GetClass()) then return false end
	ply:DropWeapon(weapon)
end
concommand.Add("DropCurrentWeapon", DropWeapon)

Looping through Entities by Class using Wildcards

 
for k,v in pairs(ents.FindByClass("npc_*")) do //access every npc
    Msg(v:GetClass())
end
 
for k,v in pairs(ents.FindByClass("prop_phys*")) do //access every type of physics prop (ie. prop_physics, prop_physics_override, etc.)
    Msg(v:GetClass())
end 
 

Returning the Entity the Player is Looking At

On a clientside script:

 
local ent = LocalPlayer():GetEyeTrace().Entity
 
if (ent:IsValid()) then
	//yup, its a valid entity!
end
 

Returns the entity the specified player is looking at, if the entity is not valid it will return false

 
function TraceEntity(ply)
	local ent = ply:GetEyeTrace().Entity
	if ValidEntity(ent) then
		return ent
	end
	return false
end
 

Finding an Entity

How to find an entity and store it for later use. NOTE: You have to call this function with an entity class you want. If there is more than one entity it will pick a random one. This snippet finds all npc's (using wildcard * )

 
FindTarget("npc_*")
 
function FindTarget( target )
     local targets = ents.FindByClass( target )
     if (#targets > 1) then 
          local randT = math.Rand( 1, #targets)
          return randT 
     else
          return targets 
     end
end
 

Spawning an Entity

local ent = ents.Create( "npc_alyx" )
if ( !ent:IsValid() ) then return end
 
ent:SetPos( tr.HitPos )
ent:Spawn()
ent:Activate()

Spawning an Effect

util.Effect Takes the name of the effect and an effectdata type. The effectdata is accessible on the client when the effect is being rendered. So you only need to set the elements that the effect uses.


local effectdata = EffectData()
effectdata:SetOrigin( Vector(0,0,0) )
effectdata:SetNormal( Vector(0,0,1) )
effectdata:SetScale( 50 )
util.Effect( "effect_name", effectdata )

Adding Entities to the Undo List

 
undo.Create("This is my Entity Name") 
  undo.AddEntity( Ent1 ) 
  undo.AddEntity( Ent2 )
  undo.SetPlayer( Ply ) 
undo.Finish()
 

Deleting entities every 5 minutes

timer.Create( "removal", 300, 0, function()
	for k,v in ipairs(ents.FindByClass("class C_ClientRagdoll")) do
		v:Remove()
	end
end)
-- This will delete clientside ragdolls every 5 minutes when run on the client, but other classes and timings can be used.
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox