LUA:RD2EntityProduction

From GMod Wiki

Jump to: navigation, search
Lua: Resource Distrubtion Production Entity Tutorial
Page white text.png Description:How to create resource distrubtion entity's.
link=User:KillerLUA Original Author:KillerLUA
Calendar.png Created:March 12, 2010

Contents


Introduction

In this tutorial, I will be showing you how to create a resource distrubtion entity that produces Coolant.

I haven't done a tutorial for a few days, I've been very busy doing coursework.

Setting up files

Gamemode

Create three new files called shared.lua, cl_init.lua and init.lua inside YourGamemode/entites/entites/rd2_coolantgen

Your done, now move onto Coding.

Addon

Create three new files called shared.lua, cl_init.lua and init.lua inside YourAddon/lua/entites/rd2_coolantgen

Your done, now move onto Coding.

Coding

Let's get cl_init over with, as it's only one line.

In your cl_init.lua file, put:

 
include('shared.lua') --Include to share it's functions and stuff like that
 

Now, we're going to work on shared.lua. We need to add the spawnable variables.

Shared.lua:

 
ENT.Base 		= "base_gmodentity"
 
ENT.PrintName	= "RD2 Production Entity"
ENT.Author		= "KillerLUA"
ENT.Contact		= "Steam: Deathtime38" --Contact
ENT.Spawnable		= true --Is it spawnable?
ENT.AdminSpawnable	= false --Is it an admin spawnable?
ENT.Active			= false --Don't be turned on when spawned
ENT.NTog			= 0
 

Now, we've got the client file. And the shared file, we've also made the entity spawnable by all.

As long as you don't have a spawn ent hook that disallows non-admin spawning.

Now to init.lua, first we need the function when the entity is initilized.

 
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "apc_engine_start" )
util.PrecacheSound( "apc_engine_stop" )
 
include('shared.lua')
 
local Ground = 1 + 0 + 2 + 8 + 32
 
function ENT:Initialize()
	self:SetModel( "models//props_wasteland/laundry_dryer002.mdl" ) --Set the model
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
 
	local phys = self:GetPhysicsObject()
	self.NextThink = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
	end
 
	self.val1 = 0 --Will store the energy
self.val2 = 0 --Store the coolant
	LS_RegisterEnt(self) --Register the entity
	RD_AddResource(self, "energy", 0) --Add the energy resource
	RD_AddResource(self, "coolant", 0)
end
 

That basicly adds the input for the energy resource, as our entity requires it to produce coolant. You can remove it's requirement for energy, it will still make coolant but will be more of a cheat entity.

Now, we move on to the spawn function...

 
function ENT:SpawnFunction( ply, tr )
 
	if ( !tr.Hit ) then return end
 
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
 
	local ent = ents.Create( self.ClassName )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end
 

Now, we need a toggle function to turn it on and off. Also included is the remove all links and turn off function:

 
function ENT:Use()
	if (CurTime() >= self.NTog) then
		if (self.Active == true) then
			self:StopSound( "apc_engine_start" )
			self:EmitSound( "apc_engine_stop" )
			self.Active = false
		else
			self:EmitSound( "apc_engine_start" )
			self.Active = true
		end
		self.NTog = CurTime() + 1
	end
end
 
--Remove all links to it
--Turn off all sounds (also emit the stop sounds)
function ENT:OnRemove()
	Dev_Unlink_All(self.conduit)
	if ( self.Active == true ) then
		self:StopSound( "apc_engine_start" )
		self:EmitSound( "apc_engine_stop" )
		self.Active = false
	end
end
 

Now for the really complicated function, the think function. This is how the entity thinks, this checks if we still have energy. If we do, then produce coolant, and so on.

 
function ENT:Think()
	self.val1 = RD_GetResourceAmount(self, "energy")
	self.val2 = RD_GetResourceAmount(self, "coolant")
	local ASt = ""
	if (self.Active == true) then
		ASt = "On"
	else
		ASt = "Off"
	end
 
	self:SetOverlayText( "RD2 Coolant Generator\n".. ASt .. "\nEnergy: " .. self.val1 .. "\nCoolant: " .. self.val2 )
 
	if (self.Active == true) then
		if (self.val1 > 0) then
			if (self.val1 > 0) then	
				if (self.val1 >= 100) then
					RD_ConsumeResource(self, "energy", 40)
					RD_SupplyResource(self, "coolant", 35)
				else
					RD_ConsumeResource(self, "energy", self.val1)
					local prod = math.Round(self.val1 - 5)
					RD_SupplyResource(self, "coolant", prod)
				end
			end
		else
			self:StopSound( "apc_engine_start" )
			self:EmitSound( "apc_engine_stop" )
			self.Active = false
		end
	end
 
