Setting entity variables through SWEPS

So I’m working on getting a menu into my SWEP pack (GDCW) so I can allow players to set options for their guns. So far the menu is working fine but some variables aren’t transferring from the SWEP base to the entity bullet that gets fired. The player sets a variable through a menu, then the SWEP base reads that and sets the “Tracer” variable for the bullet. I’ve run some tests and it turns out that the SWEP base is working right but the bullet doesn’t seem to receive the var.



function SWEP:FireRocket()
	if (self:GetIronsights() == true) and self.Owner:KeyDown(IN_ATTACK2) then
	aim = self.Owner:GetAimVector()
	else 
	aim = self.Owner:GetAimVector()+Vector(math.Rand(-0.02,0.02), math.Rand(-0.02,0.02),math.Rand(-0.02,0.02))
	end
	if !self.Owner:IsNPC() then
	pos = self.Owner:GetShootPos()
	else
	pos = self.Owner:GetShootPos()+self.Owner:GetAimVector()*50
	end
	if SERVER then
		local bullet = ents.Create(self.Primary.Round)
		if !bullet:IsValid() then return false end
		bullet:SetAngles(aim:Angle()+Angle(90,0,0))
		bullet:SetPos(pos)
		bullet:SetOwner(self.Owner)
		bullet:Spawn()
		if self.Primary.TracerCount>=self.Primary.TracerOn then 
		bullet.Tracer = true
		bullet:SetVar("Tracer", 1)
		print("SWEP Tracer")
		self.Primary.TracerCount = 0
		else 
		bullet.Tracer = false
		end
		bullet:Activate()
	end


		if SERVER and !self.Owner:IsNPC() then
		local anglo = Angle(math.Rand(-self.Primary.KickDown,self.Primary.KickUp), math.Rand(-self.Primary.KickHorizontal,self.Primary.KickHorizontal), 0)
		self.Owner:ViewPunch(anglo)
		angle = self.Owner:EyeAngles() - anglo
		self.Owner:SetEyeAngles(angle)
		end
end


That’s for the SWEP base. It prints Tracer in console every 3rd shot so I know it’s working right. Here’s the receiving bullet code;



       
print(tostring(self.Tracer))

if self.Tracer then
print("Bullet Tracer")
Trail = ents.Create("env_spritetrail")
Trail:SetKeyValue("lifetime","0.1")
Trail:SetKeyValue("startwidth","20")
Trail:SetKeyValue("endwidth","5")
Trail:SetKeyValue("spritename","trails/laser.vmt")
Trail:SetKeyValue("rendermode","5")
Trail:SetKeyValue("rendercolor","255 150 100")
Trail:SetPos(self.Entity:GetPos())
Trail:SetParent(self.Entity)
Trail:Spawn()
Trail:Activate()
Glow = ents.Create("env_sprite")
Glow:SetKeyValue("model","orangecore2.vmt")
Glow:SetKeyValue("rendercolor","255 150 100")
Glow:SetKeyValue("scale","0.10")
Glow:SetPos(self.Entity:GetPos())
Glow:SetParent(self.Entity)
Glow:Spawn()
Glow:Activate()
end


In Console it just prints “Nil” for every shot.

Can anybody help? I’m probably just doing something really stupid… Thanks!

Is that bullet code being run on the client? If so, it wouldn’t work as you set the Tracer var on the Server, not the client.

No, the bullet is running on init. They’re both serverside.

What function is the 2nd part in?

Initiation. I’ve been using it for a long time and it works, it just needs to get a value of 1 or true to work.

Initiation is not a function. if you mean Initialize() then thats your problem, self.Tracer variable will get set after the Initialize() function gets called. Because it gets called right when you spawn the entitiy. If this is your problem you will have to create and call a function on the entity after you spawn it when you want to do your tracer stuff.



AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()   

math.randomseed(CurTime())
self.Owner = self:GetOwner()
self.penetrate = 10
self.flightvector = self.Entity:GetUp() * ((900*39.37)/66)             	-- Velocity in m/s, inches to meters conversion, ticks per second.FIRST NUMMER = SPEED

self.timeleft = CurTime() + 5
self.Entity:SetModel( "models/led.mdl" ) 	
self.Entity:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,  	
self.Entity:SetMoveType( MOVETYPE_NONE )   --after all, gmod is a physics  	
self.Entity:SetSolid( SOLID_VPHYSICS )        -- CHEESECAKE!    >:3    
       
print(tostring(self.Tracer))

if self.Tracer then
print("Bullet Tracer")
Trail = ents.Create("env_spritetrail")
Trail:SetKeyValue("lifetime","0.1")
Trail:SetKeyValue("startwidth","20")
Trail:SetKeyValue("endwidth","5")
Trail:SetKeyValue("spritename","trails/laser.vmt")
Trail:SetKeyValue("rendermode","5")
Trail:SetKeyValue("rendercolor","255 150 100")
Trail:SetPos(self.Entity:GetPos())
Trail:SetParent(self.Entity)
Trail:Spawn()
Trail:Activate()
Glow = ents.Create("env_sprite")
Glow:SetKeyValue("model","orangecore2.vmt")
Glow:SetKeyValue("rendercolor","255 150 100")
Glow:SetKeyValue("scale","0.10")
Glow:SetPos(self.Entity:GetPos())
Glow:SetParent(self.Entity)
Glow:Spawn()
Glow:Activate()
end

