Fu22y Posted August 14, 2020 Share Posted August 14, 2020 (edited) Hey everyone, I have only started learning C# in the last few days, and I am trying to create random peds that are spawned with random guns. I have tried using an enum with all of the ingame weapons along with a random class, but I havent got past that. This is what I have for my ped spawns at the moment. private void OnKeyDown(object sender, KeyEventArgs e) { Ped player = Game.Player.Character; if (e.KeyCode == Keys.G) { UI.Notify("Chaos Mod Enabled"); while (true) { var npc = World.CreateRandomPed(player.GetOffsetInWorldCoords(new Vector3( 5, -25, 0))); npc.Weapons.Give(WeaponHash.RPG, 100000, true, true); npc.Task.FightAgainst(player); Wait(1000); } } } Edited August 17, 2020 by Fu22y Link to comment https://gtaforums.com/topic/960057-spawning-random-peds-with-random-weapons/ Share on other sites More sharing options...
Tanjitsu Posted August 17, 2020 Share Posted August 17, 2020 (edited) You could try something like. private Array weaponValues = Enum.GetValues(typeof(WeaponHash)); private Random random = new Random(); private int armySize = 50; private void OnKeyDown(object sender, KeyEventArgs e) { Ped player = Game.Player.Character; if (e.KeyCode == Keys.G) { SpawnEnemyPed(player.Position.Around(30f)); } else if (e.KeyCode == Keys.H) { SpawnEnemyArmy(player.Position.Around(100f)); } } WeaponHash RandomWeapon() { return (WeaponHash)weaponValues.GetValue(random.Next(weaponValues.Length)); } void SpawnEnemyPed(Vector3 position, bool asArmy = false) { if (!asArmy) UI.Notify("SpawnEnemyPed"); Ped npc = World.CreateRandomPed(position); if (npc != null) { npc.Weapons.Give(RandomWeapon(), 100000, true, true); npc.Task.ClearAllImmediately(); npc.AlwaysKeepTask(); npc.Task.FightAgainst(Game.Player.Character); } } void SpawnEnemyArmy(Vector3 position) { UI.Notify("SpawnEnemyArmy"); for (int i = 0; i < armySize; i++) { SpawnEnemyPed(position, true); } } Edited August 17, 2020 by Tanjitsu Link to comment https://gtaforums.com/topic/960057-spawning-random-peds-with-random-weapons/#findComment-1071317809 Share on other sites More sharing options...
Fu22y Posted August 17, 2020 Author Share Posted August 17, 2020 1 hour ago, Tanjitsu said: You could try something like. private Array weaponValues = Enum.GetValues(typeof(WeaponHash)); private Random random = new Random(); private int armySize = 50; private void OnKeyDown(object sender, KeyEventArgs e) { Ped player = Game.Player.Character; if (e.KeyCode == Keys.G) { SpawnEnemyPed(player.Position.Around(30f)); } else if (e.KeyCode == Keys.H) { SpawnEnemyArmy(player.Position.Around(100f)); } } WeaponHash RandomWeapon() { return (WeaponHash)weaponValues.GetValue(random.Next(weaponValues.Length)); } void SpawnEnemyPed(Vector3 position, bool asArmy = false) { if (!asArmy) UI.Notify("SpawnEnemyPed"); Ped npc = World.CreateRandomPed(position); if (npc != null) { npc.Weapons.Give(RandomWeapon(), 100000, true, true); npc.Task.ClearAllImmediately(); npc.AlwaysKeepTask(); npc.Task.FightAgainst(Game.Player.Character); } } void SpawnEnemyArmy(Vector3 position) { UI.Notify("SpawnEnemyArmy"); for (int i = 0; i < armySize; i++) { SpawnEnemyPed(position, true); } } It works! Thank you very much for helping me!! Link to comment https://gtaforums.com/topic/960057-spawning-random-peds-with-random-weapons/#findComment-1071317918 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now