• Alter bullet damage in callback
    3 replies, posted
I've been trying to make a weapon which does more damage at longer range, and less at shorter range. I've tried to implement this by setting the damage in a bullet structure's "Callback" function, like so: [lua]local bullet = {} -- setting bullet info like Src, Dir etc snipped... if SERVER then bullet.Callback = function(att, tr, dmginfo) if tr.Entity and not tr.Entity:IsWorld() then local len = tr.HitPos:Distance( tr.StartPos ) local scale = math.max( math.min( 1.5, len / 2048 ), 0.125 ) local dmg = math.ceil( self.Primary.Damage * scale ) print( "Shot Info {" .. len .. "," .. scale .. "," .. dmg .."}" ) dmginfo:SetDamage( dmg ) end end end self.Owner:FireBullets( bullet )[/lua] When testing, the info printed to the console is as expected (at short range the scale and dmg is low, where dmg is at least 13, and at long ranges dmg reaches 150). However, the actual damage done by the bullet is 100 at short range and 55 at long ranges, which is the opposite of what I wanted and the opposite of what I thought I had coded and the print suggests. Does the Callback in the bullet table only get called after the damage has been applied? If so, how else would I scale the bullet damage based on distance travelled?
[QUOTE=Ziks;35209555]I've been trying to make a weapon which does more damage at longer range, and less at shorter range. I've tried to implement this by setting the damage in a bullet structure's "Callback" function, like so: [lua]local bullet = {} -- setting bullet info like Src, Dir etc snipped... if SERVER then bullet.Callback = function(att, tr, dmginfo) if tr.Entity and not tr.Entity:IsWorld() then local len = tr.HitPos:Distance( tr.StartPos ) local scale = math.max( math.min( 1.5, len / 2048 ), 0.125 ) local dmg = math.ceil( self.Primary.Damage * scale ) print( "Shot Info {" .. len .. "," .. scale .. "," .. dmg .."}" ) dmginfo:SetDamage( dmg ) end end end self.Owner:FireBullets( bullet )[/lua] When testing, the info printed to the console is as expected (at short range the scale and dmg is low, where dmg is at least 13, and at long ranges dmg reaches 150). However, the actual damage done by the bullet is 100 at short range and 55 at long ranges, which is the opposite of what I wanted and the opposite of what I thought I had coded and the print suggests. Does the Callback in the bullet table only get called after the damage has been applied? If so, how else would I scale the bullet damage based on distance travelled?[/QUOTE] I notice that no one has answered this, so I thought I would give it a try! After thinking about this problem for a little bit, I came up with a few [I]possible[/I] solutions. Here is one hackey possibility... [B]Option 1[/B] In theory, you might be able to calculate the distance between the player and the aimvector and use that as a rough calculation of distance from a [I]possible[/I] target, if that makes sense. [lua] function SWEP:ShootBullet( damage, num_bullets, aimcone ) local bullet = {} // -snip- // So completely untested that its laughable. But I am sure there are some vector functions you // can call to derrive the projected location of the bullet. bullet.damage = self.Owner:GetShootPos():Distance( self.Owner:GetShootPos() + self.Owner:GetAimVector() ) * baseDamageModifier; // Note in my example - baseDamageModifier would be some custom variable for offsetting // damage by distance. [/lua] -note go here for more info on the shootbullet function- [URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index7161.html?title=SWEP.ShootBullet"]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index7161.html?title=SWEP.ShootBullet[/URL] I had a few other ideas but I will need to think about it more before I come up with anything remotely formal. Let me know what you think of my proposed example above though - I hope it helps! I will give this a bit more thought in the meantime =)
I ended up solving it by doing something along these lines: [lua]function SWEP.ScaleDamage( ply, hitgroup, dmginfo ) if not SERVER or not dmginfo:IsBulletDamage() then return end local att = dmginfo:GetAttacker() if not att or not att:IsPlayer() then return end local wep = att:GetActiveWeapon() if not wep or wep:GetClass() ~= "weapon_ttt_gaussrifle" then return end local len = ply:GetPos():Distance( att:GetPos() ) local scale = math.max( math.min( 8.0, ( len - 256 ) / 256 ), 0.125 ) dmginfo:ScaleDamage( scale ) end hook.Add( "ScalePlayerDamage", "GaussScaleDamage", SWEP.ScaleDamage )[/lua] Works fine. But thank you for the suggestion.
How would you add multiple guns to the function above pleaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaase
Sorry, you need to Log In to post a reply to this thread.