self:Think()
end   

function ENT:Think()

		if self.timeleft < CurTime() then
					self.exploded = true
					self.Entity:Remove()
					
	end

	Table	={} 			//Table name is table name
	Table[1]	=self.Owner 		//The person holding the gat
	Table[2]	=self.Entity 		//The cap

	local trace = {}
		trace.start = self.Entity:GetPos()
		trace.endpos = self.Entity:GetPos() + self.flightvector
		trace.filter = Table
	local tr = util.TraceLine( trace )
	
				if tr.HitSky then
				self.Entity:Remove()
				return true
				end


					if tr.Hit and !tr.Entity:IsPlayer() and !tr.Entity:IsNPC() then
					util.BlastDamage(self.Entity, self.Entity, tr.HitPos, 50, 10)
					local effectdata = EffectData()
					effectdata:SetOrigin(tr.HitPos)
					effectdata:SetScale(1)
					effectdata:SetRadius(tr.MatType)
					effectdata:SetNormal(tr.HitNormal)
					util.Effect("gdcw_universal_impact",effectdata)
					util.ScreenShake(tr.HitPos, 10, 5, 0.1, 200 )
					util.Decal("ExplosiveGunshot", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
					end

					if tr.Hit and tr.Entity:IsPlayer() || tr.Entity:IsNPC() then
					local dmginfo = DamageInfo()
					dmginfo:SetDamage( math.Rand(20,35) ) 	-- 40-70 damage
					hitgroup = tr.HitGroup
	if hitgroup == HITGROUP_HEAD 					then 	dmginfo:ScaleDamage( 5 ) 			end
	if hitgroup == HITGROUP_STOMACH 					then 	dmginfo:ScaleDamage( 1 ) 			end
	if hitgroup == HITGROUP_CHEST 					then 	dmginfo:ScaleDamage( math.Rand(1,1.5) ) 	end
	if  hitgroup == HITGROUP_LEFTARM || hitgroup == HITGROUP_RIGHTARM  	then 	dmginfo:ScaleDamage( math.Rand(0.3,0.5) ) 	end
	if  hitgroup == HITGROUP_LEFTLEG || hitgroup == HITGROUP_RIGHTLEG  	then 	dmginfo:ScaleDamage( math.Rand(0.3,0.6) ) 	end
					dmginfo:SetDamageType( DMG_BULLET ) 	--Bullet damage
					dmginfo:SetAttacker( self.Owner ) 		--Shooter gets credit
					dmginfo:SetInflictor( self.Entity ) 		--Bullet gets credit
					dmginfo:SetDamageForce( self.flightvector/250 ) 	--A few newtons...
				tr.Entity:TakeDamageInfo( dmginfo ) 			--Take damage!
					local effectdata = EffectData()
					effectdata:SetOrigin(tr.HitPos)
					effectdata:SetNormal(self.flightvector:GetNormalized())
					effectdata:SetScale(0.9)
					util.Effect( "gdcw_bloodychunkyviolence", effectdata ) 	  	// Nothing violent here!
					util.Effect( "BloodImpact", effectdata )			// Nothing violent here either!
				end

	local trace = {}
		trace.start = tr.HitPos + self.flightvector:GetNormalized()*10
		trace.endpos = tr.HitPos
		trace.filter = self.Entity 
	local pr = util.TraceLine( trace )

		if pr.StartSolid || tr.Hit and !pr.Hit || self.penetrate<0 then
		self.Entity:Remove()
		end

		if tr.Hit and pr.Hit then
		self.penetrate = self.penetrate - (tr.HitPos:Distance(pr.HitPos))
		end

		if !pr.StartSolid and tr.Hit and self.penetrate>0 and !pr.Entity:IsPlayer() and !pr.Entity:IsNPC() then
				util.BlastDamage(self.Entity, self.Entity, pr.HitPos, 50, 10)
				self.Entity:SetPos(pr.HitPos + self.flightvector:GetNormalized()*10)
					local effectdata = EffectData()
					effectdata:SetOrigin(pr.HitPos)
					effectdata:SetNormal(self.flightvector:GetNormalized())
					effectdata:SetScale(1)
					effectdata:SetRadius(1)
					util.Effect( "gdcw_penetrate", effectdata )
					util.ScreenShake(tr.HitPos, 10, 5, 0.1, 250 )
					util.Decal("ExplosiveGunshot", pr.HitPos + pr.HitNormal, pr.HitPos - pr.HitNormal)
					end


	if !pr.Hit then
	self.Entity:SetPos(self.Entity:GetPos() + self.flightvector)
	end
	self.flightvector = self.flightvector - self.flightvector/100 + Vector(math.Rand(-0.1,0.1), math.Rand(-0.1,0.1),math.Rand(-0.1,0.1)) + Vector(0,0,-0.06)
	self.Entity:SetAngles(self.flightvector:Angle() + Angle(90,0,0))
	self.Entity:NextThink( CurTime() )
	return true
	end



There’s the entire code for the init file. It works, it just doesn’t show the tracer.

Oh, well there’s your issue. You can only set variables on the entity after it has been initialized in most situations. You just need another function on the ent for that.

But shouldn’t it already have the variable on it? How can I set the variable if the ent doesn’t have it?