Easy Lua SWEP Question

So I’ve got a function that throws props, and after a set amount of time, deletes it. But, for the life of me, I can’t figure out how to make it explode on impact. I know I need it to detect a collision, remove the prop, and then emit an explosiion effect in the place that the prop collided with.

But I’m a C# guy (BLASPHEMY!) so attempting to do this in Lua confuses the hell out of me.

Here’s the applicable code (It’s a bit of a mess, but the comments should mostly explain it. The Entity variable is baby, physics object variable is phys, and velocity variable is velocity.)


function SWEP:ThrowBaby( model_file )
	self.Owner:EmitSound( sounds[math.random(#sounds)] )
		if ( CLIENT ) then return end
	local baby = ents.Create( "prop_physics" ) --create physic prop
		if ( !IsValid( baby ) ) then return end --doublecheck
	baby:SetModel( model_file ) --set prop to desired model file
	baby:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 14 ) ) --set angles and position
	baby:SetAngles( self.Owner:EyeAngles() )
	baby:Spawn()
	local phys = baby:GetPhysicsObject() --doublecheck
		if ( !IsValid( phys ) ) then baby:Remove() return end
	local velocity = self.Owner:GetAimVector() --velocity variable
	velocity = velocity * 1500
	phys:ApplyForceCenter( velocity )
	self.Weapon:SendWeaponAnim( ACT_VM_THROW ) --animations
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
		local function removebaby() --remove baby function
			baby:Remove();
		end
	timer.Simple(10, removebaby);
	
	
		local function blowbaby()
			baby:Remove();
		end
		
		local function 
	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay) --reload delay
	cleanup.Add( self.Owner, "props", baby ) --add to server cleanup
	
end

(Yes my SWEP throws babies)

I’ll give you an answer but

Alright onto the actual answer:

Use ENT:Think() (That’s what I’d do) as a function. Then use util.BlastDamage. Something like:



function ENT:Think()
if tr.Hit then
                local tr = util.TraceLine( trace )
		self.timeleft2 = CurTime()
		explodeDamage = (((20 -(self.timeleft - self.timeleft2)) * 125) + 175)
		explodeRadius = (((20 -(self.timeleft - self.timeleft2)) * 225) + 200)
                self.Entity:EmitSound("babies/crying_baby.mp3")
                util.BlastDamage(self.Entity, self.Owner, tr.HitPos, explodeRadius, explodeDamage)
                local effectdata = EffectData()
		effectdata:SetOrigin(tr.HitPos)
		effectdata:SetNormal(tr.HitNormal)
		effectdata:SetEntity(self.Entity)
		effectdata:SetScale(10)
		effectdata:SetRadius(5)
		effectdata:SetMagnitude(120)
		util.Effect("Explosion", effectdata)
                self.Entity:SetNWBool("smoke", false)
		self.Entity:Remove()
end


Don’t quote me on this.

Since you’re using prop_physics you can change the physics damage scale and add an explosion damage and radius to it using baby:SetKeyValue() function, and a OnTakeDamage addoutput to break the prop when it touches something with the baby:Fire() function. I’ll go look up the keyvalues you need.

[editline]1st December 2014[/editline]

This will make your babies explode. Call these after you spawn the baby.
[lua]
baby:Fire(“addoutput”,“OnTakeDamage !self,break”,0)
baby:Fire(“addoutput”,“explodedamage 10000”,0)
baby:Fire(“addoutput”,“exploderadius 10000”,0)
baby:Fire(“addoutput”,“physdamagescale 10000”,0)
[/lua]

You can also do (but I haven’t tested it)
[lua]
–Before baby spawns
baby:SetKeyValue(“explodedamage”,“10000”)
baby:SetKeyValue(“exploderadius”,“10000”)
baby:SetKeyValue(“physdamagescale”,“10000”)
baby:Spawn()
–After baby spawned
baby:Fire(“addoutput”,“OnTakeDamage !self,break”,0)
[/lua]
I’m not sure if Fire needs to be called after the prop is spawned or not, but I would do that just to be safe. I know SetKeyValue has to be done prior to spawning the prop. (I could be wrong, but that’s what the previous gmod wiki said.)

If you want to do fancy effects or something, you should make the baby a scripted entity and use the OnTakeDamage scripted entity function.

This worked perfectly! Thanks! And I never would have known how powerful an explosion of “10000” was until I threw it miles away from me and still died :dance: