Jump to content

How to stop ped switch their weapon to unarmed


Recommended Posts

I want to make ped use only the weapon that I gave to them. This is my code:

  foreach (RelationshipGroup group in Enum.GetValues(typeof(RelationshipGroup)))                                    currentPed.ChangeRelationship(group, Relationship.Hate);                                currentPed.BecomeMissionCharacter();                                List<GTA.Weapon> weapons = new List<GTA.Weapon>() { GTA.Weapon.Rifle_AK47, GTA.Weapon.Rifle_M4, GTA.Weapon.Shotgun_Baretta, GTA.Weapon.Shotgun_Basic,                                 GTA.Weapon.SMG_MP5, GTA.Weapon.SMG_Uzi, GTA.Weapon.Thrown_Grenade, GTA.Weapon.Thrown_Molotov, GTA.Weapon.Handgun_Glock };                                Random rnd = new Random();                                GTA.Weapon randomWeapon = weapons[rnd.Next(1, weapons.Count)];                                currentPed.Weapons.Select(randomWeapon);                                currentPed.Weapons.Current.Ammo = 500;                                currentPed.Weapons.Select(randomWeapon);                                currentPed.CowerInsteadOfFleeing = true;                                currentPed.Armor = 50;                                currentPed.CanSwitchWeapons = false;                                currentPed.WantedByPolice = true;                                currentPed.WillUseCarsInCombat = false;                                currentPed.Task.ClearAllImmediately();                                currentPed.Task.FightAgainstHatedTargets(500f);                                Blip bl = currentPed.AttachBlip();                                bl.Color = BlipColor.Green;                                gPedBlipDict.Add(currentPed, bl);                                break;


So when I test this, ped gets selected, does a couple of shots, then switches his weapon to unarmed and starts melee with closest ped. How do I prevent this? Setting unarmed ammo to negative or zero doesnt helps. I did weapon selection twice on debug purpouse, because maybe if I set ammo for weapon ped doesnt have it have no effect. After a minute of that melee brawling, ped just starts run away scared.

 

And how to make ped to attack cars and not exit car if shooting is happens nearby?

Edited by jstq
Link to comment
https://gtaforums.com/topic/712123-how-to-stop-ped-switch-their-weapon-to-unarmed/
Share on other sites

LordOfTheBongs

Try changing his relationshipgroup to one of the gangs... i think u need to mess with the DecisionMaker stuff (i never used before) to alter how he acts but i think making them a gangmember relgroup also makes them aggressive and use weapons if armed

Edited by LordOfTheBongs
foreach (Ped currentPed in pedsNearBy)                        {                            if (!currentPed.Exists() || currentPed == Player.Character|| !currentPed.isAliveAndWell || currentPed.isRequiredForMission)                                continue;                            else                            {                                currentPed.CowerInsteadOfFleeing = true;                                currentPed.WillUseCarsInCombat = true;                            }                            if (!gPedBlipDict.ContainsKey(currentPed) && !gVehBlipDict.ContainsKey(currentPed) && !currentPed.isInCombat)                            {                                foreach (RelationshipGroup group in Enum.GetValues(typeof(RelationshipGroup)))                                    currentPed.ChangeRelationship(group, Relationship.Hate);                                currentPed.ChangeRelationship(RelationshipGroup.Gang_Italian,Relationship.Companion);                                currentPed.BecomeMissionCharacter();                                GivePedRandomWeapon(currentPed);                                currentPed.CowerInsteadOfFleeing = true;                                currentPed.Armor = 50;                                currentPed.CanSwitchWeapons = false;                                currentPed.WantedByPolice = true;                                currentPed.WillUseCarsInCombat = false;                                currentPed.Task.FightAgainstHatedTargets(500f);                                Blip bl = currentPed.AttachBlip();                                bl.Color = BlipColor.Green;                                gPedBlipDict.Add(currentPed, bl);                                break;                            }                        }    private void GivePedRandomWeapon(Ped ex_Ped)        {            List<GTA.Weapon> weapons = new List<GTA.Weapon>() { GTA.Weapon.Rifle_AK47, GTA.Weapon.Rifle_M4, GTA.Weapon.Shotgun_Baretta, GTA.Weapon.Shotgun_Basic,                                GTA.Weapon.SMG_MP5, GTA.Weapon.SMG_Uzi, GTA.Weapon.Thrown_Grenade, GTA.Weapon.Thrown_Molotov, GTA.Weapon.Handgun_Glock };            Random rnd = new Random();            GTA.Weapon randomWeapon = weapons[rnd.Next(1, weapons.Count)];            ex_Ped.Weapons.Select(randomWeapon);            ex_Ped.Weapons.Current.Ammo = 500;            ex_Ped.Weapons.Select(randomWeapon);        }

