Hello, i'm making a small entity, for the purpose of learning, it's a melon, and if you use it, it kills you and explodes, but, how can I make my entity explode? I searched Gmod Wiki, but couldn't find anything.
You can find this by looking at other peoples code and learning from it. However, since you want an answer. Use this code taken from the exploding swep example:
[lua]
local eyetrace = <PLAYER>:GetEyeTrace();
local explode = ents.Create( "env_explosion" ) //creates the explosion
explode:SetPos( eyetrace.HitPos )
//This will be where the player is looking (using)
explode:SetOwner( <PLAYER> ) // this sets you as the person who made the explosion
explode:Spawn() //this actually spawns the explosion
explode:SetKeyValue( "iMagnitude", "220" ) //the magnitude
explode:Fire( "Explode", 0, 0 )
explode:EmitSound( "weapon_AWP.Single", 400, 400 ) //the sound for the explosion, and how far away it can be heard
[/lua]
Just replace <PLAYER> with the player entity
[b]Edit:[/b]
Thanks for the LUA Helper :D
I'm not sure how to implent this into a ENT:Use function. This is like, my second day of lua, so I don't know much.
I was making a gamemode a while ago which I should finish but this is the bomb entity from it. The part you want is in the ENT:Touch but I thought I might as well give you the whole file
[Lua]
AddCSLuaFile( "shared.lua" )
include('shared.lua')
function ENT:Initialize()
self.Entity:SetModel("models/bomb/bomb.mdl")
self.Entity:PhysicsInit(SOLID_VPHYSICS)
self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
self.Entity:SetSolid(SOLID_VPHYSICS)
local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
phys:SetMass(100)
end
end
function ENT:Touch( ent)
local box = false
for k,v in pairs(ents.FindByClass("sent_bomb")) do
if(ent == v)then
box = true
end
end
for k,v in pairs(ents.FindByClass("sent_missile")) do
if(ent == v)then
box = true
end
end
// THE PART YOU WANT IS BELOW!!!!!
// THE PART YOU WANT IS BELOW!!!!!
if ((ent:IsVehicle()) or (box == true))then
local explosion = ents.Create( "env_explosion" ) // Creating our explosion
explosion:SetKeyValue( "spawnflags", 144 ) //Setting the key values of the explosion
explosion:SetKeyValue( "iMagnitude", 15 ) // Setting the damage done by the explosion
explosion:SetKeyValue( "iRadiusOverride", 256 ) // Setting the radius of the explosion
explosion:SetPos(self:GetPos()) // Placing the explosion where we are
explosion:Spawn( ) // Spawning it
explosion:Fire("explode","",0)
self.Entity:Remove()
end
end
[/Lua]
But there is stuff you will want from the code above
What I have right now is
[lua]
local igniteForSeconds = 10
function ENT:Use( activator, caller )
if ( activator:IsPlayer() ) then
activator:ConCommand("kill")
self:Ignite(igniteForSeconds,0)
local explosion = ents.Create( "env_explosion" ) // Creating our explosion
explosion:SetKeyValue( "spawnflags", 144 ) //Setting the key values of the explosion
explosion:SetKeyValue( "iMagnitude", 15 ) // Setting the damage done by the explosion
explosion:SetKeyValue( "iRadiusOverride", 256 ) // Setting the radius of the explosion
explosion:SetPos(self:GetPos()) // Placing the explosion where we are
explosion:Spawn( ) // Spawning it
explosion:Fire("explode","",0)
self.Entity:Remove()
end
end[/lua]
But, now it won't spawn, and no errors too.
If you want the player to die when he uses it, use
[lua] activator:Kill() [/lua] instead of making him run a console command to kill himself.
Okay, changed that, the explosion still won't work though.
I don't know what the problem is D: sorry.
Try adding this into the init function of the entity
[lua]
self.Entity:PhysicsInit(SOLID_VPHYSICS)
self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
self.Entity:SetSolid(SOLID_VPHYSICS)
[/lua]
That didn't work either. :sigh:
[lua]
local igniteForSeconds = 10
function ENT:Use( activator, caller )
activator:Kill()
self:Ignite(igniteForSeconds,0)
local explosion = ents.Create( "env_explosion" ) // Creating our explosion
explosion:SetKeyValue( "spawnflags", 144 ) //Setting the key values of the explosion
explosion:SetKeyValue( "iMagnitude", 15 ) // Setting the damage done by the explosion
explosion:SetKeyValue( "iRadiusOverride", 256 ) // Setting the radius of the explosion
explosion:SetPos(self:GetPos()) // Placing the explosion where we are
explosion:Spawn( ) // Spawning it
explosion:Activate()
explosion:Fire("explode","",0)
timer.Create("timer",0.1,1,function() self.Entity:Remove() end)
end
end[/lua]
That might help
edit: Because im not in game and can't play right now (fixing stuff) I haven't been able to test any of the code to make sure it works.
That still didn't work, can't spawn the entity.
I Suggest you make an Effect, and do a ent:Remove to remove your entity, and then render the effect.
I know how to make a effect, but how to let my entity do that, with what function?
Ok first you dont need to run kill for the person as if you just set the damage high it will kill him anyway, unless theres a reason for you to kill the person. Second have you attualy spawned the entity and do you have a shared file aswell?
I have the shared file, complete, I already spawned the entity some times, (before adding the explosion code) and it worked fine.
Sorry, you need to Log In to post a reply to this thread.