Ricky's Example SENT

From GMod Wiki

Jump to: navigation, search
Lua: Example SENT
Page white text.png Description:Helps with the ropes of SENTs.
link=User:Ricky26 Original Author:Ricky26
Calendar.png Created:1st December 2006

Contents

Folder Structure

The files for the entity must go in garrysmod/lua/entities/entityname/

Our entity name will be prop_outline.

Client Side

cl_init.lua

 
include('shared.lua')
 
--[[---------------------------------------------------------
   Name: Draw
   Purpose: Draw the model in-game.
   Remember, the things you render first will be underneath!
---------------------------------------------------------]]
function ENT:Draw()
    -- self.BaseClass.Draw(self)  -- We want to override rendering, so don't call baseclass.
                                  -- Use this when you need to add to the rendering.
    self:DrawEntityOutline( 1.0 ) -- Draw an outline of 1 world unit.
    self:DrawModel()       -- Draw the model.
 
    AddWorldTip( self:EntIndex(), "BATHTUB TIME!", 0.5, self:GetPos(), self  ) -- Add an example tip.
end
 

This is how we draw the stuff. It's been commented nicely.

A good example of using the BaseClass.Draw line is when you want to just add a tooltip, or draw an additional sprite, like on the thrusters.

Server Side

init.lua

 
AddCSLuaFile( "cl_init.lua" ) -- Make sure clientside
AddCSLuaFile( "shared.lua" )  -- and shared scripts are sent.
 
include('shared.lua')
 
function ENT:Initialize()
 
	self:SetModel( "models/props_interiors/BathTub01a.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,
	self:SetMoveType( MOVETYPE_VPHYSICS )   -- after all, gmod is a physics
	self:SetSolid( SOLID_VPHYSICS )         -- Toolbox
 
        local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
end
 
function ENT:Use( activator, caller )
    return
end
 
function ENT:Think()
    -- We don't need to think, we are just a prop after all!
end
 

Most of the initialize function is standard physics setup, making sure that we are still a normal physics prop.

The Use function can be overridden to make the item do stuff. Next time: Jumping Bathtub Entity!

Think is for items that, well, think. The think function will get run on every frame, so the prop may do something special like moving around by its own. We actually don't need think for this SENT.

Shared

shared.lua

 
ENT.Type = "anim"
ENT.Base = "base_gmodentity"
 
ENT.PrintName		= "Glowball"
ENT.Author			= "Ricky26"
ENT.Contact			= "Don't"
ENT.Purpose			= "Exemplar material"
ENT.Instructions	= "Use with care. Always handle with gloves."
 

This stuff is all fairly basic information variables.

Shared functions are usually stored in here too, like working out the tooltip from networked variables, etc.

The Result

If you followed the example to the 't' or if you made corrections correctly you can type in the console: ent_create prop_outline

And get to see this:

Rickys Example SENT 1.jpg

Next Time on the Wiki: Jumping Bathtub!

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox