Simple explosive

im searching for a while now for a simple entiti which can explode after a time…
i want to spawn it example with ents.Create(“boom”)
then i have a folder boom with a lua file which creates an explosion on a position after a half second or something (i want to set it up in lua file)

my question is how to do this
and if i have to activate it after the ents.Create(“boom”) with something like Activate()

Hehe I had a lot of fun making this one! :v:

http://www.garrysmod.org/img/?t=dll&id=79623

[release]How to use : When spawning this entity you can choose to customize the explosion with these few properties.

Entity.Delay is the time before an automatic detonation. (integer)
Entity.Radius is the radius of the explosion. (integer)
Entity.Damage is the damage dealt. (integer)
Entity.Cheap is whether or not a an explosion easier to render will be used (boolean)
Entity.Manual is whether or not the explosion must be triggered with Entity:Detonate(). (boolean)
Entity.Repeatable is whether or not the explosion will remove itself after the first detonation. (boolean)
Boolean means true or false btw.[/release]

It’s a point entity (actually it’s just a wrapper for Env_explosion but this is simpler) so it has no model, you can spawn have it explode after a set delay automatically or manually with Entity:Detonate(). You can set it’s damage and radius and choose if you want a pretty explosion or a simple one (that doesn’t lag) with Entity.Cheap. You can even re-use it if you want the same explosion at the same place with Entity.Repeatable.

I hope this is what you wanted. :slight_smile:

Ok wow… Thanks
I didnt really expected a nice help like this :slight_smile:
It seems you gave a lot on helping me.

Im a very big lua noob… I can change some things to make them better but not anything big. (But for general I know many things in Batch/Cmd. So maybe I can learn some lua stuff.)
Ok first how I define the values with Entity.Radius(800) or Entity.Radius = 800 (if I don’t have to set it up in simple_explosion lua file)

The problem with this is that I can test it but it takes very long for each test. (Start gmod, start game, close it and then again)
I don’t have to do anything in the the SE files, just do the commands in the main file. I think that and another point is do I have to define all these values or are there default ones?

Just do create server then disconnect and do it again to test? Why close gmod?

Or better, just reload the entity. There was a script called lua_reloadent somewhere on facepunch :slight_smile:

Oh I can’t believe I forgot that part! That’s right there’s no use editing the lua files, unless you want to change some defaults. Here’s an example usage :



function SpawnExplosion( ply ) //This function will be called trough a console command which passes the player object.

	tr = ply:GetEyeTrace() // Creating a line from the player's line
	
	if !tr.Hit then return end //If it hit nothing end the execution
	
	exp = ents.Create("simple_explosion") //Creating the explosion entity
	
	//Setting up the explosion's properties, note that they are ALL optional.	
	exp.Radius = 150 // Changing the default Radius
	exp.Damage = 70 // Changing the default Damage
	exp.Delay = 0 // This already is the default so I could leave this line out
	exp.Cheap = true// Making it so a cheap explosion is used
	exp.Manual = false // Not manual, if it's not set to true I can leave that part out and it won't change a thing.
	exp.Repeatable = false // It will remove itself after exploding
	
	exp:SetPos( tr.HitPos ) // Positioning the explosion where the line of the player's eyes hit
	
	exp:Spawn() // Creating and activating the explosion.
	
end

concommand.Add( "Explosion", SpawnExplosion ) // Linking the SpawnExplosion function to the console command Explosion, it can be bound to a key.


I made that example with every property possible used, this one allows for quick spamming of the explosions and it’s a lot of fun ;).

Note that all properties are optional, the defaults are 0 seconds delay, 200 radius, 100 damage and every other option turned off.

What’s really important when spawning the explosion (or any entity) is setting it’s class, it’s position and then spawning it. The rest can be ignored.

As for quickly experimenting I suggest you run garrysmod in windowed mode. There are also a few commands you can take advantage of. (Btw when testing I suggest you take Wiremod, phx and other big addons off your addons folder because it slows you down tons.)

