Revision Difference
Scripted_Entities#564942
<cat>Dev.Entities</cat>
**Scripted Entities** (or **SENT**s for short) are custom entities coded entirely in Lua.
They are usually defined in a separate `.lua` file in the `lua/entities/` folder.
The difference from **Engine Entities** being that you cannot use <page>ENTITY Hooks</page> on engine entities.
# Scripted Entity Types
There are 6 different types of Scripted Entities:
| ENT.Type | Description |
|
| nextbot | A NextBot NPC. A newer-type NPCs with better navigation.<br/><br/>All <page>NextBot</page> functions and hooks are also usable on these entities. |
| anim | A normal entity with visual and/or physical presence in the game world, such as props or whatever else you can imagine. |
| brush | A serverside only trigger entity. Mostly used very closely with the Hammer Level Editor. |
| point | A usually serverside only entity that doesn't have a visual or physical representation in the game world, such as logic entities. |
| filter | A different kind of "point" entity used in conjunction with trigger (`brush` type) entities. |
| ai | 2004 Source Engine NPC system entity |
# Scripting
The following is usable with **Scripted Entities**:
* <page>Structures/ENT</page>
* <page>ENTITY hooks</page>
* <page>Entity</page> functions
The following is usable with **Scripted Weapons** (Also known as **SWEP**s):
* <page>Structures/SWEP</page>
* <page>Entity</page> functions
* <page>Weapon</page> functions
* <page>WEAPON hooks</page>* <page>WEAPON hooks</page>⤶
⤶
⤶
The following example scripts are placed within `lua/entities`. If you would like a more in-depth tutorial on entity creation, <page text="click here">Entity_Creating_Custom_Entities</page>⤶
<example>⤶
<description>A plain single-file **SENT** script that runs on client and server. You can name the file `barrel.lua` or place it in a folder named `barrel` and name it `shared.lua`. Creates a barrel with physics that can be destroyed.</description>⤶
<code>⤶
AddCSLuaFile()⤶
⤶
ENT.Type = "anim"⤶
ENT.Base = "base_anim"⤶
⤶
⤶
-- The name that appears in the spawnmenu.⤶
ENT.PrintName = "Explosive Barrel"⤶
⤶
ENT.Information = "Shoot to explode."⤶
⤶
ENT.Author = "You!"⤶
⤶
⤶
ENT.Spawnable = true⤶
⤶
-- The category the entity is located in within the spawnmenu.⤶
ENT.Category = "Other"⤶
⤶
⤶
-- These are custom entity variables, feel free to change them!⤶
ENT.ExplosionDamage = 100⤶
ENT.ExplosionRadius = 250⤶
⤶
-- This custom variable prevents the entity from infinitely exploding.⤶
ENT.HasExploded = false⤶
⤶
-- This code runs whenever the entity is created.⤶
function ENT:Initialize()⤶
self:SetModel("models/props_c17/oildrum001_explosive.mdl")⤶
⤶
self:PhysicsInit(SOLID_VPHYSICS)⤶
self:SetMoveType(MOVETYPE_VPHYSICS)⤶
self:SetSolid(SOLID_VPHYSICS)⤶
⤶
local phys = self:GetPhysicsObject()⤶
⤶
-- This will make the entity fall instead of being stuck in the air when spawned.⤶
if ( IsValid(phys) ) then⤶
phys:Wake()⤶
end⤶
⤶
-- This is required to allow the entity to be gibbed.⤶
if ( SERVER ) then⤶
self:PrecacheGibs()⤶
end⤶
end⤶
⤶
-- Explode when damaged!⤶
function ENT:OnTakeDamage(damageInfo)⤶
if self.HasExploded then⤶
return -- Stop the code here if the entity already exploded.⤶
end⤶
⤶
local newEffectData = EffectData() -- Creates a new EffectData to use in util.Effect.⤶
newEffectData:SetOrigin(self:GetPos())⤶
newEffectData:SetMagnitude(100)⤶
newEffectData:SetScale(1)⤶
⤶
-- Make the explosion effect!⤶
util.Effect("Explosion",newEffectData)⤶
⤶
-- Makes the entity split apart. (The vector adds force to the gibs upwards)⤶
self:GibBreakServer(Vector(0,0,10))⤶
⤶
-- Setting this to true prevents the entity from exploding again.⤶
self.HasExploded = true⤶
⤶
self:Remove()⤶
end⤶
⤶
function ENT:OnRemove()⤶
if ( SERVER ) then⤶
-- Deal explosion damage.⤶
-- (The last two variables are custom and can be changed at the top)⤶
util.BlastDamage(self,self,self:GetPos(),self.ExplosionRadius,self.ExplosionDamage)⤶
end⤶
end⤶
</code>⤶
</example>⤶
⤶
<example>⤶
<description>A **SENT** made up of three scripts, make sure to put these within a folder named after your entity. Creates a barrel with physics that can be destroyed.</description>⤶
<code>⤶
-- cl_init.lua --⤶
⤶
include("shared.lua") -- This runs shared.lua on the client⤶
⤶
</code>⤶
⤶
<code>⤶
-- init.lua --⤶
⤶
-- Send shared and cl_init to the client⤶
AddCSLuaFile("shared.lua")⤶
AddCSLuaFile("cl_init.lua")⤶
⤶
include("shared.lua") -- This runs shared.lua on the server⤶
⤶
-- This code runs whenever the entity is created.⤶
function ENT:Initialize()⤶
self:SetModel("models/props_c17/oildrum001_explosive.mdl")⤶
⤶
self:PhysicsInit(SOLID_VPHYSICS)⤶
self:SetMoveType(MOVETYPE_VPHYSICS)⤶
self:SetSolid(SOLID_VPHYSICS)⤶
⤶
local phys = self:GetPhysicsObject()⤶
⤶
-- This will make the entity fall instead of being stuck in the air when spawned.⤶
if ( IsValid(phys) ) then⤶
phys:Wake()⤶
end⤶
⤶
-- This is required to allow the entity to be gibbed.⤶
self:PrecacheGibs()⤶
⤶
end⤶
⤶
-- Explode when damaged!⤶
function ENT:OnTakeDamage(damageInfo)⤶
if self.HasExploded then⤶
return -- Stop the code here if the entity already exploded.⤶
end⤶
⤶
local newEffectData = EffectData() -- Creates a new EffectData to use in util.Effect.⤶
newEffectData:SetOrigin(self:GetPos())⤶
newEffectData:SetMagnitude(100)⤶
newEffectData:SetScale(1)⤶
⤶
-- Make the explosion effect!⤶
util.Effect("Explosion",newEffectData)⤶
⤶
-- Makes the entity split apart. (The vector adds force to the gibs upwards)⤶
self:GibBreakServer(Vector(0,0,10))⤶
⤶
-- Setting this to true prevents the entity from exploding again.⤶
self.HasExploded = true⤶
⤶
self:Remove()⤶
end⤶
⤶
function ENT:OnRemove()⤶
-- Deal explosion damage.⤶
-- (The last two variables are custom and can be changed at the top)⤶
util.BlastDamage(self,self,self:GetPos(),self.ExplosionRadius,self.ExplosionDamage)⤶
end⤶
⤶
</code>⤶
⤶
<code>⤶
-- shared.lua --⤶
⤶
AddCSLuaFile()⤶
⤶
ENT.Type = "anim"⤶
ENT.Base = "base_anim"⤶
⤶
⤶
-- The name that appears in the spawnmenu.⤶
ENT.PrintName = "Explosive Barrel"⤶
⤶
ENT.Information = "Shoot to explode."⤶
⤶
ENT.Author = "You!"⤶
⤶
⤶
ENT.Spawnable = true⤶
⤶
-- The category the entity is located in within the spawnmenu.⤶
ENT.Category = "Other"⤶
⤶
⤶
-- These are custom entity variables, feel free to change them!⤶
ENT.ExplosionDamage = 100⤶
ENT.ExplosionRadius = 250⤶
⤶
-- This custom variable prevents the entity from infinitely exploding.⤶
ENT.HasExploded = false⤶
⤶
</code>⤶
⤶
</example>