Explode a prop

I have a function which explodes a prop, thanks to whoever wrote this code, I found it in another thread:


						local explosion = ents.Create( "env_explosion" )
						explosion:SetKeyValue( "spawnflags", 174 )
						explosion:SetKeyValue( "iMagnitude", 45 ) 
						explosion:SetKeyValue( "iRadiusOverride", 456 )
						explosion:SetPos(trent:GetPos()) 
						explosion:Spawn( ) 
						explosion:Fire("explode","",0)

I wonder, how can I make the entity(trent) get impact from the explosion? so it flies away?

[editline]4th September 2013[/editline]

This is not solved, I must have put it by mistake!

What’s the code you used to spawn Trent? Is it a physics prop? Did you :Spawn( ), and :Activate( ), and PhysWake( ) if it wasn’t moving?

It is a car :downs:
Which is driving so it’s activated and physwaked.

I noticed when I ran the code, it did rock a car which I spawned.

According to: if you need a larger magnitude, create more explosions!!!

Doing it 500 times actually throws the car quite a LARGE distance!
[lua] for i = 1, 500 do
local explosion = ents.Create( “env_explosion” )
explosion:SetKeyValue( “spawnflags”, 174 )
explosion:SetKeyValue( “iMagnitude”, 100 ) // Clamped from 1 to 100
explosion:SetKeyValue( “iRadiusOverride”, 150)
explosion:SetPos( trace.HitPos )
explosion:Spawn( )
explosion:Fire(“explode”,"",0)

	// Add a timer, sometimes the ent is removed TOO quickly without it, and it doesn't explode.
	// Or use the kill flag
	timer.Simple( 1, function( )
		explosion:Remove( );
	end )
end[/lua]

Make sure you remove the explosion after you’re done, the entity stays when you’re done exploding.

Read into FLAGS:
1 : No Damage - Only Force
2 : Push players
4 : Push radially - not as a sphere
8 : Test LOS before pushing
16 : Disorient player if pushed

If I were you, I’d create a helper function for the explosion, which lets you decide magnitude, how many times to force, and also for the FORCE explosions ( repeats in the loop ), don’t do damage. Reserve damage for FIRST explosion only, otherwise you may have some WEIRD effects on death such as spammed audio, etc.

EDIT: Added timer around the Explode ( had originally, then removed as it seemed to be fine, re-added as there was an issue without at times )

EDIT: You’ve given me a new function idea to add to my skeleton game-mode to help people with development.

There needs to be an explosion effect added, but aside from that it works fine.

SERVERSIDE:

Arguments are:
Position of the center of the explosion,
Magnitude of explosion ( While the default is clamped from 1 to 100, this function will generate MORE explosions until all magnitude is dispersed ),
Radius of explosion ( again, a visual effect would need to be added for the full radius, which will come in final copy ),
Damage caused if you were standing at the center of the explosion ( It travels more outwards than up, so at 100 if it’s right under you, you’d survive barely, but right beside you at the same height, no – ADDITIONALLY damage is only created 1 time - util.BlastDamage is finicky in the sense that it sometimes creates other effects; final version will have it all - DAMAGE IS OPTIONAL, 0 by default, if 0 then it just force pushes! ),
Which Weapon caused the damage ( In case a grenade, or other entity is to be held accountable OPTIONAL - world by default ),
Which Player caused the damage ( In case a Player is to be held accountable OPTIONAL - world by default )

[lua]CreateExplosion( Vector( 0, 0, 0 ), – Pos
14187, – Magnitude
450, – Radius of damage / force
500 – damage
);[/lua]

Here’s a rough copy:
[lua]//
// CreateExplosion - with proper magnitude dispersement, damage, etc… - Josh ‘Acecool’ Moser
//
function CreateExplosion( _pos, _magnitude, _radius, _damage, _inflictorWeapon, _inflictorPlayer )
// Generate the damage from the explosion.
util.BlastDamage( _inflictorWeapon or game.GetWorld( ), _inflictorPlayer or game.GetWorld( ), _pos, _radius or _magnitude, _damage or 0 )

// Ensure that the entire magnitude is dispensed in 100 increments/intervals
local _times = math.ceil( _magnitude / 100 );	
for i = 0, _times - 1 do
	local _mag = ( _magnitude - ( i * 100 ) )

	// Generate the explosion / force of the explosion.
	local explosion = ents.Create( "env_explosion" )
	
	// No damage just force, push players, disorient players if pushed
	explosion:SetKeyValue( "spawnflags", 1 + 2 + 16 );

	// Mag is clamped from 1 to 100 anyway
	explosion:SetKeyValue( "iMagnitude", math.Clamp( _mag, 1, 100 ) );

	// Radius or magnitude
	explosion:SetKeyValue( "iRadiusOverride", _radius or _magnitude );

	explosion:SetPos( _pos );
	explosion:Spawn( );

	// Touch-Off
	explosion:Fire( "Explode", "", 0 );

	// Remove it from the world
	explosion:Fire( "Kill", "", 0 );
end

end[/lua]

Edit again:

A few resources. So, env_explosion is a visual explosion, ar2explosion is a nice dust cloud, physexplosion nudges phys objects and stuff, env_shake to shake screen etc etc…
My skeleton game-mode will have all of these coded and ready to use. I did create a new helper function to manage all of them with input, table of options; but the above function should serve as more than enough for the player; although the dust cloud might be nice for you:

[lua]function CreateExplosionDustCloud( _pos )
local explosion = ents.Create( “env_ar2explosion” );
explosion:SetPos( _pos )
explosion:Spawn( )

// Touch-Off
explosion:Fire( "Explode", "", 0 );

// Remove it from the world
explosion:Fire( "Kill", "", 0 );

end[/lua]

Simply add that function call right after util.BlastEffect, don’t do it inside the for loop.