gamemode_reload (reloads the gamemode)
gamemode_reload_cl (reloads the gamemode from the client’s perspective)
lua_run (Run any lua code with this)
lua_run_cl (Run lua code as a client)
lua_openscript (Open or reload a lua file. You could test your code in a file in garrysmod/lua and simply reload it with this.
lua_openscript_cl (Same thing but clientside)
lua_reloadents (Reloads all scripted entities and sweps, might take a moment with big addons installed)
lua_reloadent (Reloads a single entity/swep. Must be downloaded first.)

And here’s a really useful addon :
Luapad (Allows you to code ingame and test your code on the run!

There, I hope I didn’t forget anything.

Ok thank you for all.
I don’t wanted to tell you that because maybe there is already a thing and so you would’t helped me…
So im doing this to get a custom redeemer weapon. (Its a rpg but when you shoot you can press rightclick and conrol the rocket [you are the rocket] ) You can find it in the “Nuke 4” pack. It is much fun to do that but you kill everything in the map. So im trying to do just a small explosion.

… So wheres my problem? If I hit anything then I get just an error !!! :frowning:
includes/extensions/math.lua:66: attempt to compare nil with number

Here is my modified version if the code:
Additional: I changed exp.bla to nuke.bla and I put the simple_explosion folder to the addons lua/entities folder and renamed it to sent_nuke caus that was default…
Scroll to middle i marked the function…



local sndRPGFire = Sound("Weapon_RPG.Single")
REDEEMER = {}

if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	AddCSLuaFile( "cl_init.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= true
	SWEP.AutoSwitchFrom		= true
	
	SWEP.HoldType			= "rpg"

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= true
	SWEP.ViewModelFOV		= 64
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= false
	
	
	SWEP.PrintName			= "Controlable RPG"			
	SWEP.Author			= "Monoffel"
	SWEP.Category			= "Other"
	SWEP.Slot				= 3
	SWEP.SlotPos			= 10

end

SWEP.Author			= "..."
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= "LM=Shoot RM=Switch User Control"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.ViewModel			= "models/weapons/v_rpg.mdl"
SWEP.WorldModel			= "models/weapons/w_rocket_launcher.mdl"

SWEP.Primary.Recoil			= 0
SWEP.Primary.Damage			= -1
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0
SWEP.Primary.Delay			= 2

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= 3
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "slam"
SWEP.Primary.NextFire 		= 0

SWEP.Secondary.Recoil		= 0
SWEP.Secondary.Damage		= -1
SWEP.Secondary.NumShots		= 1
SWEP.Secondary.Cone			= 0
SWEP.Secondary.Delay		= 0.3

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.NextFire 	= 0

SWEP.Rocket = SWEP.Rocket or NULL
SWEP.IsGuidingNuke = false
SWEP.DrawReticle = false
SWEP.LastAng = Vector(0,0,0)


function SWEP:Initialize()

	if ( SERVER ) then
		self:SetWeaponHoldType( self.HoldType )
	end

end


function SWEP:Deploy()

	self.Owner:SetNWBool("DrawReticle",false)

end


function SWEP:Reload()
	if self.Rocket and self.Rocket:IsValid() then return false end
	self.Weapon:DefaultReload( ACT_VM_RELOAD )
end

function SWEP:Think()

	if self.IsGuidingNuke and self.Rocket and self.Rocket:IsValid() then
		local PlayerAng = self.Owner:GetAimVector()
		self.Rocket:SetAngles(PlayerAng:Angle())
		self.RocketPhysObj:SetVelocity(PlayerAng*1200)
		
		local ViewEnt = self.Owner:GetViewEntity() -- we should be able to do this client-side, but for some fucking reason GetViewEntity() is server only
		
		if self.DrawReticle and ViewEnt ~= self.Rocket then
			self.DrawReticle = false
			self.Owner:SetNWBool("DrawReticle",false)
		end
		
		if not self.DrawReticle and ViewEnt == self.Rocket then
			self.DrawReticle = true
			self.Owner:SetNWBool("DrawReticle",true)
		end
		
		if ViewEnt == self.Owner or ViewEnt == NULL then
			self.Owner:SetViewEntity(self.Rocket) 
		end
		
	else
		self:StopGuiding()
	end

end
**
function SWEP:PrimaryAttack()

	
	if self.Primary.NextFire > CurTime() or self.Owner:GetAmmoCount("slam") < 1 then 
	return end
	
	self.Primary.NextFire = CurTime() + self.Primary.Delay
	
	if self.IsGuidingNuke and self.Rocket and self.Rocket:IsValid() then
		self:StopGuiding()
		nuke:SetOwner(self.Owner)
		
	nuke = ents.Create("sent_nuke") //--Creating the explosion entity
	
	//--Setting up the explosion's properties, note that they are ALL optional.	
	nuke.Radius = 150 //-- Changing the default Radius
	nuke.Damage = 70 //-- Changing the default Damage
	nuke.Delay = 0 //-- This already is the default so I could leave this line out
	nuke.Cheap = true//-- Making it so a cheap explosion is used
	nuke.Manual = false //-- Not manual, if it's not set to true I can leave that part out and it won't change a thing.
	nuke.Repeatable = false //-- It will remove itself after exploding
	
	nuke:SetPos( self.Rocket:GetPos() )
	
	nuke:Spawn() //-- Creating and activating the explosion.
	self.Rocket:Remove()
		
		return
	end
	
**
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
	self.Weapon:EmitSound(sndRPGFire)
	self.Owner:MuzzleFlash()

	self:TakePrimaryAmmo(1)

	local PlayerPos = self.Owner:GetShootPos()
	local PlayerAng = self.Owner:GetAimVector()
	
	--the muzzle attachement for the rocket launcher is fucked, so we need to adjust the missile's position by hand
	local PlayerForward = self.Owner:GetForward()
	local PlayerRight = self.Owner:GetRight()
	local SpawnPos = PlayerPos + 32*PlayerForward + 32*PlayerRight
	
	local trace = {}
	trace.start = PlayerPos + PlayerAng*32
	trace.endpos = PlayerPos + PlayerAng*16384
	trace.filter = {self.Owner}
	local traceRes = util.TraceLine(trace)

	self.Rocket = ents.Create("sent_nuke_missile")
	self.Rocket:SetVar("owner",self.Owner)
	self.Rocket:SetPos(SpawnPos)
	self.Rocket:SetAngles(PlayerAng:Angle())
	self.Rocket:Spawn()
	self.Rocket:Activate()
	
	self.RocketPhysObj = self.Rocket:GetPhysicsObject()
	self.RocketPhysObj:SetVelocity(PlayerAng*512 - 16*PlayerRight + Vector(0,0,256))
	
	timer.Simple(0.5, REDEEMER.AimRocket, self.Rocket, traceRes.HitPos)
	
	
	
end

function SWEP:SecondaryAttack()

	if self.Secondary.NextFire > CurTime() then 
	return end
	
	self.Secondary.NextFire = CurTime() + self.Secondary.Delay

	if self.IsGuidingNuke then
		self:StopGuiding()
	else
		self:StartGuiding()
	end
	
end

function SWEP:StartGuiding()

	if not self.Rocket or self.Rocket == NULL then return end

	self.LastAng = self.Owner:EyeAngles()
	self.Owner:SetEyeAngles(self.Rocket:GetAngles())
	
	self.IsGuidingNuke = true
	self.DrawReticle = true
	self.Owner:SetNWBool("DrawReticle",true)
	self.Owner:SetViewEntity(self.Rocket)
	
	if SERVER then
		self.Owner:DrawViewModel(false) --we need to hide the viewmodel while we're guiding the rocket.  Otherwise it would look fugly.
		self.Owner:CrosshairDisable()
	end


end

function SWEP:StopGuiding()

	if not self.IsGuidingNuke then return end

	self.IsGuidingNuke = false
	self.DrawReticle = false
	self.Owner:SetNWBool("DrawReticle",false)
	self.Owner:SetViewEntity(self.Owner)
	
	if SERVER then
		self.Owner:DrawViewModel(true)
		self.Owner:CrosshairEnable()
	end
	
	self.Owner:SetEyeAngles(self.LastAng)
	
end

function REDEEMER.AimRocket(rocket,pos)

	local NewAng = (pos - rocket:GetPos()):GetNormalized()
	rocket:SetAngles(NewAng:Angle())

end


function SWEP:Holster()

	self:StopGuiding()
	return true

end


function SWEP:OnRemove()

	self:StopGuiding()
	return true
end