end
 

Allows consuming of any amount of energy and turning it into (energy - 5) coolant.

And now finally, for the duplicator functions

 
function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self)
end
 
function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
end
 

And that's all. Your entity, will update the overlay every half of a seccond. You should be able to spawn the entity from the spawn menu!

For the sake of the copy and pasters, here is the whole code for init.lua:

Final Code

Shared.lua

 
ENT.Base 		= "base_gmodentity"
 
ENT.PrintName	= "RD2 Production Entity"
ENT.Author		= "KillerLUA"
ENT.Contact		= "Steam: Deathtime38" --Contact
ENT.Spawnable		= true --Is it spawnable?
ENT.AdminSpawnable	= false --Is it an admin spawnable?
ENT.Active			= false --Don't be turned on when spawned
ENT.NTog			= 0
 

CL_Init.lua

 
include('shared.lua') --Include to share it's functions and stuff like that
 

Init.lua

 
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "apc_engine_start" )
util.PrecacheSound( "apc_engine_stop" )
 
include('shared.lua')
 
local Ground = 1 + 0 + 2 + 8 + 32
 
function ENT:Initialize()
	self:SetModel( "models//props_wasteland/laundry_dryer002.mdl" ) --Set the model
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
 
	local phys = self:GetPhysicsObject()
	self.NextThink = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
	end
 
	self.val1 = 0 --Will store the energy
self.val2 = 0 --Store the coolant
	LS_RegisterEnt(self) --Register the entity
	RD_AddResource(self, "energy", 0) --Add the energy resource
	RD_AddResource(self, "coolant", 0)
end
 
function ENT:SpawnFunction( ply, tr )
 
	if ( !tr.Hit ) then return end
 
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
 
	local ent = ents.Create( "rd2_coolantgen" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end
 
function ENT:Use()
	if (CurTime() >= self.NTog) then
		if (self.Active == true) then
			self:StopSound( "apc_engine_start" )
			self:EmitSound( "apc_engine_stop" )
			self.Active = false
		else
			self:EmitSound( "apc_engine_start" )
			self.Active = true
		end
		self.NTog = CurTime() + 1
	end
end
 
--Remove all links to it
--Turn off all sounds (also emit the stop sounds)
function ENT:OnRemove()
	Dev_Unlink_All(self.conduit)
	if ( self.Active == true ) then
		self:StopSound( "apc_engine_start" )
		self:EmitSound( "apc_engine_stop" )
		self.Active = false
	end
end
 
function ENT:Think()
	self.val1 = RD_GetResourceAmount(self, "energy")
	self.val2 = RD_GetResourceAmount(self, "coolant")
	local ASt = ""
	if (self.Active == true) then
		ASt = "On"
	else
		ASt = "Off"
	end
 
	self:SetOverlayText( "RD2 Coolant Generator\n".. ASt .. "\nEnergy: " .. self.val1 .. "\nCoolant: " .. self.val2 )
 
	if (self.Active == true) then
		if (self.val1 > 0) then
			if (self.val1 > 0) then	
				if (self.val1 >= 100) then
					RD_ConsumeResource(self, "energy", 40)
					RD_SupplyResource(self, "coolant", 35)
				else
					RD_ConsumeResource(self, "energy", self.val1)
					local prod = math.Round(self.val1 - 5)
					RD_SupplyResource(self, "coolant", prod)
				end
			end
		else
			self:StopSound( "apc_engine_start" )
			self:EmitSound( "apc_engine_stop" )
			self.Active = false
		end
	end
 
end
 
function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self)
end
 
function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
end
 


Yarin Kaul Icon ArrowSquare32.png Back to KillerLUA Tutorials


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox