Jump to content

Spawning Random Peds With Random Weapons


Recommended Posts

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 by Fu22y
Link to comment
https://gtaforums.com/topic/960057-spawning-random-peds-with-random-weapons/
Share on other sites

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 by Tanjitsu
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!!

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 0 User Currently Viewing
    0 members, 0 Anonymous, 0 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.