Return to Main Page

First Scripted Entity

Page white text.png Description:Article about SENT.
link=User:Dim1xs Author:Dim1xs
Calendar.png Created:September 28th 2023

This article will learn you, how to code your first Scripted Entity - SENT

Scripted Weapons

ENT or SENT (from the word Scripted Entity), is a lua entity that can perform various functions. In this tutorial, we will write code for SENT that will change color every 5 seconds.

result.gif

The Basic Layout

By going to the 'entities' subdirectory from the 'lua' directory ('hl2/custom/hl2gmed/lua/entities'),
you can find an entity (folder) named 'prop_example'.

You can create a copy of the folder and name it your class, or continue working in the same folder.

Files

In entity folder you can find 3 files:

🔵init.lua

🔵cl_init.lua

🔵shared.lua

init.lua is run Serverside functions.

cl_init.lua is run Clientside functions.

shared.lua is run in both environments (server/clientside).

If you want to start from clear project, then open shared.lua script, and remove everything from functions:
function  ENT:Initialize() 
end

function  ENT:StartTouch( pEntity )
end

function  ENT:Touch( pEntity )
end

function  ENT:EndTouch( pEntity )
end

function  ENT:VPhysicsUpdate( pPhysics )
end
               

Initialization

Okay, so, now we need fill in the Initialization so that our entity at least represents something.

Initializaition is NewerShared.pngShared function, so you can call any function from it.
function ENT:Initialize()
   if ( not CLIENT ) then
      local pModel = "models/props_junk/wood_crate001a.mdl"--Box model. 
      self.SetAllowPrecache(true)
      self.PrecacheModel(pModel)
      self:SetModel(pModel)
      self.pCooldown = 5 --Set cooldown for next color change (in seconds). 
      self.NextColorChange = gpGlobals.curtime() + self.pCooldown --Cooldown for next time change. 
      self:VPhysicsInitNormal( 6, 0, false ); --Turn Entity into CPhysicsProp. 
   end
end
               

ChangeColor

So, we need create our new unction, and code for it execution.

Move on to init.lua script.

Now let's create color change function:

include( "shared.lua" )

function ENT:Think()
end

function ENT:ChangeColor() --Here, color change function. 
end
               
Okay, good, now code color change:
local red = random.RandomFloat(1,255) --Always random color. 
local green = random.RandomFloat(1,255) --Always random color. 
local blue = random.RandomFloat(1,255) --Always random color. 
self:SetRenderColor(red,green,blue,255)--Final color change. 
               
And put this stuff into the function:

function ENT:ChangeColor() --Here, color change function. 
   local red = random.RandomFloat(1,255) --Always random color. 
   local green = random.RandomFloat(1,255) --Always random color. 
   local blue = random.RandomFloat(1,255) --Always random color. 
   self:SetRenderColor(red,green,blue,255)--Final color change. 
end
               

Think

Last one part.

So, now we need write code for executing our ColorChange function, move to Think function.

We need to calculate our cooldown with current time:
function ENT:Think()
   if self.NextColorChange < gpGlobals.curtime() then
         self.NextColorChange = gpGlobals.curtime() + self.pCooldown
         self:ChangeColor()
   end
end
            
Now run the game and check it!

Result:


result.gif

Thank you for reading this tutorial, see ya later!

JOIN HL2GMED DISCORD SERVER!