• How to make a prop inflict damage on a player.
    25 replies, posted
I'm making a SWEP that throws a prop, I want to know how to make that prop inflict x amount of damage to a player or npc once thrown. How would I do that? Thanks! Code: [CODE]function SWEP:Deploy() self:EmitSound( DeploySound ) end function SWEP:PrimaryAttack() self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 ) self:ThrowChair( "models/props/luffy/fist/fist.mdl" ) end -- -- Called when the rightmouse button is pressed -- function SWEP:SecondaryAttack() -- Note we don't call SetNextSecondaryFire here because it's not -- automatic and so we let them fire as fast as they can click. -- Call 'ThrowChair' on self with this model self:ThrowChair( "models/props/luffy/fist/fist.mdl" ) end -- -- A custom function we added. When you call this the player will fire a chair! -- function SWEP:ThrowChair( model_file ) -- -- Play the shoot sound we precached earlier! -- self:EmitSound( ShootSound ) -- -- If we're the client then this is as much as we want to do. -- We play the sound above on the client due to prediction. -- ( if we didn't they would feel a ping delay during multiplayer ) -- if ( CLIENT ) then return end -- -- Create a prop_physics entity -- local ent = ents.Create( "prop_physics" ) -- -- Always make sure that created entities are actually created! -- if ( !IsValid( ent ) ) then return end -- -- Set the entity's model to the passed in model -- local trail = util.SpriteTrail( ent,0,Color(255,163,128,50) ,false, 15, 15, 2.5, 0.00625,"trails/arm.vmt") ent:SetModel( model_file ) -- -- Set the position to the player's eye position plus 16 units forward. -- Set the angles to the player'e eye angles. Then spawn it. -- ent:SetPos( self.Owner:EyePos() + AngleRand():Forward() * 16 ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() -- -- Now get the physics object. Whenever we get a physics object -- we need to test to make sure its valid before using it. -- If it isn't then we'll remove the entity. -- local phys = ent:GetPhysicsObject() if ( !IsValid( phys ) ) then ent:Remove() return end -- -- Now we apply the force - so the chair actually throws instead -- of just falling to the ground. You can play with this value here -- to adjust how fast we throw it. -- local velocity = self.Owner:GetAimVector() velocity = velocity * 100000 velocity = velocity + ( VectorRand() * 1000 ) -- a random element phys:ApplyForceCenter( velocity ) timer.Simple(2.5, function() if ent:IsValid() then ent:Remove() end end) -- -- Assuming we're playing in Sandbox mode we want to add this -- entity to the cleanup and undo lists. This is done like so. -- cleanup.Add( self.Owner, "props", ent ) undo.Create( "Thrown_punch" ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end[/CODE]
I think you'd have to create a new [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityTakeDamage]GM:EntityTakeDamage[/url], and check if the attacking entity was the chair created, and if so, change the damage accordingly
[QUOTE=MPan1;50342682]I think you'd have to create a new [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityTakeDamage]GM:EntityTakeDamage[/url], and check if the attacking entity was the chair created, and if so, change the damage accordingly[/QUOTE] I didn't work, I also added some code at the top that was supposed to make it so I didn't take bullet damage. I didn't work either. Note that my prop's mass in the qc file is 10. changed code: [CODE] function SWEP:ThrowChair( model_file ) -- -- Play the shoot sound we precached earlier! -- self:EmitSound( ShootSound ) -- -- If we're the client then this is as much as we want to do. -- We play the sound above on the client due to prediction. -- ( if we didn't they would feel a ping delay during multiplayer ) -- if ( CLIENT ) then return end -- -- Create a prop_physics entity -- local ent = ents.Create( "prop_physics" ) -- -- Always make sure that created entities are actually created! -- if ( !IsValid( ent ) ) then return end -- -- Set the entity's model to the passed in model -- local trail = util.SpriteTrail( ent,0,Color(255,163,128,50) ,false, 15, 15, 2.5, 0.00625,"trails/arm.vmt") function EntityTakeDamage( target, dmginfo ) if ( target:IsNPC() and dmginfo:IsDamageType( 1 ) ) then dmginfo:SetDamage( 100 ) end end ent:SetModel( model_file ) -- -- Set the position to the player's eye position plus 16 units forward. -- Set the angles to the player'e eye angles. Then spawn it. -- ent:SetPos( self.Owner:EyePos() + AngleRand():Forward() * 16 ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() -- -- Now get the physics object. Whenever we get a physics object -- we need to test to make sure its valid before using it. -- If it isn't then we'll remove the entity. -- local phys = ent:GetPhysicsObject() if ( !IsValid( phys ) ) then ent:Remove() return end -- -- Now we apply the force - so the chair actually throws instead -- of just falling to the ground. You can play with this value here -- to adjust how fast we throw it. -- local velocity = self.Owner:GetAimVector() velocity = velocity * 10000 velocity = velocity + ( VectorRand() * 100 ) -- a random element phys:ApplyForceCenter( velocity ) timer.Simple(2.5, function() if ent:IsValid() then ent:Remove() end end) -- -- Assuming we're playing in Sandbox mode we want to add this -- entity to the cleanup and undo lists. This is done like so. -- cleanup.Add( self.Owner, "props", ent ) undo.Create( "Thrown_punch" ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end[/CODE] code at the top that was supposed to make it so I don't take bullet damage: [CODE] function EntityTakeDamage( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then dmginfo:SetDamage( 0 ) end end[/CODE]
That isn't a hook? Try this: [CODE] function SWEP:ThrowChair( model_file ) self:EmitSound( ShootSound ) if ( CLIENT ) then return end local ent = ents.Create( "prop_physics" ) if ( !IsValid( ent ) ) then return end local trail = util.SpriteTrail( ent,0,Color(255,163,128,50) ,false, 15, 15, 2.5, 0.00625,"trails/arm.vmt") ent:SetModel( model_file ) ent:SetPos( self.Owner:EyePos() + AngleRand():Forward() * 16 ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() local phys = ent:GetPhysicsObject() if ( !IsValid( phys ) ) then ent:Remove() return end hook.Add( 'EntityTakeDamage', ent, function( target, dmginfo ) -- the hook if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then dmginfo:SetDamage( 0 ) end end ) local velocity = self.Owner:GetAimVector() velocity = velocity * 100000 velocity = velocity + ( VectorRand() * 1000 ) -- a random element phys:ApplyForceCenter( velocity ) timer.Simple(2.5, function() if ent:IsValid() then ent:Remove() end end) cleanup.Add( self.Owner, "props", ent ) undo.Create( "Thrown_punch" ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end [/CODE] Remember, if you wanted to do certain damage involving the newly spawned chair entity, just use 'ent' in the hook, since that's the variable you set it as
Not positive this will trigger the [url="http://wiki.garrysmod.com/page/GM/EntityFireBullets"]EntityFireBullets[/url] hook, but you can give it a shot. Also look into modifying [url="http://wiki.garrysmod.com/page/Structures/Bullet"]this[/url]
[QUOTE=Derek_SM;50350546]Not positive this will trigger the [url="http://wiki.garrysmod.com/page/GM/EntityFireBullets"]EntityFireBullets[/url] hook, but you can give it a shot. Also look into modifying [url="http://wiki.garrysmod.com/page/Structures/Bullet"]this[/url][/QUOTE] Wouldn't that only work if he's using the aptly named [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/FireBullets]Entity:FireBullets[/url] somewhere in his code? Anyway, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/AddCallback]Entity:AddCallback[/url] in combination with the PhysicsCollide hook. In the hook, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/DamageInfo]DamageInfo[/url] to create a new DamageInfo object. Then, configure your DamageInfo object using functions from [url=http://wiki.garrysmod.com/page/Category:CTakeDamageInfo]CTakeDamageInfo[/url]. After you're done, call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/TakeDamageInfo]Entity:TakeDamageInfo[/url] with your DamageInfo object as the only argument. You can remove the chair entity if you like after this.
[QUOTE=AK to Spray;50350811]Wouldn't that only work if he's using the aptly named [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/FireBullets]Entity:FireBullets[/url] somewhere in his code? Anyway, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/AddCallback]Entity:AddCallback[/url] in combination with the PhysicsCollide hook. In the hook, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/DamageInfo]DamageInfo[/url] to create a new DamageInfo object. Then, configure your DamageInfo object using functions from [url=http://wiki.garrysmod.com/page/Category:CTakeDamageInfo]CTakeDamageInfo[/url]. After you're done, call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/TakeDamageInfo]Entity:TakeDamageInfo[/url] with your DamageInfo object as the only argument. You can remove the chair entity if you like after this.[/QUOTE] Sorry I'm not completely sure what you mean. I read all the articles you gave me I just don't know to use it.
[QUOTE=Lord_Melvin;50352506]Sorry I'm not completely sure what you mean. I read all the articles you gave me I just don't know to use it.[/QUOTE] What exactly don't you understand? Is it the callback, the arguments, the use of DamageInfo? Be more specific and I'll try to help you out. I'm just trying to avoid caving in and doing this for you.
[QUOTE=AK to Spray;50352520]What exactly don't you understand? Is it the callback, the arguments, the use of DamageInfo? Be more specific and I'll try to help you out. I'm just trying to avoid caving in and doing this for you.[/QUOTE] I'm not sure what your giving me directions for. editing this?: [CODE] hook.Add( 'EntityTakeDamage', ent, function( target, dmginfo ) -- the hook if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then dmginfo:SetDamage( 0 ) end end )[CODE/] or making a new hook.
[QUOTE=Lord_Melvin;50352965]I'm not sure what your giving me directions for. editing this?: [CODE] hook.Add( 'EntityTakeDamage', ent, function( target, dmginfo ) -- the hook if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then dmginfo:SetDamage( 0 ) end end )[CODE/] or making a new hook.[/QUOTE] No, I'm trying to to tell you how to damage the player when the thrown prop collides with the player. Is this not what you wanted?
[QUOTE=AK to Spray;50353242]No, I'm trying to to tell you how to damage the player when the thrown prop collides with the player. Is this not what you wanted?[/QUOTE] Yes it is, sorry I got a little confused. [editline]20th May 2016[/editline] I'm going to abandon this for now. I know little to none about how hooks work and how to use them. I don't expect you to just do it for me, I just need to get more into lua before I do this more advanced stuff.
[QUOTE=AK to Spray;50350811]Wouldn't that only work if he's using the aptly named [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/FireBullets]Entity:FireBullets[/url] somewhere in his code? Anyway, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/AddCallback]Entity:AddCallback[/url] in combination with the PhysicsCollide hook. In the hook, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/DamageInfo]DamageInfo[/url] to create a new DamageInfo object. Then, configure your DamageInfo object using functions from [url=http://wiki.garrysmod.com/page/Category:CTakeDamageInfo]CTakeDamageInfo[/url]. After you're done, call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/TakeDamageInfo]Entity:TakeDamageInfo[/url] with your DamageInfo object as the only argument. You can remove the chair entity if you like after this.[/QUOTE] Ok, here is what I've got so far: [CODE]hook.Add( "PhysicsCollide" ) ent:AddCallback( "PhysicsCollide", SWEP:ThrowChair )[/CODE] Can you explain what I should do a little bit clearer for me to understand, someone who has never used a hook before. Thanks!
[QUOTE=Lord_Melvin;50361869]Ok, here is what I've got so far: [CODE]hook.Add( "PhysicsCollide" ) ent:AddCallback( "PhysicsCollide", SWEP:ThrowChair )[/CODE] Can you explain what I should do a little bit clearer for me to understand, someone who has never used a hook before. Thanks![/QUOTE] [url]http://wiki.garrysmod.com/page/Entity_Callbacks[/url] [url]http://wiki.garrysmod.com/page/Structures/CollisionData[/url] [lua] ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsPlayer() then local ply = data.HitEntity --- call shit here with ply variable end end ) [/lua]
[QUOTE=Derek_SM;50361935][url]http://wiki.garrysmod.com/page/Entity_Callbacks[/url] [url]http://wiki.garrysmod.com/page/Structures/CollisionData[/url] [lua] ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsPlayer() then local ply = data.HitEntity --- call shit here with ply variable end end ) [/lua][/QUOTE] So like this?: [CODE] ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:SetDamage(5) --- call shit here with ply variable end end )[/CODE] Sorry again, it gives me a nil value.
[QUOTE=Lord_Melvin;50371050]So like this?: [CODE] ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:SetDamage(5) --- call shit here with ply variable end end )[/CODE] Sorry again, it gives me a nil value.[/QUOTE] ply:TakeDamage(5) instead
[QUOTE=jorji;50371178]ply:TakeDamage(5) instead[/QUOTE] Ok, that works, but it still takes damage from the prop itself. How do I set the physical damage to 0 so it will only do 5 damage?
Can I set the physical damage to 0 with ent:TakePhysicsDamage? If so, how?
[QUOTE=Lord_Melvin;50373470]Can I set the physical damage to 0 with ent:TakePhysicsDamage? If so, how?[/QUOTE] OK, I tried this but it says that IsDamageType is returning a nil value [CODE]ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakeDamage(5) if ply:IsDamageType( DMG_CRUSH ) then ply:TakeDamage(0) end end end )[/CODE] [editline]22nd May 2016[/editline] [QUOTE=Lord_Melvin;50374572]OK, I tried this but it says that IsDamageType is returning a nil value [CODE]ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakeDamage(5) if ply:IsDamageType( DMG_CRUSH ) then ply:TakeDamage(0) end end end )[/CODE][/QUOTE] Sill having troubles, anyone? [CODE] ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakeDamage(5) ply:GetDamageType() if ply:IsDamageType( DMG_CRUSH ) then ply:TakeDamage(0) end end end )[/CODE]
[QUOTE=Lord_Melvin;50374572] [CODE] ent:AddCallback( "PhysicsCollide", function( ent, data ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakeDamage(5) ply:GetDamageType() if ply:IsDamageType( DMG_CRUSH ) then ply:TakeDamage(0) end end end )[/CODE][/QUOTE] IsDamageType isn't a valid function for the Player class. You need to do something like: [CODE]ent:AddCallback( "PhysicsCollide", function( data, collider ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakePhysicsDamage( 5 ) end end )[/CODE] Then hook [URL="http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage"]http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage[/URL], check if an NPC is being hit, then scale damage down if CTakeDamageInfo:IsDamageType( DMG_CRUSH )
[QUOTE=jorji;50375346]IsDamageType isn't a valid function for the Player class. You need to do something like: [CODE]ent:AddCallback( "PhysicsCollide", function( data, collider ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakePhysicsDamage( 5 ) end end )[/CODE] Then hook [URL="http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage"]http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage[/URL], check if an NPC is being hit, then scale damage down if CTakeDamageInfo:IsDamageType( DMG_CRUSH )[/QUOTE] So would something like this work? [CODE] ent:AddCallback( "PhysicsCollide", function( data, collider ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity if ply:IsDamageType( DMG_CRUSH ) then ply:TakePhysicsDamage( 0 ) end end end ) [/CODE]
I tried this but the NPC is still taking physical damage. Help? [CODE] ent:AddCallback( "PhysicsCollide", function( data, collider ) if IsValid( data.HitEntity ) and data.HitEntity:IsNPC() then local ply = data.HitEntity ply:TakeDamage(5) local dmginfo = DamageInfo() dmginfo:SetDamageType(DMG_CRUSH) function ply:OnTakeDamage( dmginfo ) ply:ScaleDamage( 0 ) end end end ) [/CODE]
Still having troubles :( npc's still take physical damage.
[QUOTE=Lord_Melvin;50432976]Still having troubles :( npc's still take physical damage.[/QUOTE] Wow long thread. That would be because you haven't added code to ignore the default prop damage. This needs to be done in GM:EntityTakeDamage. The damagetype from this is always going to be DMG_CRUSH. Or adjust the damage given from DMG_CRUSH to get the amount you want, and resolve the props owner with dmginfo:GetAttacker():GetOwner() or something so you know who threw it. You can make the damage even more accurate by combining the above (ignore dmg_crush damagetypes) and using a dummy entity instead of prop_physics that just has ENTITY:PhysicsCollide() on it. Or if you don't want to disable prop damage entirely and just have this specific thrown entity to do a certain amount of damage, combine the above (use an entity with custom physicscollide to deal the damage), and use code to make GM:EntityTakeDamage() ignore DMG_CRUSH if it's from that specific entity class. Or add a flag to the prop_physics entity that you spawn in the function so it gets passed to GM:EntityTakeDamage via the inflictor/attacker. ex. ent.IsThrownFromMyUberWeapon = true (And also ignore dmg_crush from the entity)
[QUOTE=Pyro-Fire;50438679]Wow long thread. That would be because you haven't added code to ignore the default prop damage. This needs to be done in GM:EntityTakeDamage. The damagetype from this is always going to be DMG_CRUSH. Or adjust the damage given from DMG_CRUSH to get the amount you want, and resolve the props owner with dmginfo:GetAttacker():GetOwner() or something so you know who threw it. You can make the damage even more accurate by combining the above (ignore dmg_crush damagetypes) and using a dummy entity instead of prop_physics that just has ENTITY:PhysicsCollide() on it. Or if you don't want to disable prop damage entirely and just have this specific thrown entity to do a certain amount of damage, combine the above (use an entity with custom physicscollide to deal the damage), and use code to make GM:EntityTakeDamage() ignore DMG_CRUSH if it's from that specific entity class. Or add a flag to the prop_physics entity that you spawn in the function so it gets passed to GM:EntityTakeDamage via the inflictor/attacker. ex. ent.IsThrownFromMyUberWeapon = true (And also ignore dmg_crush from the entity)[/QUOTE] Ok, I can kind of see what you're saying. It would be better if you had more examples though, for some reason it's hard for me to grasp this concept. Anyway here is what I have so far, I'm not able to test it out right now because I have something downloading. I'm trying to make it so when the prop that is being thrown from the gun will hit a NPC and change the physical damage to 0. Code: [CODE] function GM:EntityTakeDamage( target, dmginfo) if( target:IsNPC() and dmginfo:IsDamageType( 1 )) // 1 = DMG_CRUSH correct? Or do I put DMG_CRUSH? dmginfo:ScaleDamage( 0 ) end end[/CODE]
Yes something like that. And in general it's better to use the enums for anything. [lua] if( dmginfo:IsDamageType( DMG_CRUSH ) ) then dmginfo:SetDamage( 0 ) end [/lua]
FINALLY!! GOD DAMN THAT TOOK SO LONG!!!! For anyone else who is having this problem or just wants to know how to do it here is my code: [CODE] function EntityTakeDamage( target, dmginfo) if( target:IsNPC() and dmginfo:IsDamageType( DMG_CRUSH )) then dmginfo:SetDamage( 0 ) target:TakeDamage(100) end end hook.Add("EntityTakeDamage", "Get Damage", EntityTakeDamage) [/CODE]
Sorry, you need to Log In to post a reply to this thread.