LUA:RD2Entity
From GMod Wiki
Lua: Resource Distrubtion Entity Tutorial |
Description: | How to create resource distrubtion entity's. |
Original Author: | KillerLUA |
Created: | March 12, 2010 |
Contents |
Introduction
In this tutorial, I will be showing you how to create a basic energy storage entity. It can be spawned from the spawn menu.
Setting up files
Gamemode
Create three new files called shared.lua, cl_init.lua and init.lua inside YourGamemode/entites/entites/rd2_energystorage
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_energystorage
Your done, now move onto Coding.
Coding
In shared.lua, we are going to add the information about the entity, if it's spawnable and if it's admin only.
Go ahead and open your shared.lua, enter these lines of code:
ENT.Type = "anim" --The entity source base, most entity's use anim ENT.Base = "base_gmodentity" --The entity's garrys mod base, most entity's use base_gmodentity ENT.Category = "KillerLUA Tutorial Sents" --The entites catagory in the spawn menu ENT.PrintName = "Energy Storage" --The name ENT.Author = "KillerLUA" --Author ENT.Contact = "Don't" --Contact email ENT.Spawnable = true --Spawnable? ENT.AdminSpawnable = false --If spawnable, is it admin only? /*Example two: ENT.Spawnable = true --Spawnable? ENT.AdminSpawnable = true --If spawnable, is it admin only? --Makes an admin only entity and so does: ENT.Spawnable = false --Spawnable? ENT.AdminSpawnable = true --If spawnable, is it admin only? */
Now that we have added the base lines of code, let's set some client things up first.
Not too complicated, mostly basic functions. Put this code into cl_init.lua.
include('shared.lua') --Include shared.lua function ENT:Initialize() --Empty, but you must have this function end function ENT:Draw() self:DrawModel() --Draw the model, without this. You'll get a invisible, modeless entity. end
Now, for the more advanced stuff
Coding the serverside bits
Let's get the first peice of code
AddCSLuaFile( "cl_init.lua" ) --Send cl_init.lua to the client AddCSLuaFile( "shared.lua" ) --Send shared.lua to the client include( 'shared.lua' ) --Include shared.lua function ENT:Initialize() self:SetModel( "models/Items/car_battery01.mdl" ) 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 --Store the energy in a variable RD_AddResource(self, "energy", 15000) --Add a resource to the storage device, with a maximum hold of 15000 energy end
In this bit, we set the model and add the energy resource with a capacity of 15000.
The first argument is the entity to add it to, this is useful for setting other entity's resources.
Now, let's look at the rest of the code
function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos + tr.HitNormal * 36 local ent = ents.Create( "rd2_energystorage" ) ent:SetPos( SpawnPos ) ent:Spawn() ent:Activate() return ent end function ENT:Use() end function ENT:OnRemove() Dev_Unlink_All(self:GetTable().conduit) --Remove all links to it when removed end function ENT:Think() self:SetOverlayText( "My Energy Cell\nEnergy: " .. self.val1) --Set the overlay text self.val1 = RD_GetResourceAmount(self, "energy") --Update the resource amount value on think self.NextThink = CurTime() + 1 end function ENT:PreEntityCopy() RD_BuildDupeInfo(self) --Pasting info for duplicators end function ENT:PostEntityPaste( Player, Ent, CreatedEntities ) RD_ApplyDupeInfo(Ent, CreatedEntities) --Pasting info for duplicators 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!