Cheers!
i want to add thruster sound to my "Pew Pew" weapon.
The sound is working but i can't stop it, and the sound continue emitting, when the rocket explode.
The sound only stopping when i shoot new rocket.
[lua]
-- Think
BULLET.ThinkOverride = true
function BULLET:ThinkFunc( self )
-- Make it fly
self.Entity:SetPos( self.Entity:GetPos() + self.FlightDirection * self.Bullet.Speed )
if (self.TargetPos != Vector(0,0,0)) then
self.FlightDirection = self.FlightDirection + (self.TargetDir-self.FlightDirection) / 20
self.FlightDirection = self.FlightDirection:GetNormalized()
end
if (self.Cannon:IsValid()) then
if (self.Cannon.TargetPos and self.Cannon.TargetPos != Vector(0,0,0)) then
self.TargetDir = (self.Cannon.TargetPos-self:GetPos()):GetNormalized()
end
end
self.Entity:SetAngles( self.FlightDirection:Angle() + Angle(90,0,0) )
self.SoundName = Sound( "thrusters/hover01.wav" )
if (self.Cannon:IsValid()) then
self:EmitSound( self.SoundName )
else
self:StopSound( self.SoundName )
end
[/lua]
Sorry im new in lua, yesterday started learning.
Thanks Divran!:)
You mean something like this?
I Don't understand how can i link back to the function.
[lua]
function Sound()
self.SoundName = Sound( "thrusters/hover01.wav" )
self:EmitSound( self.SoundName )
end
function ENT:OnRemove()
self:StopSound( self.SoundName )
end
[/lua]
You should use a sound that loops by itself, and create it in the initialize function and stop it in the explode function.
I know, i have to learn more about lua, but i think the best way when we use "more complex" lua's.
So, any idea how can i use this function in this homing missle?
When i create this sound in the initialize the sound work, and sound stop doesn't work while it's exploding, so same problem.
[lua]local SoundName = Sound( "thrusters/hover01.wav" )
function BULLET:InitializeFunc( self )
self.ThrusterSound = CreateSound( self, SoundName )
self.ThrusterSound:Play()
-- Stuff copied from the bullet entity
-----------------------------------------------------------------
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_NONE )
self.Entity:SetSolid( SOLID_NONE )
self.FlightDirection = self.Entity:GetUp()
self.Exploded = false
self.TraceDelay = CurTime() + self.Bullet.Speed / 1000 / 4
-- Lifetime
self.Lifetime = false
if (self.Bullet.Lifetime) then
if (self.Bullet.Lifetime[1] > 0 and self.Bullet.Lifetime[2] > 0) then
if (self.Bullet.Lifetime[1] == self.Bullet.Lifetime[2]) then
self.Lifetime = CurTime() + self.Bullet.Lifetime[1]
else
self.Lifetime = CurTime() + math.Rand(self.Bullet.Lifetime[1],self.Bullet.Lifetime[2])
end
end
end
-- Trail
if (self.Bullet.Trail) then
local trail = self.Bullet.Trail
util.SpriteTrail( self.Entity, 0, trail.Color, false, trail.StartSize, trail.EndSize, trail.Length, 1/(trail.StartSize+trail.EndSize)*0.5, trail.Texture )
end
-- Material
if (self.Bullet.Material) then
self.Entity:SetMaterial( self.Bullet.Material )
end
-- Color
if (self.Bullet.Color) then
local C = self.Bullet.Color
self.Entity:SetColor( C.r, C.g, C.b, C.a or 255 )
end
end
function BULLET:Explode( self, trace )
self.ThrusterSound:Stop()
-- Stuff copied from the entity's explode function
-------------------------------------------------------------
-- Effects
if (self.Bullet.ExplosionEffect) then
local effectdata = EffectData()
effectdata:SetOrigin( trace.HitPos + trace.HitNormal * 5 )
effectdata:SetStart( trace.HitPos + trace.HitNormal * 5 )
effectdata:SetNormal( trace.HitNormal )
util.Effect( self.Bullet.ExplosionEffect, effectdata )
end
-- Sounds
if (self.Bullet.ExplosionSound) then
local soundpath = ""
if (table.Count(self.Bullet.ExplosionSound) > 1) then
soundpath = table.Random(self.Bullet.ExplosionSound)
else
soundpath = self.Bullet.ExplosionSound[1]
end
WorldSound( soundpath, trace.HitPos+trace.HitNormal*5,100,100)
end
-- Damage
local damagetype = self.Bullet.DamageType
if (damagetype and type(damagetype) == "string") then
if (damagetype == "BlastDamage") then
if (trace.Entity and trace.Entity:IsValid()) then
pewpew:PointDamage( trace.Entity, self.Bullet.Damage, self )
pewpew:BlastDamage( trace.HitPos, self.Bullet.Radius, self.Bullet.Damage, self.Bullet.RangeDamageMul, trace.Entity, self )
else
pewpew:BlastDamage( trace.HitPos, self.Bullet.Radius, self.Bullet.Damage, self.Bullet.RangeDamageMul, self )
end
-- Player Damage
if (self.Bullet.PlayerDamageRadius and self.Bullet.PlayerDamage and pewpew.Damage) then
util.BlastDamage( self.Entity, self.Entity, trace.HitPos + trace.HitNormal * 10, self.Bullet.PlayerDamageRadius, self.Bullet.PlayerDamage )
end
elseif (damagetype == "PointDamage") then
pewpew:PointDamage( trace.Entity, self.Bullet.Damage, self )
elseif (damagetype == "SliceDamage") then
pewpew:SliceDamage( trace.HitPos, self.FlightDirection, self.Bullet.Damage, self.Bullet.NumberOfSlices or 1, self.Bullet.SliceDistance or 50, self.Bullet.ReducedDamagePerSlice or 0, self )
elseif (damagetype == "EMPDamage") then
pewpew:EMPDamage( trace.HitPos, self.Bullet.Radius, self.Bullet.Duration )
elseif (damagetyp == "DefenseDamage") then
pewpew:DefenseDamage( trace.Entity, self.Bullet.Damage )
end
end
-- Remove the bullet
self.Entity:Remove()
end[/lua]
Stack overflow at line 157, hm...
[lua]
-- Homing Missile
local BULLET = {}
-- General Information
BULLET.Name = "Homing Missile - Test"
BULLET.Author = "Geo"
BULLET.Description = "Homing missiles Test."
BULLET.AdminOnly = false
BULLET.SuperAdminOnly = true
-- Appearance
BULLET.Model = "models/aamissile.mdl"
-- BULLET.Material = "phoenix_storms/gear"
BULLET.Color = Color(255, 255, 255 )
BULLET.Trail = { StartSize = 20,
EndSize = 0,
Length = 2,
Texture = "trails/laser.vmt",
Color = Color(50,100,255, 255 ) }
-- Effects / Sounds
BULLET.FireSound = {"arty/rocket.wav"}
BULLET.ExplosionSound = {"weapons/explode3.wav","weapons/explode4.wav","weapons/explode5.wav"}
BULLET.FireEffect = "muzzleflash"
BULLET.ExplosionEffect = "Enersplosion"
-- Movement
BULLET.Speed = 25
BULLET.Gravity = nil
BULLET.RecoilForce = 5000
BULLET.Spread = 4
-- Damage
BULLET.DamageType = "BlastDamage"
BULLET.Damage = 800
BULLET.Radius = 325
BULLET.RangeDamageMul = 0.5
BULLET.NumberOfSlices = nil
BULLET.SliceDistance = nil
BULLET.PlayerDamage = 350
BULLET.PlayerDamageRadius = 400
-- Reloading/Ammo
BULLET.Reloadtime = 0.1
BULLET.Ammo = 3
BULLET.AmmoReloadtime = 6
BULLET.Lifetime = {40,6}
BULLET.ExplodeAfterDeath = true
BULLET.EnergyPerShot = 4800
BULLET.CustomInputs = { "Fire", "X", "Y", "Z", "XYZ [VECTOR]"}
-- Custom Functions
-- (If you set the override var to true, the cannon/bullet will run these instead. Use these functions to do stuff which is not possible with the above variables)
-- Wire Input (This is called whenever a wire input is changed)
BULLET.WireInputOverride = true
function BULLET:WireInput( self, inputname, value )
if (inputname == "Fire") then
if (value != 0) then
self.Firing = true
else
self.Firing = false
end
if (value != 0 and self.CanFire == true) then
self.LastFired = CurTime()
self.CanFire = false
WireLib.TriggerOutput(self.Entity, "Can Fire", 0)
self:FireBullet()
end
elseif (inputname == "X") then
if (!self.TargetPos) then self.TargetPos = Vector(0,0,0) end
self.TargetPos.x = value
elseif (inputname == "Y") then
if (!self.TargetPos) then self.TargetPos = Vector(0,0,0) end
self.TargetPos.y = value
elseif (inputname == "Z") then
if (!self.TargetPos) then self.TargetPos = Vector(0,0,0) end
self.TargetPos.z = value
elseif (inputname == "XYZ") then
self.TargetPos = value
end
end
local SoundName = Sound( "thrusters/hover01.wav" )
-- Initialize (Is called when the bullet initializes)
BULLET.InitializeOverride = true
function BULLET:InitializeFunc( self )
self.ThrusterSound = CreateSound( self, SoundName )
self.ThrusterSound:Play()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_NONE )
self.Entity:SetSolid( SOLID_NONE )
self.FlightDirection = self.Entity:GetUp()
self.TargetDir = self.Entity:GetUp()
if (self.Cannon:IsValid()) then
if (self.Cannon.TargetPos and self.Cannon.TargetPos != Vector(0,0,0)) then
self.TargetDir = (self.Cannon.TargetPos-self:GetPos()):GetNormalized()
end
end
self.Exploded = false
self.TraceDelay = CurTime() + self.Bullet.Speed / 1000 / 4
-- Lifetime
self.Lifetime = false
if (self.Bullet.Lifetime) then
if (self.Bullet.Lifetime[1] > 0 and self.Bullet.Lifetime[2] > 0) then
if (self.Bullet.Lifetime[1] == self.Bullet.Lifetime[2]) then
self.Lifetime = CurTime() + self.Bullet.Lifetime[1]
else
self.Lifetime = CurTime() + math.Rand(self.Bullet.Lifetime[1],self.Bullet.Lifetime[2])
end
end
end
-- Trail
if (self.Bullet.Trail) then
local trail = self.Bullet.Trail
util.SpriteTrail( self.Entity, 0, trail.Color, false, trail.StartSize, trail.EndSize, trail.Length, 1/(trail.StartSize+trail.EndSize)*0.5, trail.Texture )
end
-- Material
if (self.Bullet.Material) then
self.Entity:SetMaterial( self.Bullet.Material )
end
-- Color
if (self.Bullet.Color) then
local C = self.Bullet.Color
self.Entity:SetColor( C.r, C.g, C.b, 255 )
end
end
function BULLET:Explode( self, trace )
self.ThrusterSound:Stop()
end
-- Think
BULLET.ThinkOverride = true
function BULLET:ThinkFunc( self )
-- Make it fly
self.Entity:SetPos( self.Entity:GetPos() + self.FlightDirection * self.Bullet.Speed )
if (self.TargetPos != Vector(0,0,0)) then
self.FlightDirection = self.FlightDirection + (self.TargetDir-self.FlightDirection) / 20
self.FlightDirection = self.FlightDirection:GetNormalized()
end
if (self.Cannon:IsValid()) then
if (self.Cannon.TargetPos and self.Cannon.TargetPos != Vector(0,0,0)) then
self.TargetDir = (self.Cannon.TargetPos-self:GetPos()):GetNormalized()
end
end
self.Entity:SetAngles( self.FlightDirection:Angle() + Angle(90,0,0) )
-- Lifetime
if (self.Lifetime) then
if (CurTime() > self.Lifetime) then
if (self.Bullet.ExplodeAfterDeath) then
local tr = {}
tr.start = self.Entity:GetPos()-self.FlightDirection
tr.endpos = self.Entity:GetPos()
tr.filter = self.Entity
local trace = util.TraceLine( tr )
self:Explode( trace )
else
self.Entity:Remove()
end
end
end
if (CurTime() > self.TraceDelay) then
-- Check if it hit something
local tr = {}
tr.start = self.Entity:GetPos() - self.FlightDirection * self.Bullet.Speed
tr.endpos = self.Entity:GetPos()
tr.filter = self.Entity
local trace = util.TraceLine( tr )
if (trace.Hit and !self.Exploded) then
self.Exploded = true
self:Explode( trace )
else
-- Run more often!
self.Entity:NextThink( CurTime() )
return true
end
else
-- Run more often!
self.Entity:NextThink( CurTime() )
return true
end
end
pewpew:AddBullet( BULLET )
[/lua]
You haven't changed anything in the think function, no need to override it.
Your bullet won't explode now, because you've overwritten the explode function with a function that only stops the sound.
Dont know what can i do now.
I tried 100 version, but now when i use:
local SoundName = Sound( "thrusters/hover01.wav" )
or
self.SoundName = Sound( "thrusters/hover01.wav" )
i get error message: stack overflow :(
I already gave you a fully working code (untested, sure, but it should work just fine), you just failed to use it correctly (Although how someone can fail to copy paste a code correctly beats me).
Don't know what more I can do.
Working!!!:D Thank you very much! And sorry for...you know.
Here is the lua:
[lua]
-- Homing Missile
local BULLET = {}
local SoundName = Sound( "thrusters/hover01.wav" )
-- General Information
BULLET.Name = "Homing Missile - Geo"
BULLET.Author = "Geo"
BULLET.Description = "Homing missilesv1."
BULLET.AdminOnly = false
BULLET.SuperAdminOnly = true
-- Appearance
BULLET.Model = "models/aamissile.mdl"
-- BULLET.Material = "phoenix_storms/gear"
BULLET.Color = Color(255, 255, 255 )
BULLET.Trail = { StartSize = 20,
EndSize = 0,
Length = 2,
Texture = "trails/laser.vmt",
Color = Color(50,100,255, 255 ) }
-- Effects / Sounds
BULLET.FireSound = {"arty/rocket.wav"}
BULLET.ExplosionSound = {"weapons/explode3.wav","weapons/explode4.wav","weapons/explode5.wav"}
BULLET.FireEffect = "muzzleflash"
BULLET.ExplosionEffect = "Enersplosion"
-- Movement
BULLET.Speed = 25
BULLET.Gravity = nil
BULLET.RecoilForce = 5000
BULLET.Spread = 4
-- Damage
BULLET.DamageType = "BlastDamage"
BULLET.Damage = 800
BULLET.Radius = 325
BULLET.RangeDamageMul = 0.5
BULLET.NumberOfSlices = nil
BULLET.SliceDistance = nil
BULLET.PlayerDamage = 350
BULLET.PlayerDamageRadius = 400
-- Reloading/Ammo
BULLET.Reloadtime = 0.1
BULLET.Ammo = 3
BULLET.AmmoReloadtime = 6
BULLET.Lifetime = {40,6}
BULLET.ExplodeAfterDeath = true
BULLET.EnergyPerShot = 4800
BULLET.CustomInputs = { "Fire", "X", "Y", "Z", "XYZ [VECTOR]"}
-- Custom Functions
-- (If you set the override var to true, the cannon/bullet will run these instead. Use these functions to do stuff which is not possible with the above variables)
-- Wire Input (This is called whenever a wire input is changed)
BULLET.WireInputOverride = true
function BULLET:WireInput( self, inputname, value )
if (inputname == "Fire") then
if (value != 0) then
self.Firing = true
else
self.Firing = false
end
if (value != 0 and self.CanFire == true) then
self.LastFired = CurTime()
self.CanFire = false
WireLib.TriggerOutput(self.Entity, "Can Fire", 0)
self:FireBullet()
end
elseif (inputname == "X") then
if (!self.TargetPos) then self.TargetPos = Vector(0,0,0) end
self.TargetPos.x = value
elseif (inputname == "Y") then
if (!self.TargetPos) then self.TargetPos = Vector(0,0,0) end
self.TargetPos.y = value
elseif (inputname == "Z") then
if (!self.TargetPos) then self.TargetPos = Vector(0,0,0) end
self.TargetPos.z = value
elseif (inputname == "XYZ") then
self.TargetPos = value
end
end
-- Initialize (Is called when the bullet initializes)
BULLET.InitializeOverride = true
function BULLET:InitializeFunc( self )
self.ThrusterSound = CreateSound( self, SoundName )
self.ThrusterSound:Play()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_NONE )
self.Entity:SetSolid( SOLID_NONE )
self.FlightDirection = self.Entity:GetUp()
self.TargetDir = self.Entity:GetUp()
if (self.Cannon:IsValid()) then
if (self.Cannon.TargetPos and self.Cannon.TargetPos != Vector(0,0,0)) then
self.TargetDir = (self.Cannon.TargetPos-self:GetPos()):GetNormalized()
end
end
self.Exploded = false
self.TraceDelay = CurTime() + self.Bullet.Speed / 1000 / 4
-- Lifetime
self.Lifetime = false
if (self.Bullet.Lifetime) then
if (self.Bullet.Lifetime[1] > 0 and self.Bullet.Lifetime[2] > 0) then
if (self.Bullet.Lifetime[1] == self.Bullet.Lifetime[2]) then
self.Lifetime = CurTime() + self.Bullet.Lifetime[1]
else
self.Lifetime = CurTime() + math.Rand(self.Bullet.Lifetime[1],self.Bullet.Lifetime[2])
end
end
end
-- Trail
if (self.Bullet.Trail) then
local trail = self.Bullet.Trail
util.SpriteTrail( self.Entity, 0, trail.Color, false, trail.StartSize, trail.EndSize, trail.Length, 1/(trail.StartSize+trail.EndSize)*0.5, trail.Texture )
end
-- Material
if (self.Bullet.Material) then
self.Entity:SetMaterial( self.Bullet.Material )
end
-- Color
if (self.Bullet.Color) then
local C = self.Bullet.Color
self.Entity:SetColor( C.r, C.g, C.b, 255 )
end
end
-- Think
BULLET.ThinkOverride = true
function BULLET:ThinkFunc( self )
-- Make it fly
self.Entity:SetPos( self.Entity:GetPos() + self.FlightDirection * self.Bullet.Speed )
if (self.TargetPos != Vector(0,0,0)) then
self.FlightDirection = self.FlightDirection + (self.TargetDir-self.FlightDirection) / 20
self.FlightDirection = self.FlightDirection:GetNormalized()
end
if (self.Cannon:IsValid()) then
if (self.Cannon.TargetPos and self.Cannon.TargetPos != Vector(0,0,0)) then
self.TargetDir = (self.Cannon.TargetPos-self:GetPos()):GetNormalized()
end
end
self.Entity:SetAngles( self.FlightDirection:Angle() + Angle(90,0,0) )
-- Lifetime
if (self.Lifetime) then
if (CurTime() > self.Lifetime) then
if (self.Bullet.ExplodeAfterDeath) then
local tr = {}
tr.start = self.Entity:GetPos()-self.FlightDirection
tr.endpos = self.Entity:GetPos()
tr.filter = self.Entity
local trace = util.TraceLine( tr )
self:Explode( trace )
else
self.Entity:Remove()
end
end
end
if (CurTime() > self.TraceDelay) then
-- Check if it hit something
local tr = {}
tr.start = self.Entity:GetPos() - self.FlightDirection * self.Bullet.Speed
tr.endpos = self.Entity:GetPos()
tr.filter = self.Entity
local trace = util.TraceLine( tr )
if (trace.Hit and !self.Exploded) then
self.Exploded = true
self:Explode( trace )
self.ThrusterSound:Stop()
else
-- Run more often!
self.Entity:NextThink( CurTime() )
return true
end
else
-- Run more often!
self.Entity:NextThink( CurTime() )
return true
end
end
pewpew:AddBullet( BULLET )
[/lua]
You're still not doing it correctly. But if it works..
I said, yesterday started to "learn" lua, so i need more time to understand.
So we can't do this with emitsound? Because the CreateSound don't have amplifier.
Any idea how can i make this sound more louder?