just noticed how old lady gets shotgun, does 2 shots and DROP it on the ground and starts melee... WTF? And some other peds shooting fine until they die...

Now I have another problem with armed peds..

LQrilDW.jpg

Whenever I try they just stand still

  foreach (RelationshipGroup group in Enum.GetValues(typeof(RelationshipGroup)))                                currentPed.ChangeRelationship(group, Relationship.Hate);                            currentPed.RelationshipGroup = RelationshipGroup.Gang_Italian;                            currentPed.BecomeMissionCharacter();                            GivePedRandomWeapon(currentPed);                            currentPed.CowerInsteadOfFleeing = true;                            currentPed.Armor = 50;                            currentPed.CanSwitchWeapons = false;                            currentPed.WantedByPolice = true;                            currentPed.WillUseCarsInCombat = true;                            TaskSequence ts = new TaskSequence();                            ts.AddTask.FightAgainstHatedTargets(500f);                            ts.AddTask.FightAgainstHatedTargets(500f);                            ts.AddTask.FightAgainstHatedTargets(500f);                            currentPed.Task.PerformSequence(ts);

What is wrong there and is there a way to check if ped standing still without any task?

Tried to push them with car, they dont move untill they die. But other part of code with tasks:

    currentPed.BecomeMissionCharacter();                            currentPed.WillUseCarsInCombat = true;                            TaskSequence ts = new TaskSequence();                            ts.AddTask.EnterVehicle(currVeh, VehicleSeat.Driver);                            ts.AddTask.CruiseWithVehicle(currVeh, 250f, false);                            currentPed.Task.PerformSequence(ts);

works fine

LordOfTheBongs

I heard the TaskSequence stuff was bugged but I never tested myself, just what I heard from a more experienced coder.

 

I'm curious what you are trying to do? Is this like a riot script where all peds should be fighting?

One ped should do riot thing, other should steal cars... Trying to bring some life to streets.

   TaskSequence ts = new TaskSequence();                            ts.AddTask.FightAgainstHatedTargets(50f);                            ts.AddTask.Die();                            currentPed.Task.PerformSequence(ts);

Ped gets selected and instantly dies. I guess there is no hated targets for him in this raduis? But I saw at least one other ped nearby.

tried this sequence

   TaskSequence ts = new TaskSequence();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                            ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                                                         ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                             ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                            ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                            ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                             ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                             ts.AddTask.WanderAround();                            ts.AddTask.FightAgainstHatedTargets(100f, 9999999);                            currentPed.Task.PerformSequence(ts);

ped wanders around, stops for a moment, then starts wandering again. I've shotted at him, he is just wandering after wakes up.

Edited by jstq
LordOfTheBongs

not really sure how TaskSequences work and what triggers them to know a task is completed. I wouldn't rely on it. You can manually write a sequence of tasks too and actually control how it works.

 

im curious, the loop you do... foreach (Ped currentPed in pedsNearBy)

 

do u just do that once? or is that in a tick and u run that loop over and over... not really sure but if u paste your whole script maybe i can test it

Edited by LordOfTheBongs

just a little test code http://pastebin.com/QzTQGSJD this is the only script that's connected to the game

gTMAIN - creates crazy driver, works fine
gTRampage -creates mad ped, doesnt works, worked yesterday.
gTStealers - creates ped that steal car from street and drives away
gTCleaning - cleans sh*t. Probably the source of the problem
gLockCars - locks nearby cars and checks if player steals car and gives warning level

gScheduledWantedLevel - adds wanted level if player doesnt get away far

Edited by jstq
LordOfTheBongs

uJmeCYm.png

 

mVedMMw.png

 

