• Using a table with stats to modify shootbullet stats
    3 replies, posted
So I'm making a weapon base that is super simple, and I want to make bullet stat presets depending on the ammotype. [code] local AmmoStatMod = { [".22"] = {Recoil = 0.5, Num = 1, Force = 2, Damage = 10}, ["9mm"] = {Recoil = 1, Num = 1, Force = 2, Damage = 15}, [".357"] = {Recoil = 3, Num = 1, Force = 4, Damage = 30}, [".45"] = {Recoil = 1.5, Num = 1, Force = 3, Damage = 35}, ["50_BMG"] = {Recoil = 5, Num = 1, Force = 6, Damage = 85}, ["7.62mm"] = {Recoil = 1.5, Num = 1, Force = 4, Damage = 25}, ["5.56mm"] = {Recoil = 1.5, Num = 1, Force = 3, Damage = 20}, [".30-06"] = {Recoil = 1.5, Num = 1, Force = 3, Damage = 25}, ["12_Gauge"] = {Recoil = 2, Num = 9, Force = 2, Damage = 6}, [".410_Bore"] = {Recoil = 2, Num = 5, Force = 2, Damage = 6} } for k,v in pairs(AmmoStatMod) do -- k will return something like ".357" instead of a number (On purpose of course), and v returns the table of statistics. self.Owner:ViewPunch(Angle(self.Primary.Recoil,math.Rand(-self.Primary.Recoil/5,self.Primary.Recoil/5),0)) bullet.Num = self.Primary.Num bullet.Force = self.Primary.Force bullet.Damage = self.Primary.Damage end [/code] What I need help with is literally just getting the bullet data to change depending on self.Primary.Ammo, and I don't even know where to start
[code]local tAmmoInfo = AmmoStatMod[self.Primary.Ammo] or {Recoil = 0, Num = 1, Force = 1, Damage = 1} bullets.Num = tAmmoInfo.Num -- etc[/code]
Had to modify the table values because of the self.Primary.Ammo thing, but everything else is what you said: [code] local AmmoStatMod = { ["simbase_.22"] = {Recoil = 0.5, Num = 1, Force = 2, Damage = 10}, ["simbase_9mm"] = {Recoil = 1, Num = 1, Force = 2, Damage = 15}, ["simbase_.357"] = {Recoil = 3, Num = 1, Force = 4, Damage = 30}, ["simbase_.45"] = {Recoil = 1.5, Num = 1, Force = 3, Damage = 35}, ["simbase_50_BMG"] = {Recoil = 5, Num = 1, Force = 6, Damage = 85}, ["simbase_7.62mm"] = {Recoil = 1.5, Num = 1, Force = 4, Damage = 25}, ["simbase_5.56mm"] = {Recoil = 1.5, Num = 1, Force = 3, Damage = 20}, ["simbase_.30-06"] = {Recoil = 1.5, Num = 1, Force = 3, Damage = 25}, ["simbase_12_Gauge"] = {Recoil = 2, Num = 9, Force = 2, Damage = 6}, ["simbase_.410_Bore"] = {Recoil = 2, Num = 5, Force = 2, Damage = 6} } for k,v in pairs(AmmoStatMod) do local AmmoInfo = AmmoStatMod[self.Primary.Ammo] self.Owner:ViewPunch(Angle(-AmmoInfo.Recoil,math.Rand(-AmmoInfo.Recoil/3,AmmoInfo.Recoil/3),0)) bullet.Num = AmmoInfo.Num bullet.Force = AmmoInfo.Force bullet.Damage = AmmoInfo.Damage end [/code] and then I get: [code] [ERROR] lua/weapons/simbase_base.lua:73: attempt to index local 'AmmoInfo' (a nil value) [/code] Line 73 is the viewpunch, which is the first function to call AmmoInfo EDIT: I'm an idiot, the weapon I was using had the ammo set to simbase_12_gauge instead of simbase_12_Gauge. It works.
You should make it all lower-case, run string.lower on the SWEP.Primary.Ammo key, and add the variable default like I did in the last post.
Sorry, you need to Log In to post a reply to this thread.