• I am trying to disolve some entities but it never works
    9 replies, posted
I have this block of code that runs when the player dies and spawns a bunch of gibs. It is supposed to fizzle them after spawning them but never works! What am I doing wrong???? CODE: [LUA] local gibs = { "models/player/alpha/gib/alpha_gib_leftleglower.mdl", "models/player/alpha/gib/alpha_gib_rightleglower.mdl", "models/player/alpha/gib/alpha_gib_leftlegupper.mdl", "models/player/alpha/gib/alpha_gib_rightlegupper.mdl", "models/player/alpha/gib/alpha_gib_waist.mdl", "models/player/alpha/gib/alpha_gib_tummy.mdl", "models/player/alpha/gib/alpha_gib_body.mdl", "models/player/alpha/gib/alpha_gib_head.mdl" } function GM:PlayerDeath( victim ) local velocity = victim:GetVelocity()/2 victim:GetRagdollEntity():Remove() local explode = ents.Create( "env_explosion" ) explode:SetPos( victim:GetPos() ) explode:SetOwner( victim ) explode:Spawn() explode:SetKeyValue( "iMagnitude", "50" ) explode:Fire( "Explode", 0, 0 ) explode:EmitSound( "npc/env_headcrabcanister/explosion.wav", 100, 100 ) local gibsspawned = {} local count = #gibs local pos = victim:GetPos() pos.z = pos.z + 16 for i = 1, count do local gib = ents.Create( "prop_physics" ) gib:SetModel( gibs[i] ) gib:SetPos( pos ) gib:SetAngles( Angle( math.random( -7, 7 ), math.random( -7, 7 ), math.random( -7, 7 ) ) ) gib:SetCollisionGroup( COLLISION_GROUP_DEBRIS ) gib:SetColor( team.GetColor( victim:Team() ) ) gib:SetKeyValue( "targetname", "gibdis" .. i .. math.random( 0, 1000 ) ) gib:Spawn() gib:Activate() gib:GetPhysicsObject():SetVelocity( velocity ) gibsspawned[i] = gib end local dissolver = ents.Create( "env_entity_dissolver" ) dissolver:SetPos( victim:GetPos() ) dissolver:Spawn() dissolver:Activate() dissolver:SetKeyValue( "magnitude", 100 ) dissolver:SetKeyValue( "dissolvetype", 0 ) timer.Simple( 0.01, function() if ( dissolver:IsValid() ) then for i = 1, count do local gib = gibsspawned[i] if ( gib:IsValid() ) then dissolver:Fire( "Dissolve", gib:GetName() ) end end dissolver:Remove() else for i = 1, count do local gib = gibsspawned[i] if ( gib:IsValid() ) then gib:Remove() end end end end ) end [/LUA] If you want to test it replace the gib array with this code: [LUA] local gibs = { "models/props_junk/PropaneCanister001a.mdl", "models/props_junk/propane_tank001a.mdl", "models/props_junk/gascan001a.mdl", "models/props_junk/gascan001a.mdl", "models/props_junk/CinderBlock01a.mdl", "models/props_junk/TrafficCone001a.mdl", "models/props_combine/breenglobe.mdl", "models/props_junk/PlasticCrate01a.mdl" } [/LUA] It switches the gibs to some random props. THANKZ FOR HELPING ME
Try printing out the variable gibsspawned and gib, and see what happens after death. My assumption, since the code doesn't seem bad, is that gibsspawned isn't populating.
Already checked everything. The code works completely and all the gibs are put into the array and processed. I checked it and they are all taken care of and set to dissolve, but fireing dissolve on the disolver does nothing even when all the gibs are setup and are set as the target correctly.
did you check if gib:GetName() actually returns the targetname keyvalue you set?
Checked it just now and it is working fine and all the names are correct.
perhaps the timer is too short? what if you increase the delay to one full second? [editline]17th September 2016[/editline] try gib:SetName(name) instead of gib:SetKeyValue("targetname",name)
I will try it now [editline]17th September 2016[/editline] Both changes effected nothing. Gibs still do not dissolve and still have their names correct when having print( gib:GetName() ) before dissolver:Fire() [editline]17th September 2016[/editline] Current code if you want to follow along(Doesn't work) [LUA] local gibs = { "models/player/alpha/gib/alpha_gib_leftleglower.mdl", "models/player/alpha/gib/alpha_gib_rightleglower.mdl", "models/player/alpha/gib/alpha_gib_leftlegupper.mdl", "models/player/alpha/gib/alpha_gib_rightlegupper.mdl", "models/player/alpha/gib/alpha_gib_waist.mdl", "models/player/alpha/gib/alpha_gib_tummy.mdl", "models/player/alpha/gib/alpha_gib_body.mdl", "models/player/alpha/gib/alpha_gib_head.mdl" } function GM:PlayerDeath( victim ) local velocity = victim:GetVelocity()/2 victim:GetRagdollEntity():Remove() local explode = ents.Create( "env_explosion" ) explode:SetPos( victim:GetPos() ) explode:SetOwner( victim ) explode:Spawn() explode:SetKeyValue( "iMagnitude", "50" ) explode:Fire( "Explode", 0, 0 ) explode:EmitSound( "npc/env_headcrabcanister/explosion.wav", 100, 100 ) local gibsspawned = {} local count = #gibs local pos = victim:GetPos() pos.z = pos.z + 16 for i = 1, count do local gib = ents.Create( "prop_physics" ) gib:SetModel( gibs[i] ) gib:SetPos( pos ) gib:SetAngles( Angle( math.random( -7, 7 ), math.random( -7, 7 ), math.random( -7, 7 ) ) ) gib:SetCollisionGroup( COLLISION_GROUP_DEBRIS ) gib:SetColor( team.GetColor( victim:Team() ) ) gib:SetName( "gibdis" .. i .. math.random( 0, 1000 ) ) gib:Spawn() gib:Activate() gib:GetPhysicsObject():SetVelocity( velocity ) gibsspawned[i] = gib end local dissolver = ents.Create( "env_entity_dissolver" ) dissolver:SetPos( victim:GetPos() ) dissolver:SetKeyValue( "magnitude", 100 ) dissolver:SetKeyValue( "dissolvetype", 0 ) dissolver:SetName( "disgib" .. math.random( 0, 1000 ) ) dissolver:Spawn() dissolver:Activate() timer.Simple( 1, function() if ( dissolver:IsValid() ) then for i = 1, count do local gib = gibsspawned[i] if ( gib:IsValid() ) then dissolver:Fire( "Dissolve", gib:GetName() ) end end dissolver:Remove() else for i = 1, count do local gib = gibsspawned[i] if ( gib:IsValid() ) then gib:Remove() end end end end ) end [/LUA]
Bumpidy bumping
After editing, works on my end. The problem was the dissolver got removed before it could dissolve anything, its fixed now. Also, used hook instead of overiding the function [code]if SERVER then local gibmodels = { "models/player/alpha/gib/alpha_gib_leftleglower.mdl", "models/player/alpha/gib/alpha_gib_rightleglower.mdl", "models/player/alpha/gib/alpha_gib_leftlegupper.mdl", "models/player/alpha/gib/alpha_gib_rightlegupper.mdl", "models/player/alpha/gib/alpha_gib_waist.mdl", "models/player/alpha/gib/alpha_gib_tummy.mdl", "models/player/alpha/gib/alpha_gib_body.mdl", "models/player/alpha/gib/alpha_gib_head.mdl", } hook.Add("PlayerDeath", "PlyDeathGibModelsFunc", function(victim) local velocity = victim:GetVelocity()/2 victim:GetRagdollEntity():Remove() local explode = ents.Create( "env_explosion" ) explode:SetPos( victim:GetPos() ) explode:SetOwner( victim ) explode:Spawn() explode:SetKeyValue( "iMagnitude", "50" ) explode:Fire( "Explode", 0, 0 ) explode:EmitSound( "npc/env_headcrabcanister/explosion.wav", 100, 100 ) local gibsspawned = {} local count = #gibmodels local pos = victim:GetPos() pos.z = pos.z + 16 for i = 1, count do local gib = ents.Create( "prop_physics" ) gib:SetModel( gibmodels[i] ) gib:SetPos( pos ) gib:SetAngles( Angle( math.random( -7, 7 ), math.random( -7, 7 ), math.random( -7, 7 ) ) ) gib:SetCollisionGroup( COLLISION_GROUP_DEBRIS ) gib:SetColor( team.GetColor( victim:Team() ) ) gib:SetName( "gibdis" .. gib:EntIndex() ) gib:Spawn() gib:Activate() local gibphys = gib:GetPhysicsObject() if IsValid(gibphys) then gibphys:SetVelocity(velocity) end gibsspawned[i] = gib end local dissolver = ents.Create( "env_entity_dissolver" ) dissolver:SetPos( victim:GetPos() ) dissolver:SetKeyValue( "magnitude", 100 ) dissolver:SetKeyValue( "dissolvetype", 0 ) dissolver:SetName( "disgib" .. dissolver:EntIndex() ) dissolver:Spawn() dissolver:Activate() timer.Simple( 1, function() if (IsValid(dissolver)) then for i = 1, count do local gib = gibsspawned[i] if ( IsValid(gib) ) then dissolver:Fire( "Dissolve", gib:GetName(), 0 ) end end dissolver:Fire( "Kill","", 1 ) -- removes dissolver 1 second after dissolving gibs end end) end) end[/code]
Thanks! The code works great and I have my fizzling gibs! Thank you all for your help and support!
Sorry, you need to Log In to post a reply to this thread.