i started cleaning up a little code, i do things a certain way, maybe you might like the style... also notice how i create the Random object on load... you need to do this because if u create a new Random everytime u need then it will 99.9% of the time choose the same weapon... google how the random object works... doing something like new Random.Next() will always give u same result. So create the object on load and then it will properly randomize everytime u call

 

Also I notice u start the random at 1 instead of 0... so it will never choose the AK... you should just remove the AK from the array and start the Next() at 0 or maybe u meant to include the AK and it is just a typo... but change to...

GTA.Weapon randomWeapon = weapons[rnd.Next(0, weapons.Count)];
Edited by LordOfTheBongs

I'm creating it once per tick, so its different. Not passing it into random weapon function is my mistake though, ty. Also, I'm always trying to minimize global variables count, which is very hard in this gta coding with those timers-oriented code style.

 

So the weapon func should look like this

 

 private void GivePedRandomWeapon(Ped ex_Ped, Random ex_rand)        {            List<GTA.Weapon> weapons = new List<GTA.Weapon>() { GTA.Weapon.Rifle_AK47, GTA.Weapon.Rifle_M4, GTA.Weapon.Shotgun_Baretta, GTA.Weapon.Shotgun_Basic,                                GTA.Weapon.SMG_MP5, GTA.Weapon.SMG_Uzi, GTA.Weapon.Thrown_Grenade, GTA.Weapon.Thrown_Molotov, GTA.Weapon.Handgun_Glock };            GTA.Weapon randomWeapon = weapons[ex_rand.Next(1, weapons.Count)];            ex_Ped.Weapons.Current.Ammo = 500;            ex_Ped.Weapons.Select(randomWeapon);           }
I've also saw rare unhandled exception that is probably was thrown in the only place without try block - cleaning tick Edited by jstq
LordOfTheBongs

u know what, i came to the realization i have helped u before with this... i just tried this and everyone is going crazy, u can use some ideas maybe... i wrote this a long time ago so it might need work...

