• Npc Weapon
    10 replies, posted
Hey I'm trying to make this weapon useable by npc's. This is what I wan't the npc to do when using the weapon: -Fire in bursts like with the normal npc weapons. -Reload. -Have a normal rate of fire. This is what the npc/weapon is doing right now: -Fiering with a high rate of fire. -Never reloads. -Runs up to the enemy like a mad dog never using cover. Here is the basic code for the weapon: [lua] -- 'Realistic' Gun base: FN SCAR-L 5.56x45mm Assault Rifle -- By MedicalHeadcrab if SERVER then AddCSLuaFile("shared.lua") SWEP.HoldType = "smg" end if CLIENT then SWEP.HoldType = "smg" SWEP.DrawAmmo = true SWEP.DrawCrosshair = false SWEP.ViewModelFOV = 76 SWEP.ViewModelFlip = true SWEP.CSMuzzleFlashes = true SWEP.Slot = 2 SWEP.SlotPos = 1 SWEP.IconLetter = "q" SWEP.DrawWeaponInfoBox = true killicon.AddFont("weapon_rif_scar", "CSKillIcons", SWEP.IconLetter, Color(255, 220, 0, 255)) end SWEP.HoldType = "smg" SWEP.Base = "rg_base" SWEP.Category = "MedicalHeadcrab's Rifles" ------------ -- Info -- ------------ SWEP.PrintName = "Mk 16 Mod 0" SWEP.Author = "MedicalHeadcrab" SWEP.Purpose = "" SWEP.Instructions = "The Special Forces Combat Assault Rifle Mk 16 Mod 0 (SCAR-L) is a 5.56mm assault rifle.\nHold use and press secondary fire to change fire modes.\nType: Assault Rifle\nOrigin: Belgium, United States\nManufacturer: Fabrique Nationale de Herstal\nWeight: 7.7 lb\nLength: N/A\nCartridge: 5.56x45mm NATO\nRate of Fire: 625 rounds/min\nMuzzle Velocity: 875 m/s\nClip Size: 30" ------------- -- Misc. -- ------------- SWEP.Weight = 7.7 function SWEP:GetCapabilities() return CAP_WEAPON_RANGE_ATTACK1 | CAP_INNATE_RANGE_ATTACK1 | CAP_WEAPON_RANGE_ATTACK2 | CAP_INNATE_RANGE_ATTACK2 end ---------------------- -- Primary Fire -- ---------------------- SWEP.Primary.Sound = Sound("Weapon_SCAR-L.Single") SWEP.Primary.Damage = 48 -- This determines both the damage dealt and force applied by the bullet. SWEP.Primary.NumShots = 1 SWEP.Primary.ClipSize = 30 SWEP.Primary.DefaultClip = 30 SWEP.Primary.Ammo = "smg1" SWEP.MuzzleVelocity = 875 -- How fast the bullet travels in meters per second. For reference, an AK47 shoots at about 750, an M4 shoots at about 900, and a Luger 9mm shoots at about 350 (source: Wikipedia) SWEP.FiresUnderwater = false ------------------------- -- Secondary Fire -- ------------------------- -- Secondary Fire is used to switch ironsights and firemodes SWEP.Secondary.ClipSize = -1 -- best left at -1 SWEP.Secondary.DefaultClip = -1 -- set to -1 if you don't use secondary ammo SWEP.Secondary.Ammo = "none" -- Leave this if you want your SWEP to have grenades, otherwise set to "none" if you don't use secondary ammo. --------------------------------------- -- Recoil, Spread, and Spray -- --------------------------------------- SWEP.RecoverTime = 0.4 -- Time in seconds it takes the player to re-steady his aim after firing. -- The following variables control the overall accuracy of the gun and typically increase with each shot -- Recoil: how much the gun kicks back the player's view. SWEP.MinRecoil = 0.36 SWEP.MaxRecoil = 0.76 SWEP.DeltaRecoil = 0.17 -- The recoil to add each shot. Same deal for spread and spray. -- Spread: the width of the gun's firing cone. More spread means less accuracy. SWEP.MinSpread = 0.0014 SWEP.MaxSpread = 0.033 SWEP.DeltaSpread = 0.0014 -- Spray: the gun's tendancy to point in random directions. More spray means less control. SWEP.MinSpray = 0 SWEP.MaxSpray = 1.4 SWEP.DeltaSpray = 0.16 --------------------------- -- Ironsight/Scope -- --------------------------- -- IronSightsPos and IronSightsAng are model specific paramaters that tell the game where to move the weapon viewmodel in ironsight mode. SWEP.IronSightsPos = Vector(2.9811, -2.9791, 0.559) -- Comment out this line of you don't want ironsights. This variable must be present if your SWEP is to use a scope. SWEP.IronSightsAng = Vector(0.0824, -0.0667, 0) SWEP.IronSightZoom = 1.3 -- How much the player's FOV should zoom in ironsight mode. SWEP.UseScope = false -- Use a scope instead of iron sights. SWEP.ScopeScale = 0.4 -- The scale of the scope's reticle in relation to the player's screen size. SWEP.ScopeZooms = {2,4} -- The possible magnification levels of the weapon's scope. If the scope is already activated, secondary fire will cycle through each zoom level in the table. SWEP.DrawParabolicSights = false -- Set to true to draw a cool parabolic sight (helps with aiming over long distances) ------------------------- -- Effects/Visual -- ------------------------- SWEP.ViewModel = "models/weapons/v_rif_scar.mdl" SWEP.WorldModel = "models/weapons/w_smg_ump45.mdl" SWEP.MuzzleEffect = "rg_muzzle_pistol" -- This is an extra muzzleflash effect -- Available muzzle effects: rg_muzzle_grenade, rg_muzzle_highcal, rg_muzzle_hmg, rg_muzzle_pistol, rg_muzzle_rifle, rg_muzzle_silenced, none SWEP.ShellEffect = "none" -- This is a shell ejection effect -- Available shell eject effects: rg_shelleject, rg_shelleject_rifle, rg_shelleject_shotgun, none SWEP.MuzzleAttachment = "1" -- Should be "1" for CSS models or "muzzle" for hl2 models SWEP.ShellEjectAttachment = "2" -- Should be "2" for CSS models or "1" for hl2 models ------------------- -- Modifiers -- ------------------- -- Modifiers scale the gun's recoil, spread, and spray based on the player's stance SWEP.CrouchModifier = 0.7 -- Applies if player is crouching. SWEP.IronSightModifier = 0.7 -- Applies if player is in iron sight mode. SWEP.RunModifier = 1.4 -- Applies if player is moving. SWEP.JumpModifier = 1.6 -- Applies if player is in the air (jumping) -- Note: the jumping and crouching modifiers cannot be applied simultaneously -------------------- -- Fire Modes -- -------------------- -- You can choose from a list of firemodes, or add your own! \0/ SWEP.AvailableFireModes = {"Auto","Semi"} -- What firemodes shall we use? -- "Auto", "Burst", "Semi", and "Grenade" are firemodes that are available by default. -- RPM is the rounds per minute the gun can fire for each mode (if applicable) SWEP.AutoRPM = 625 SWEP.SemiRPM = 300 SWEP.BurstRPM = 950 -- Burst RPM affects the space between the shots during the burst. The space between bursts is determined by SemiRPM. SWEP.DrawFireModes = true -- Set to true to allow drawing of a visual indicator for the current firemode. -- Additional parameters for the "Grenade" firemode SWEP.GrenadeDamage = 100 SWEP.GrenadeVelocity = 1400 SWEP.GrenadeRPM = 50 -- Custom firemode! --------------------------------------------- -- Firemode: UnderWaterShotgun -- --------------------------------------------- -- Description: A shotgun that fires underwater SWEP.FireModes = {} -- Don't touch this! SWEP.FireModes.UnderWaterShotgun = {} -- Our firemode's main table. -- If you want this firemode to be used, the part after the SWEP.FireModes. (in this case, "UnderWaterShotgun") should be defined as a string in the SWEP.AvailableFireModes table SWEP.UWShotgunRPM = 180 -- We can define our own variables for this firemode if we so desire SWEP.FireModes.UnderWaterShotgun.NumBullets = 8 -- Either way of adding variables is fine, as long as we call the right variable name when we need it -- Generally, a firemode consists of 4 main functions: the FireFunction, InitFunction,RevertFunction, and HUDDrawFunction -- This function is called when the player attacks and the firemode is active. SWEP.FireModes.UnderWaterShotgun.FireFunction = function(self) if not self:CanFire(self.Weapon:Clip1()) then return end -- Do we have enough ammo to fire? -- Note: if you want your firemode to use the secondary ammo, I reccomend replacing self.Weapon:Clip1() with self.Weapon:Ammo2() if not self.OwnerIsNPC then self:TakePrimaryAmmo(1) -- NPCs get infinate ammo, as they don't know how to reload -- ^ obviously, this should be self:TakeSecondaryAmmo(1) if your
Please apply lua tags to the code, as such: [NOPARSE][lua]your text[/lua][/NOPARSE]
Yeah sorry about that, fixed now
Try fixing your initialize, remember initialize is ran before the weapon is assigned an owner. Fixed version:[LUA]function SWEP:Initialize() self:SetWeaponHoldType("ar2") self:SetNPCMinBurst(3) self:SetNPCMaxBurst(10) self:SetNPCFireRate(1) self:SetCurrentWeaponProficiency(WEAPON_PROFICIENCY_PERFECT) end[/LUA]
Thanks, will try that at once
Ok so I have finally been able to make them shoot in bursts wich is really awesome with custom weapons, they shoot almoust exactly like with the normal weapons except that between each burst they lower the weapon and then bring it up again for the next burst. So, now npc battles sound even more awesome (with custom weapon sounds) and looks better witht the new models. BUT they still don't reload... ever. I have tried adding alot of code at various places with no success. Here I will show you some of the code I'm using: The shared.lua from the m4a1 rifle folder. weapon_rif_m4a1_npc: [lua] -- 'Realistic' Gun base: Colt M4A1 Carbine -- By MedicalHeadcrab include( "ai_translations.lua" ) if SERVER then AddCSLuaFile("shared.lua") SWEP.HoldType = "ar2" end if CLIENT then SWEP.HoldType = "ar2" SWEP.DrawAmmo = true SWEP.DrawCrosshair = false SWEP.ViewModelFOV = 76 SWEP.ViewModelFlip = true SWEP.CSMuzzleFlashes = true SWEP.Slot = 2 SWEP.SlotPos = 1 SWEP.IconLetter = "w" SWEP.DrawWeaponInfoBox = true killicon.AddFont("weapon_rif_m4a1", "CSKillIcons", SWEP.IconLetter, Color(255, 220, 0, 255)) end SWEP.HoldType = "ar2" SWEP.Base = "rg_base" SWEP.Category = "MedicalHeadcrab's Rifles" ------------ -- Info -- ------------ SWEP.PrintName = "Colt M4A1 Carbine" SWEP.Author = "MedicalHeadcrab" SWEP.Purpose = "" SWEP.Instructions = "The M4 carbine is a family of firearms based on the M16 assault rifle.\nHold use and press secondary fire to change fire modes.\nType: Assault Rifle\nOrigin: United States\nManufacturer: Colt Defense\nWeight: 5.9 lb\nLength: 33 in\nCartridge: 5.56x45mm NATO\nRate of Fire: 700 rounds/min\nMuzzle Velocity: 884 m/s\nClip Size: 30" ------------- -- Misc. -- ------------- SWEP.Spawnable = false SWEP.AdminSpawnable = false SWEP.Weight = 5.9 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false ---------------------- -- Primary Fire -- ---------------------- SWEP.Primary.Sound = Sound("Weapon_M4Carbine.Single") SWEP.Primary.Damage = 44 -- This determines both the damage dealt and force applied by the bullet. SWEP.Primary.NumShots = 1 SWEP.Primary.ClipSize = 30 SWEP.Primary.DefaultClip = 30 SWEP.Primary.Ammo = "smg1" SWEP.MuzzleVelocity = 884 -- How fast the bullet travels in meters per second. For reference, an AK47 shoots at about 750, an M4 shoots at about 900, and a Luger 9mm shoots at about 350 (source: Wikipedia) SWEP.FiresUnderwater = false ------------------------- -- Secondary Fire -- ------------------------- -- Secondary Fire is used to switch ironsights and firemodes SWEP.Secondary.ClipSize = -1 -- best left at -1 SWEP.Secondary.DefaultClip = -1 -- set to -1 if you don't use secondary ammo SWEP.Secondary.Ammo = "none" -- Leave this if you want your SWEP to have grenades, otherwise set to "none" if you don't use secondary ammo. --------------------------------------- -- Recoil, Spread, and Spray -- --------------------------------------- SWEP.RecoverTime = 0.4 -- Time in seconds it takes the player to re-steady his aim after firing. -- The following variables control the overall accuracy of the gun and typically increase with each shot -- Recoil: how much the gun kicks back the player's view. SWEP.MinRecoil = 0.42 SWEP.MaxRecoil = 0.8 SWEP.DeltaRecoil = 0.2 -- The recoil to add each shot. Same deal for spread and spray. -- Spread: the width of the gun's firing cone. More spread means less accuracy. SWEP.MinSpread = 0.0018 SWEP.MaxSpread = 0.012 SWEP.DeltaSpread = 0.0018 -- Spray: the gun's tendancy to point in random directions. More spray means less control. SWEP.MinSpray = 0 SWEP.MaxSpray = 1.4 SWEP.DeltaSpray = 0.16 --------------------------- -- Ironsight/Scope -- --------------------------- -- IronSightsPos and IronSightsAng are model specific paramaters that tell the game where to move the weapon viewmodel in ironsight mode. SWEP.IronSightsPos = Vector(2.3327, -3.7679, 0.5055) -- Comment out this line of you don't want ironsights. This variable must be present if your SWEP is to use a scope. SWEP.IronSightsAng = Vector(0.0974, -0.0413, 0) SWEP.IronSightZoom = 1.3 -- How much the player's FOV should zoom in ironsight mode. SWEP.UseScope = false -- Use a scope instead of iron sights. SWEP.ScopeScale = 0.4 -- The scale of the scope's reticle in relation to the player's screen size. SWEP.ScopeZooms = {2,4} -- The possible magnification levels of the weapon's scope. If the scope is already activated, secondary fire will cycle through each zoom level in the table. SWEP.DrawParabolicSights = false -- Set to true to draw a cool parabolic sight (helps with aiming over long distances) ------------------------- -- Effects/Visual -- ------------------------- SWEP.ViewModel = "models/weapons/v_hex_m4a1.mdl" SWEP.WorldModel = "models/weapons/w_rif_m4a1.mdl" SWEP.MuzzleEffect = "rg_muzzle_pistol" -- This is an extra muzzleflash effect -- Available muzzle effects: rg_muzzle_grenade, rg_muzzle_highcal, rg_muzzle_hmg, rg_muzzle_pistol, rg_muzzle_rifle, rg_muzzle_silenced, none SWEP.ShellEffect = "none" -- This is a shell ejection effect -- Available shell eject effects: rg_shelleject, rg_shelleject_rifle, rg_shelleject_shotgun, none SWEP.MuzzleAttachment = "1" -- Should be "1" for CSS models or "muzzle" for hl2 models SWEP.ShellEjectAttachment = "2" -- Should be "2" for CSS models or "1" for hl2 models ------------------- -- Modifiers -- ------------------- -- Modifiers scale the gun's recoil, spread, and spray based on the player's stance SWEP.CrouchModifier = 0.7 -- Applies if player is crouching. SWEP.IronSightModifier = 0.7 -- Applies if player is in iron sight mode. SWEP.RunModifier = 1.4 -- Applies if player is moving. SWEP.JumpModifier = 1.6 -- Applies if player is in the air (jumping) -- Note: the jumping and crouching modifiers cannot be applied simultaneously -------------------- -- Fire Modes -- -------------------- -- You can choose from a list of firemodes, or add your own! \0/ SWEP.AvailableFireModes = {"Auto","Semi"} -- What firemodes shall we use? -- "Auto", "Burst", "Semi", and "Grenade" are firemodes that are available by default. -- RPM is the rounds per minute the gun can fire for each mode (if applicable) SWEP.AutoRPM = 700 SWEP.SemiRPM = 300 SWEP.BurstRPM = 950 -- Burst RPM affects the space between the shots during the burst. The space between bursts is determined by SemiRPM. SWEP.DrawFireModes = true -- Set to true to allow drawing of a visual indicator for the current firemode. -- Additional parameters for the "Grenade" firemode SWEP.GrenadeDamage = 100 SWEP.GrenadeVelocity = 1400 SWEP.GrenadeRPM = 50 -- Custom firemode! --------------------------------------------- -- Firemode: UnderWaterShotgun -- --------------------------------------------- -- Description: A shotgun that fires underwater SWEP.FireModes = {} -- Don't touch this! SWEP.FireModes.UnderWaterShotgun = {} -- Our firemode's main table. -- If you want this firemode to be used, the part after the SWEP.FireModes. (in this case, "UnderWaterShotgun") should be defined as a string in the SWEP.AvailableFireModes table SWEP.UWShotgunRPM = 180 -- We can define our own variables for this firemode if we so desire SWEP.FireModes.UnderWaterShotgun.NumBullets = 8 -- Either way of adding variables is fine, as long as we call the right variable name when we need it -- Generally, a firemode consists of 4 main functions: the FireFunction, InitFunction,RevertFunction, and HUDDrawFunction -- This function is called when the player attacks and the firemode is active. SWEP.FireModes.UnderWaterShotgun.FireFunction = function(self) if not self:CanFire(self.Weapon:Clip1()) then return end -- Do we have enough ammo to fire? -- Note: if you want your firemode to use the secondary ammo, I reccomend replacing self.Weapon:Clip1() with self.Weapon:Ammo2() if not self.OwnerIsNPC then self:TakePrimaryAmmo(1) -- NPCs get infinate ammo, as they don't know how to reload -- ^ obviously, this should be self:TakeSecondaryAmmo(1) if your firemode uses secondary ammo. end --Fire ze bullets!
You have been trying to figure this out for 2 days? O.o And couldn't make it? NPCs are bugged to hell dealing with sweps, so you'll have to completely recreate the whole reloading process, not that difficult. Just create another variable of NPC clip and NPC max clip, reduce the clip every attack they make, check if they have anything in the clip left on primary attack, if not make an NPC Reload function and call it from the primary attack, in it do the schedule of NPC reload, restore the clip to the max clip and play the sound which you want. Did a more complex version of the thing I've just said when I started coding.
Well, guess you are smarter than me then. I understood some of what you just wrote but could you please make it into a code? I would be very grateful :)
[QUOTE=Jack Hamilto;28726289]Well, guess you are smarter than me then. I understood some of what you just wrote but could you please make it into a code? I would be very grateful :)[/QUOTE] Doesn't mean you're not as smart as me, it means you have less experience on this subject. I don't really have time to look through all of your code, but this should do:[LUA]SWEP.NPC_Clip_Size = 30 SWEP.NPC_Reload_Sound = "Weapon_AR2.NPC_Reload" SWEP.NPC_Reload_SDelay = 0.8 function SWEP:NPCReload() if self.Owner:IsNPC() then self.Owner:SetSchedule(SCHED_RELOAD) self.NPCClip = self.NPC_Clip_Size timer.Simple(self.NPC_Reload_SDelay, function() if ValidEntity(self.Owner) then self.Weapon:EmitSound(self.NPC_Reload_Sound) end end) end end //Place the code bellow somewhere in your primary attack function, at the end somewhere if self.Owner():IsNPC() then self.NpcClip = self.NpcClip- 1 if self.NpcClip <= 0 then self:NPCReload() end end [/LUA]Off the top of my head, but should work.
Thanks mate, will try to play around a bit with that code and see if I can get it to work. Thanks!
[QUOTE=Jack Hamilto;28726891]Thanks mate, will try to play around a bit with that code and see if I can get it to work. Thanks![/QUOTE] No problem.
Sorry, you need to Log In to post a reply to this thread.