TTT Entity Replacer

I run a TTT server and have custom weapons, and I’m trying to make it so that when a default weapon spawns, say weapon_ttt_m16, it will redirect to a table of entities to replace it, in this case other assault rifles, with an equal chance to spawn. I want to do this so that the same type of weapon spawns next to its appropriate ammo, and each weapon gets a fair chance to spawn. I tried copying the ttt_random_weapon entity into the file for weapon_ttt_m16 forever ago and replacing theall spawnable weapon entities with a table, but that resulted in nothing spawning. How would I make it so that when a weapon entity spawns at the start of a round, it would replace it with a random weapon from a table?

Thanks,
Zeph

Start by showing what you’ve done so far.

More details:
I want to creating an entity (say ttt_random_assaultrifle), where every instance of weapon_ttt_m16 is, and removing the m16. I don’t know how to do that. Putting the code for ttt_random_assaultrifle inside of the shared.lua of weapon_ttt_m16 resulted in no weapon spawning.

BKU’s Default, Uneddited, ttt_random_weapon entity, found in the entities folder, under ttt_random_weapon/init.lua:


---- Dummy ent that just spawns a random TTT weapon and kills itself

ENT.Type = "point"
ENT.Base = "base_point"

ENT.AutoAmmo = 0

function ENT:KeyValue(key, value)
   if key == "auto_ammo" then
      self.AutoAmmo = tonumber(value)
   end
end

function ENT:Initialize()
   local weps = ents.TTT.GetSpawnableSWEPs()
   if weps then
      local w = table.Random(weps)
      local ent = ents.Create(WEPS.GetClass(w))
      if IsValid(ent) then
         local pos = self:GetPos()
         ent:SetPos(pos)
         ent:SetAngles(self:GetAngles())
         ent:Spawn()
         ent:PhysWake()

         if ent.AmmoEnt and self.AutoAmmo > 0 then
            for i=1, self.AutoAmmo do
               local ammo = ents.Create(ent.AmmoEnt)
               if IsValid(ammo) then
                  pos.z = pos.z + 3 -- shift every box up a bit
                  ammo:SetPos(pos)
                  ammo:SetAngles(VectorRand():Angle())
                  ammo:Spawn()
                  ammo:PhysWake()
               end
            end
         end
      end

      self:Remove()
   end
end

BKU’s Default, uneddited, weapon_ttt_m16 entity, found in the weapons folder, under weapon_ttt_m16/shared.lua:


if SERVER then

   AddCSLuaFile( "shared.lua" )
end

SWEP.HoldType			= "ar2"


if CLIENT then

   SWEP.PrintName			= "M16"
   SWEP.Slot				= 2

   SWEP.Icon = "VGUI/ttt/icon_m16"
end


SWEP.Base				= "weapon_tttbase"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.Kind = WEAPON_HEAVY
SWEP.WeaponID = AMMO_M16

SWEP.Primary.Delay			= 0.19
SWEP.Primary.Recoil			= 1.6
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "Pistol"
SWEP.Primary.Damage = 23
SWEP.Primary.Cone = 0.018
SWEP.Primary.ClipSize = 20
SWEP.Primary.ClipMax = 60
SWEP.Primary.DefaultClip = 20
SWEP.AutoSpawnable      = true
SWEP.AmmoEnt = "item_ammo_pistol_ttt"

SWEP.UseHands			= true
SWEP.ViewModelFlip		= false
SWEP.ViewModelFOV		= 64
SWEP.ViewModel			= "models/weapons/cstrike/c_rif_m4a1.mdl"
SWEP.WorldModel			= "models/weapons/w_rif_m4a1.mdl"

SWEP.Primary.Sound = Sound( "Weapon_M4A1.Single" )

SWEP.IronSightsPos = Vector(-7.58, -9.2, 0.55)
SWEP.IronSightsAng = Vector(2.599, -1.3, -3.6)


function SWEP:SetZoom(state)
   if CLIENT then return end
   if not (IsValid(self.Owner) and self.Owner:IsPlayer()) then return end
   if state then
      self.Owner:SetFOV(35, 0.5)
   else
      self.Owner:SetFOV(0, 0.2)
   end
end

-- Add some zoom to ironsights for this gun
function SWEP:SecondaryAttack()
   if not self.IronSightsPos then return end
   if self.Weapon:GetNextSecondaryFire() > CurTime() then return end

   bIronsights = not self:GetIronsights()

   self:SetIronsights( bIronsights )

   if SERVER then
      self:SetZoom(bIronsights)
   end

   self.Weapon:SetNextSecondaryFire(CurTime() + 0.3)
end

function SWEP:PreDrop()
   self:SetZoom(false)
   self:SetIronsights(false)
   return self.BaseClass.PreDrop(self)
end

function SWEP:Reload()
   self.Weapon:DefaultReload( ACT_VM_RELOAD );
   self:SetIronsights( false )
   self:SetZoom(false)
end


function SWEP:Holster()
   self:SetIronsights(false)
   self:SetZoom(false)
   return true
end

I tried changing line 15 of the ttt_random_weapon to


local weps = {weapon_ttt_aug, weapon_ttt_ak47, weapon_ttt_m4a1, weapon_ttt_sg552, weapon_ttt_famas}

and pasting that in the weapon_ttt_m16 file, but it did not spawn any weapons. I thought this may be due to it having no weapon variables, such as


SWEP.Spawnable = true

and


SWEP.Base = "weapon_tttbase"

Any ideas how to get this to work?

Oh, also, I would have separate entities such as:

ttt_random_smg with the table {weapon_ttt_p90, weapon_ttt_mac10, weapon_ttt_mp5}
ttt_random_hpistol with the table {weapon_ttt_deserteagle, weapon_357magnum}
ttt_random_lpistol with the table {weapon_ttt_fiveseven, weapon_ttt_p228}

etc

I’m definitely interested in this. Have you had any luck with this?