namespace Riot{    using System;    using System.Windows.Forms;    using System.Collections.Generic;    using GTA;    public class Main : Script    {        List<Ped> armedPeds;        Random random;        RelationshipGroup[] groups;        bool toggle;        public Main()        {            armedPeds = new List<Ped>();            groups = new RelationshipGroup[] { RelationshipGroup.Civillian_Female, RelationshipGroup.Civillian_Male, RelationshipGroup.Cop, RelationshipGroup.Fireman, RelationshipGroup.Gang_AfricanAmerican, RelationshipGroup.Gang_Albanian, RelationshipGroup.Gang_Biker1, RelationshipGroup.Gang_Biker2, RelationshipGroup.Gang_ChineseJapanese, RelationshipGroup.Gang_Irish, RelationshipGroup.Gang_Italian, RelationshipGroup.Gang_Jamaican, RelationshipGroup.Gang_Korean, RelationshipGroup.Gang_PuertoRican, RelationshipGroup.Gang_Russian1, RelationshipGroup.Gang_Russian2, RelationshipGroup.Medic, RelationshipGroup.Player };            random = new Random();            BindKey(Keys.Insert, Toggle);        }        private void Toggle()        {            toggle = !toggle;            if (toggle) { Tick += Main_Tick; Game.WantedMultiplier = 0f; }            else { Tick -= Main_Tick; Game.WantedMultiplier = 1f; }            Game.DisplayText("Riot " + (toggle ? "En" : "Dis") + "abled");        }        private void Main_Tick(object sender, EventArgs e)        {            CleanList();            foreach (Ped ped in World.GetPeds(Game.LocalPlayer.Character.Position, 100f))            {                if (!Game.Exists(ped)) continue;                if (Game.LocalPlayer.Character == ped) continue;                if (!ped.isAliveAndWell) continue;                if (armedPeds.Contains(ped)) continue;                HateEveryone(ped);                ped.BlockGestures = true;                ped.BlockPermanentEvents = true;                ped.Task.AlwaysKeepTask = true;                ped.CanSwitchWeapons = true;                ped.BecomeMissionCharacter();                if (ped.isInVehicle())                {                    ped.WillDoDrivebys = true;                    if (random.Next(0, 2) == 0) { ped.Weapons.Uzi.Ammo = 500; }                    else { ped.Weapons.MP5.Ammo = 500; }                    if (ped.CurrentVehicle.GetPedOnSeat(VehicleSeat.Driver) == ped) { ped.Task.CruiseWithVehicle(ped.CurrentVehicle, 100f, false); }                    else ped.Task.FightAgainstHatedTargets(100f);                }                else                {                    ArmPed(ped);                    ped.Task.FightAgainstHatedTargets(100f);                }                armedPeds.Add(ped);            }        }        private void HateEveryone(Ped ped)        {            foreach (RelationshipGroup group in groups)            {                if (ped.RelationshipGroup == RelationshipGroup.Cop && group == RelationshipGroup.Cop) continue;                ped.ChangeRelationship(group, Relationship.Hate);            }        }        private void ArmPed(Ped ped)        {            int selection = random.Next(0, 5);            switch (selection)            {                case 0:                    if (random.Next(0, 2) == 0) { ped.Weapons.Glock.Ammo = 500; ped.Weapons.Glock.Select(); } else { ped.Weapons.DesertEagle.Ammo = 500; ped.Weapons.DesertEagle.Select(); }                    break;                case 1: if (random.Next(0, 2) == 0) { ped.Weapons.Uzi.Ammo = 500; ped.Weapons.Uzi.Select(); } else { ped.Weapons.MP5.Ammo = 500; ped.Weapons.MP5.Select(); }                    break;                case 2: if (random.Next(0, 2) == 0) { ped.Weapons.BasicShotgun.Ammo = 500; ped.Weapons.BasicShotgun.Select(); } else { ped.Weapons.BarettaShotgun.Ammo = 500; ped.Weapons.BarettaShotgun.Select(); }                    break;                case 3: if (random.Next(0, 2) == 0) { ped.Weapons.AssaultRifle_AK47.Ammo = 500; ped.Weapons.AssaultRifle_AK47.Select(); } else { ped.Weapons.AssaultRifle_M4.Ammo = 500; ped.Weapons.AssaultRifle_M4.Select(); }                    break;                case 4: if (random.Next(0, 2) == 0) { ped.Weapons.MolotovCocktails.Ammo = 500; ped.Weapons.MolotovCocktails.Select(); } else { ped.Weapons.Grenades.Ammo = 500; ped.Weapons.Grenades.Select(); }                    break;            }        }        private void CleanList()        {            for (int i = armedPeds.Count - 1; i > -1; i--)            {                if (!Game.Exists(armedPeds[i]))                {                    armedPeds.RemoveAt(i);                    continue;                }                if (!armedPeds[i].isAliveAndWell || armedPeds[i].Position.DistanceTo(Game.LocalPlayer.Character.Position) > 100f)                {                    armedPeds[i].NoLongerNeeded();                    armedPeds.RemoveAt(i);                }            }        }    }}

The main thing I cant fighure out is why ped looses their task to start fighting, but does other tasks fine.

i dont think u should use task sequences

Edited by LordOfTheBongs
 foreach (RelationshipGroup group in Enum.GetValues(typeof(RelationshipGroup)))                                currentPed.ChangeRelationship(group, Relationship.Hate);                            currentPed.RelationshipGroup = RelationshipGroup.Gang_Italian;                            currentPed.ChangeRelationship(RelationshipGroup.Gang_Italian, Relationship.Companion);                            currentPed.BecomeMissionCharacter();                            GivePedRandomWeapon(currentPed, rnd);                            currentPed.BlockGestures = true;                            currentPed.BlockPermanentEvents = true;                            currentPed.Task.AlwaysKeepTask = true;                            currentPed.Armor = 50;                            currentPed.CanSwitchWeapons = false;                            currentPed.WantedByPolice = true;                            currentPed.Task.ClearSecondary();                                    currentPed.Task.FightAgainstHatedTargets(100f);                                                      Blip bl = currentPed.AttachBlip();                            bl.Color = BlipColor.Green;                            gPedBlipDict.Add(currentPed, bl);

nothing changed, ped stands still. I think its something really messed up in my code because I remember that thing was worked, meh

currentPed.BlockPermanentEvents = true;

this sh*t was the problem

now fighuring out why ped sometimes starts run away scared

or stays still until pushed and then shots once, drops his weapon and starts melee

Edited by jstq

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.